2012-03-27 134 views
10

有沒有任何例子可以解釋我從本地主機服務器發送電子郵件? 我已經寫了這個例子,但它不起作用的錯誤是「發送郵件失敗」。通過本地主機服務器在asp.net發送電子郵件

SmtpClient smtpClient = new SmtpClient(); 
     smtpClient.Host = "localhost"; 
     smtpClient.Port = 25; 
     smtpClient.EnableSsl = false; 
     smtpClient.Credentials = new NetworkCredential("[email protected]", "secret"); 
     smtpClient.Send("[email protected]", "[email protected]", "Let’s eat lunch!", "Lunch at the Steak House?");//here is the error 

我應該怎麼做web.config?

+1

您是否在本地主機上配置了SMTP? – Habib 2012-03-27 11:25:55

+0

你正在使用本地主機,並使用雅虎的憑據,我不認爲這會工作 – Habib 2012-03-27 11:31:08

回答

19

這裏亞去:) localhost-with-aspnet-without-smtp-server

讓我請知道它是否適合你,你需要它的方式。


上面的鏈接不起作用,所以我會改進答案。

出於測試目的,我們可以使用localhost這樣:How to Test Email Without Configure SMTP in ASP.NET

萬一鏈路出現故障再次,基本上我們必須修改web.config中像這樣:

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="SpecifiedPickupDirectory"> 
     <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

和C#代碼

MailMessage mailMessage = new MailMessage(); 
    MailAddress fromAddress = new MailAddress("[email protected]"); 
    mailMessage.From = fromAddress; 
    mailMessage.To.Add("[email protected]"); 
    mailMessage.Body = "This is Testing Email Without Configured SMTP Server"; 
    mailMessage.IsBodyHtml = true; 
    mailMessage.Subject = " Testing Email"; 
    SmtpClient smtpClient = new SmtpClient(); 
    smtpClient.Host = "localhost"; 
    smtpClient.Send(mailMessage); 

這將輸出一個文件到我們想要的目錄。

+0

旁註:'System.Web.Mail.MailMessage'現已被棄用。你可以使用'System.Net.Mail.MailMessage'。 – rst 2017-07-23 09:43:54

2

您需要在web.config中指定SMTP服務器的設置。有幾個例子在網上(如this

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
      <network host="smtp.mail.com" userName="[email protected]" password="pwd" port="25"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

然後,你可以簡單地使用SmtpClient類發送:

SmtpClient smtpClient = new SmtpClient(); 
smtpClient.EnableSsl = true; 

MailMessage msg = new MailMessage(); 
msg.To.Add("[email protected]"); 
msg.Subject = "test"; 
msg.Body = "test body"; 

smtpClient.Send(msg); 
+0

你不需要任何額外的SMTP服務器......;) – walther 2012-03-27 11:33:44

+0

我同意它是否安裝在本地主機上。如果您使用外部提供商(例如Rackspace),那該怎麼辦? – Strillo 2012-03-27 11:38:21

2

這裏是樣本:

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body) 
{ 
    // Instantiate a new instance of MailMessage 
    MailMessage mMailMessage = new MailMessage(); 

    // Set the sender address of the mail message 
    mMailMessage.From = new MailAddress(from); 
    // Set the recepient address of the mail message 
    mMailMessage.To.Add(new MailAddress(to)); 

    // Check if the bcc value is null or an empty string 
    if ((bcc != null) && (bcc != string.Empty)) 
    { 
     // Set the Bcc address of the mail message 
     mMailMessage.Bcc.Add(new MailAddress(bcc)); 
    }  // Check if the cc value is null or an empty value 
    if ((cc != null) && (cc != string.Empty)) 
    { 
     // Set the CC address of the mail message 
     mMailMessage.CC.Add(new MailAddress(cc)); 
    }  // Set the subject of the mail message 
    mMailMessage.Subject = subject; 
    // Set the body of the mail message 
    mMailMessage.Body = body; 

    // Set the format of the mail message body as HTML 
    mMailMessage.IsBodyHtml = true; 
    // Set the priority of the mail message to normal 
    mMailMessage.Priority = MailPriority.Normal; 

    // Instantiate a new instance of SmtpClient 
    SmtpClient mSmtpClient = new SmtpClient(); 
    // Send the mail message 
    mSmtpClient.Send(mMailMessage); 
} 

並調用該函數:

SendMailMessage("[email protected]", "[email protected]", "[email protected]", "[email protected]", "Sample Subject", "Sample body of text for mail message") 
+0

我得到了'System.InvalidOperationException:沒有指定SMTP主機.'。 – mattalxndr 2017-10-25 16:39:18

1
bool ret = true; 

      try 
      { 
       string _smtpServer = ConfigurationSettings.AppSettings["appEmailHost"]; 

       Web.Mail.Mail mail = new Web.Mail.Mail(_smtpServer,   
     System.Web.Mail.MailFormat.Html, System.Web.Mail.MailPriority.Normal); 
       mail._From = [email protected]; 
       mail._To = [email protected]; 
       mail._Subject = subject; 

       mail._Body = body; 
       mail.SendMail(); 
       ret = true; 
      } 
      catch(Exception exp) 
      { 
       _GravaErro(exp); 
       ret = false; 
      } 

      return ret; 
相關問題