private static IAsyncMail SmtpClientSendAsync(MailConfig mailConfig, MailMessage message) { var asyncHandle = new AsyncMail(); var sender = new SmtpClient(); sender.UseDefaultCredentials = false; sender.SendCompleted += asyncHandle.Complete; sender.DeliveryMethod = SmtpDeliveryMethod.Network; try { message.IsBodyHtml = mailConfig.IsHtml; message.From = new MailAddress(mailConfig.From, mailConfig.DisplayName); sender.Host = mailConfig.Host; sender.Port = mailConfig.Port; sender.Credentials = new NetworkCredential(mailConfig.User, mailConfig.Password); sender.EnableSsl = mailConfig.EnableSsl; sender.SendAsync(message, asyncHandle); } catch (Exception e) { var defultConfig = MailConfig.Defult; //发送异常一般为发送邮箱配置有误,这里提供一个默认发件邮箱。 message.From = new MailAddress(defultConfig.From, defultConfig.DisplayName); // "*****@*****.**", "Mafly" message.IsBodyHtml = true; sender.Host = defultConfig.Host; //"smtp.163.com"; sender.Port = defultConfig.Port; // 25; sender.Credentials = new NetworkCredential(defultConfig.User, defultConfig.Password); // "*****@*****.**", "vzihlbquwnriqlht" sender.EnableSsl = false; message.Body += e.ToString(); sender.SendAsync(message, asyncHandle); } return(asyncHandle); }
public IAsyncMail SendAsync(string body) { var mail = new AsyncMail(); ForEachInfo((info) => { info.Body = body; var asyn = MailUtility.SendAsync(mailConfig, info); mail.RecordSubMail(asyn); asyn.onComplete = mail.OnSubMailComplete; }); return(mail); }