2013-03-17 134 views
3

我使用using System.Net.Mail;我們可以使用asp.net和c#從localhost發送郵件嗎?

和下面的代碼來發送郵件

MailMessage message = new MailMessage(); 
     SmtpClient client = new SmtpClient(); 

     // Set the sender's address 
     message.From = new MailAddress("fromAddress"); 

    // Allow multiple "To" addresses to be separated by a semi-colon 
     if (toAddress.Trim().Length > 0) 
     { 
      foreach (string addr in toAddress.Split(';')) 
      { 
       message.To.Add(new MailAddress(addr)); 
      } 
     } 
     // Allow multiple "Cc" addresses to be separated by a semi-colon 
     if (ccAddress.Trim().Length > 0) 
     { 
      foreach (string addr in ccAddress.Split(';')) 
      { 
       message.CC.Add(new MailAddress(addr)); 
      } 
     } 
     // Set the subject and message body text 
     message.Subject = subject; 
     message.Body = messageBody; 

     // Set the SMTP server to be used to send the message 
     client.Host = "YourMailServer"; 

     // Send the e-mail message 
     client.Send(message); 

的主機我提供client.Host = "localhost";

此其下降與錯誤

無連接可以作出因爲目標機器積極 拒絕它 some_ip_address_he再

,當我使用client.Host = "smtp.gmail.com";

我獲得以下錯誤

連接嘗試失敗,因爲連接的方沒有 一段時間後正確響應或已建立的連接 因連接的主機無法響應而失敗

我無法通過本地主機發送郵件。 請幫助我,我是新來的C#請糾正我在代碼中我錯了..?

+2

您需要一個願意爲您發送郵件的SMTP服務器。 (和登錄,以及正確的端口和SSL設置) – SLaks 2013-03-17 16:10:39

+0

如何從我的機器發送郵件,我不部署代碼。我在本地主機上工作。當我使用主機「smtp.gmail.com」;它的faling ..我不知道如何更正端口和SSL設置。 – 2013-03-17 16:12:34

回答

5

下面是一些代碼,用於通過Gmail發送郵件工作(代碼從某處這裏計算器它類似於此代碼:Gmail: How to send an email programmatically):

using (var client = new SmtpClient("smtp.gmail.com", 587) 
{ 
    Credentials = new NetworkCredential("[email protected]", "yourpassword"), 
    EnableSsl = true 
}) 
{ 
    client.Send("[email protected]", "[email protected]", "subject", message); 
} 
+0

如果你在某處找到了代碼,你可以鏈接到原始代碼嗎? – svick 2013-03-17 17:13:24

+0

是的。問題是我前一段時間在某處發現它,然後一直使用它一段時間,現在我不記得確切的來源。 – tomsv 2013-03-17 17:30:52

+0

@dontomaso感謝它的工作對我來說非常好.....再次感謝..dontomaso – 2013-03-17 17:50:12

1

將此代碼放在網頁一個<configuration> </configuration>內。配置文件

<system.net> 
<mailSettings> 
    <smtp> 
    <network host="smtp.gmail.com" enableSsl="true" port="587" userName="[email protected]" password="yourpassword" /> 
    </smtp> 
</mailSettings> 
</system.net> 

然後後端代碼

MailMessage message = new MailMessage(); 
message.IsBodyHtml = true; 
message.From = new MailAddress("[email protected]"); 
message.To.Add(new MailAddress(TextBoxEadd.Text)); 
message.CC.Add(new MailAddress("[email protected]")); 
message.Subject = "New User Registration ! "; 
message.Body = "HELLO"; 
sr.Close(); 
SmtpClient client = new SmtpClient(); 
client.Send(message); 

我希望這段代碼能幫助你! :)

+0

這爲我工作!但你能告訴我爲什麼你需要定義message.From =新的MailAddress(「[email protected]」),當你在web配置中指定你的憑證,即userName =「[email protected]」 – Haris 2015-01-09 21:17:55

2

要發送郵件從client.Host = "localhost"您需要設置本地SMTP服務器。

要通過Google(或通過任何其他SMTP服務器,包括您自己的本地SMTP)發送郵件,您必須設置用戶名,密碼,ssl設置 - 所有SMTP服務器都需要您選擇,並且您需要閱讀他們的幫助。

例如Google says您需要SSL,端口465或587,服務器smtp.gmail.com以及您的用戶名和密碼。

您可以在.config文件中分配所有這些值。

<system.net> 
    <mailSettings> 
    <smtp> 
     <network host="smtp.gmail.com" enableSsl="true" port="587" userName="[email protected]" password="password" /> 
    </smtp> 
    </mailSettings> 
</system.net> 

,或設置爲SmtpClient在代碼中每次使用前:

client.Host = "smtp.gmail.com"; 
client.Port = 587; 
client.EnableSSL = true; 
client.Credentials = new NetworkCredential("[email protected]", "password"); 
0

使用本Line..Dont使用端口和主機名

LocalClient.DeliveryMethod = SmtpDeliveryMethod。PickupDirectoryFromIis;

相關問題