/// <summary> /// 发送邮件 /// </summary> /// <param name="entity">邮件实体</param> /// <param name="setting">服务器设置</param> /// <returns>发送成功返回True</returns> public override bool Send(MailMessage entity, EmailSetting setting) { try { var smtpClient = ConfigClient(entity, setting); smtpClient.Send(entity); return(true); } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取协议对象 /// </summary> /// <param name="entity">邮件实体</param> /// <param name="setting">服务器设置</param> protected SmtpClient ConfigClient(MailMessage entity, EmailSetting setting) { var smtpClient = new SmtpClient(); smtpClient.Host = setting.SmtpServer; smtpClient.Port = setting.Port; if (setting.IsAuthenticationRequired) { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(setting.AuthenticationUserName, setting.AuthenticationPassword); } smtpClient.Timeout = setting.Timeout; smtpClient.EnableSsl = setting.EnableSsl; return(smtpClient); }
/// <summary> /// 异步发送邮件 /// </summary> /// <param name="entity">邮件实体</param> /// <param name="setting">服务器设置</param> /// <param name="userToken">一个用户定义对象,此对象将被传递给完成异步操作时所调用的方法。</param> /// <param name="sendCompletedCallback">在异步电子邮件发送操作完成时回调</param> public override void SendAsync(MailMessage entity, EmailSetting setting, object userToken, Action <AsyncCompletedEventArgs> sendCompletedCallback) { try { var smtpClient = ConfigClient(entity, setting); SendCompletedEventHandler completedCallback = (o, e) => { if (sendCompletedCallback != null) { sendCompletedCallback(e); } }; smtpClient.SendCompleted += completedCallback; smtpClient.SendAsync(entity, userToken); } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步发送邮件 /// </summary> /// <param name="entity">邮件实体</param> /// <param name="setting">服务器设置</param> /// <param name="userToken">一个用户定义对象,此对象将被传递给完成异步操作时所调用的方法</param> /// <param name="sendCompletedCallback">在异步电子邮件发送操作完成时回调</param> public abstract void SendAsync(MailMessage entity, EmailSetting setting, object userToken, Action <AsyncCompletedEventArgs> sendCompletedCallback);
/// <summary> /// 发送邮件 /// </summary> /// <param name="entity">邮件实体</param> /// <param name="setting">服务器设置</param> /// <returns>发送成功返回True</returns> public abstract bool Send(MailMessage entity, EmailSetting setting);