我有一個web應用程序,我將它設置爲使用MailHelper.cs和web.config設置通過gmail帳戶自動發送電子郵件。我的應用程序已經工作了10天,現在它給了我錯誤,並在發送電子郵件時停止處理錯誤,它是「發送郵件失敗。」和內側的例外是「A套接字操作嘗試一個無法連接的主機216.239.59.109:587」這裏是代碼:使用MailHelper.cs的電子郵件問題
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.gmail.com"
port="587"
userName="[email protected]"
password="mypass"/>
</smtp>
</mailSettings>
using System.Net.Mail;
public class MailHelper
{
/// <summary>
/// Sends an mail message
/// </summary>
/// <param name="from">Sender address</param>
/// <param name="to">Recepient address</param>
/// <param name="bcc">Bcc recepient</param>
/// <param name="cc">Cc recepient</param>
/// <param name="subject">Subject of mail message</param>
/// <param name="body">Body of mail message</param>
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();
mSmtpClient.Host = "smtp.gmail.com";
mSmtpClient.Port = 587;
mSmtpClient.EnableSsl = true;
// Send the mail message
mSmtpClient.Send(mMailMessage);
}
}
當我需要新的項目需要MailHelper時,我再次回到這個問題。一個比它的答案更有用的問題。 – 2013-01-30 06:42:12