日期:2014-05-17 浏览次数:20466 次
using System; using System.Data; using System.Configuration; using System.Linq; using System.Xml.Linq; using System.Net; using System.Net.Mail; /// <summary> ///Sendmail 的摘要说明 /// </summary> public class Sendmail { public Sendmail() { // //TODO: 在此处添加构造函数逻辑 // } private static string account = "acc"; private static string password = "pwd"; private static string smtpserver = "smtp.163.com"; private static int smtpport = 25; private static string status = "status"; private static string _Msg; /// <summary> /// 发送邮件 /// </summary> public static int SendMail(string MailFrom, string MailTo,string task_id, string Subject, string Content) { int flag = 1; if (MailFrom != null && MailFrom.Trim() != "" && MailTo != null && MailTo.Trim() != "" && Subject != null && Subject.Trim() != "") { try { _Msg = ""; SmtpClient smtpClient = new SmtpClient(smtpserver, smtpport); smtpClient.Credentials = new System.Net.NetworkCredential(account, password); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.EnableSsl = false; smtpClient.Timeout = 5 * 1000; MailMessage MyEmail = new MailMessage(MailFrom, MailTo, Subject, Content); //MyEmail.From = MailFrom; //MyEmail.To = MailTo; //MyEmail.Subject = Subject; //MyEmail.Body = Content; MyEmail.IsBodyHtml = true; MyEmail.Priority = MailPriority.Normal; smtpClient.SendCompleted += new SendCompletedEventHandler(client_SendCompleted); smtpClient.SendAsync(MyEmail,task_id); if (!_Msg.Equals("true"))//发送成功 { flag = 1; } } catch (Exception er) { flag = 0;//發送失敗 } } else { flag = 0;//发送失败 } return flag; } public static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { String token = (string)e.UserState; if (e.Cancelled) { _Msg = token + " Send canceled."; } if (e.Error != null) { _Msg = token + " " + e.Error.ToString() + " "; } else { _Msg = ("true"); } //IEnquiriesService enquiriesService = new EnquiriesServiceImpl(); //enquiriesService.saveRemarkById(_Msg, _Enqid); //if (NotifyCaller != null) NotifyCaller(mailStatus, EventArgs.Empty); } }