发送电子邮件
public class EmailNotificationService{
/// <summary>
/// 构造一个邮件通知服务类的实例。
/// </summary>
/// <param name="smtpService">SMTP服务器的IP地址</param>
/// <param name="enableSSL">是否使用SSL连接SMTP服务器器</param>
/// <param name="port">SMTP服务器端口</param>
/// <param name="loginName">用于登录SMTP服务器的用户名</param>
/// <param name="password">登录密码</param>
private readonly string m_smtpService;
private readonly string m_loginName;
private readonly string m_password;
private readonly bool m_enableSSL;
private readonly int m_port;
public EmailNotificationService(string smtpService,bool enableSSL,int port,string loginName,string password)
{
this.m_smtpService = smtpService;
this.m_password = password;
this.m_loginName = loginName;
this.m_enableSSL = enableSSL;
this.m_port = port;
}
/// <summary>
///
/// </summary>
/// <param name="sendName"></param>
/// <param name="address"></param>
/// <param name="title"></param>
/// <param name="content"></param>
public void SendTo(string sendName,string address,string title,string content)
{
MailMessage mail = new MailMessage();
mail.To.Add(address);
mail.From = new MailAddress(this.m_loginName, sendName, Encoding.UTF8);
mail.Subject = title;
mail.Body = content;
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = false;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential(this.m_loginName, this.m_password);
smtp.Host = this.m_smtpService;
smtp.EnableSsl = this.m_enableSSL;
smtp.Port = this.m_port;
try
{
smtp.Send(mail);
}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
Console.WriteLine("Delivery failed - retrying in 5 seconds.");
System.Threading.Thread.Sleep(5000);
smtp.Send(mail);
}
else
{
Console.WriteLine("Failed to deliver message to {0}",
ex.InnerExceptions[i].FailedRecipient);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in RetryIfBusy(): {0}",
ex.ToString());
}
}
public class NotificationHandler
{
private static readonly NotificationHandler g_instance = new NotificationHandler();
public static NotificationHandler Instance
{
get
{
return g_instance;
}
}
public NotificationHandler()
{
this.m_lockObject = new object();
this.m_mailNotifyInfos = new LinkedList<string>();
this.m_threadEvent = new ManualResetEvent(false);
this.m_workThread = new Thread(this.ThreadStart);
this.m_workThread.Start();
}
private readonly LinkedList<string> m_mailNotifyInfos;
private readonly Thread m_workThread;
private readonly ManualResetEvent m_threadEvent;
private readonly Object m_lockObject;
/// <summary>
///
/// </summary>
/// <param name="mailNotifyInfo"></param>
public void AppendNotification(string mailNotifyInfo)
{
lock (this.m_lockObject)
{
this.m_mailNotifyInfos.AddLast(mailNotifyInfo);
if (this.m_mailNotifyInfos.Count != 0)
{
this.m_threadEvent.Set();
}
}
}
/// <summary>
/// 发送邮件线程的执行方法
/// </summary>
public void ThreadStart()
{
while(true)
{
this.m_threadEvent.WaitOne();
string mailNotifyInfo = this.m_mailNotifyInfos.First.Value;
EmailNotificationService mailNotificationService = new EmailNotificationService("smtp.163.com",true,587, "loginName@gmail.com", "loginPassword");
mailNotificationService.SendTo("稿件中心", "halo_maxs@hotmail.com", "稿件状态变更通知", string.Format("{0}你的稿件{1}状态已变更为{2}", "你的email", "测试邮件发送", "等待发送"));
lock(this.m_lockObject)
{
this.m_mailNotifyInfos.Remove(mailNotifyInfo);
if (this.m_mailNotifyInfos.Count == 0)
{
this.m_threadEvent.Reset();
}
}
}
}
protected void sendButton_Click(object sender, EventArgs e)// 触发事件
{
NotificationHandler.Instance.AppendNotification("woowoowow");
}
老是报错 + InnerException {"由于目标计算机积极拒绝,无法连接。 220.181.12.18:587"} System.Exception {System.Net.Sockets.SocketException}
请问是什么原因 --------------------编程问答-------------------- 由于目标计算机积极拒绝,无法连接
说明220.181.12.18不在线,或者587端口被阻滞了。总之就是客户端访问不到220.181.12.18:587 --------------------编程问答-------------------- 1.看看服务器端监听有没有绑定IP地址
2.看看服务器端是否开了什么防火墙,或客户端有没有开什么防火墙。 --------------------编程问答-------------------- 防火墙 我都关了.. 服务器端监听有怎样绑定IP地址
补充:.NET技术 , C#