public EmailInfo(EmailInfo mInfo) { MailTo = mInfo.MailTo; Content = mInfo.Content; Subject = mInfo.Subject; MailFrom = mInfo.MailFrom; }
/// <summary> /// 发送邮件 /// </summary> /// <param name="SchedulerItem"></param> /// <param name="Attachment"></param> /// <returns></returns> public void SendEmail(DNNGo_PowerForms_Scheduler SchedulerItem, String Attachment, Int32 index, ModuleInfo mItem) { EmailInfo eInfo = new EmailInfo(); eInfo.Settings = new ModuleController().GetModule(mItem.ModuleID).ModuleSettings; //eInfo.SMTPEnableSSL = SMTPEnableSSL(mItem.PortalID); eInfo.PushSettings(); eInfo.Attachments = Attachment; eInfo.MailTo = SchedulerItem.SenderEmail; eInfo.Content = eInfo.Subject = String.Format("PowerForms Excel. Module Title:{0} , time:{1}", mItem.ModuleTitle, DateTime.Now.ToString()); try { String status = NetHelper.SendMail(eInfo); //this.ScheduleHistoryItem.AddLogNote(String.Format("SMTPServer:{0}; SMTPUsername:{1}; EnableSMTPSSL:{2}; status:{3};<br />", Host.SMTPServer, Host.SMTPUsername, Host.EnableSMTPSSL, status)); this.ScheduleHistoryItem.AddLogNote(String.Format("task {0},Send mail to completed. time:{1}<br />", index, DateTime.Now.ToString())); } catch (Exception exc) { this.ScheduleHistoryItem.AddLogNote(exc.Source); } //MailScheduler.AssignMessage(eInfo); }
/// <summary> /// 任务循环处理函数 /// </summary> private void WorkerHandler() { this.working = true; while (this.working) { while (this.queueMessage.Count > 0) { try { EmailInfo mail = this.queueMessage.Dequeue(); NetHelper.SendMail(mail);//发送邮件程序 } catch (Exception ex) { } finally { Thread.Sleep(50); } } Thread.Sleep(1000); } }
/// <summary> /// 发送邮件 /// </summary> /// <param name="mailInfo"></param> /// <returns></returns> public static string SendMail(EmailInfo mailInfo) { Hashtable Settings = mailInfo.Settings; //string SMTPServer = null; //SMTPServer = (string)Globals.HostSettings["SMTPServer"].ToString(); //string SMTPAuthentication = null; //SMTPAuthentication = (string)Globals.HostSettings["SMTPAuthentication"].ToString(); //string SMTPUsername = null; //SMTPUsername = (string)Globals.HostSettings["SMTPUsername"].ToString(); //string SMTPPassword = null; //SMTPPassword = (string)Globals.HostSettings["SMTPPassword"].ToString(); Boolean SMTPEnableSSL = mailInfo.SMTPEnableSSL; string HostEmail = Settings["PowerForms_SenderEmail"] != null && Mail.IsValidEmailAddress(Convert.ToString(Settings["PowerForms_SenderEmail"]), Null.NullInteger) ? Convert.ToString(Settings["PowerForms_SenderEmail"]) : Host.HostEmail; String MailFrom = String.IsNullOrEmpty(mailInfo.MailFrom) ? HostEmail : mailInfo.MailFrom; string status = null; if (!String.IsNullOrEmpty(mailInfo.MailTo))//if (!String.IsNullOrEmpty(mailInfo.MailTo) && Mail.IsValidEmailAddress(mailInfo.MailTo, Null.NullInteger)) { if (!String.IsNullOrEmpty(mailInfo.ReplyTo)) { /** 因为要加入ReplyTo,故只有一个参数可以使用,这里需要多测试了 2015.12.21 **/ List <System.Net.Mail.Attachment> AttachmentList = new List <System.Net.Mail.Attachment>(); if (!String.IsNullOrEmpty(mailInfo.Attachments)) { foreach (String filename in WebHelper.GetList(mailInfo.Attachments)) { AttachmentList.Add(new Attachment(filename)); } } status = Mail.SendMail(MailFrom, mailInfo.MailTo, "", "", mailInfo.ReplyTo, DotNetNuke.Services.Mail.MailPriority.Normal, mailInfo.Subject, MailFormat.Html, Encoding.UTF8, mailInfo.Content, AttachmentList, mailInfo.SMTPServer, mailInfo.SMTPAuthentication, mailInfo.SMTPUsername, mailInfo.SMTPPassword, SMTPEnableSSL); } else { if (SMTPEnableSSL) { if (!String.IsNullOrEmpty(mailInfo.Attachments)) { status = Mail.SendMail(MailFrom, mailInfo.MailTo, "", "", DotNetNuke.Services.Mail.MailPriority.Normal, mailInfo.Subject, MailFormat.Html, Encoding.UTF8, mailInfo.Content, mailInfo.Attachments, mailInfo.SMTPServer, mailInfo.SMTPAuthentication, mailInfo.SMTPUsername, mailInfo.SMTPPassword, SMTPEnableSSL); } else { status = Mail.SendMail(MailFrom, mailInfo.MailTo, "", "", "", DotNetNuke.Services.Mail.MailPriority.Normal, mailInfo.Subject, MailFormat.Html, Encoding.UTF8, mailInfo.Content, new List <System.Net.Mail.Attachment>(), mailInfo.SMTPServer, mailInfo.SMTPAuthentication, mailInfo.SMTPUsername, mailInfo.SMTPPassword, SMTPEnableSSL); } } else { if (!String.IsNullOrEmpty(mailInfo.Attachments)) { status = Mail.SendMail(MailFrom, mailInfo.MailTo, "", mailInfo.Subject, mailInfo.Content, mailInfo.Attachments, "html", mailInfo.SMTPServer, mailInfo.SMTPAuthentication, mailInfo.SMTPUsername, mailInfo.SMTPPassword); } else { status = Mail.SendMail(MailFrom, mailInfo.MailTo, "", mailInfo.Subject, mailInfo.Content, "", "html", mailInfo.SMTPServer, mailInfo.SMTPAuthentication, mailInfo.SMTPUsername, mailInfo.SMTPPassword); } //statue = Mail.SendMail(MailFrom, mailInfo.MailTo, "", mailInfo.Subject, mailInfo.Content, mailInfo.Attachments, "html", "", "", "", ""); } } } return(status); }
/// <summary> /// 发送邮件 /// </summary> /// <param name="subject"></param> /// <param name="content"></param> /// <param name="toemail"></param> /// <param name="mailFrom"></param> /// <returns></returns> public static string SendMail(string subject, string content, string toemail, string mailFrom, Hashtable settings) { EmailInfo mailInfo = new EmailInfo(toemail, content, subject, mailFrom, settings); return(SendMail(mailInfo)); }
/// <summary> /// 提交成功发送邮件 /// 1.构造管理员邮件并发送 /// 2.构造提交者的邮件并发送 /// </summary> /// <param name="SubmitContent"></param> public void SendMail(DNNGo_PowerForms_Content SubmitContent, List <DNNGo_PowerForms_ContentItem> ContentList, List <DNNGo_PowerForms_Field> FieldList) { DNNGo_PowerForms_Template Template = Settings_EmailTemplate; //if (String.IsNullOrEmpty(SubmitContent.Email) || SubmitContent.Email.IndexOf("Anonymous e-mail", StringComparison.CurrentCultureIgnoreCase) >= 0) //{ // DNNGo_PowerForms_ContentItem ContentItem = ContentList.Find(r1 => r1.FieldName.IndexOf(Settings_SubmitUserEmail, StringComparison.CurrentCultureIgnoreCase) >= 0); // if (ContentItem != null && !String.IsNullOrEmpty(ContentItem.ContentValue)) // { // SubmitContent.Email = ContentItem.ContentValue; // } //} EmailInfo Email = new EmailInfo(); Email.Settings = Settings; Email.PushSettings(this); //1.构造管理员邮件并发送 if (Settings_SendToAdmin && !String.IsNullOrEmpty(Settings_AdminEmail))//发邮件||管理员邮箱不为空 { List <String> SendMailAddress = new List <string>(); //Boolean ReplaceSender = Settings["PowerForms_ReplaceSender"] != null && !string.IsNullOrEmpty(Settings["PowerForms_ReplaceSender"].ToString()) ? Convert.ToBoolean(Settings["PowerForms_ReplaceSender"]) : false; //if (ReplaceSender && !String.IsNullOrEmpty(SubmitContent.Email) && Mail.IsValidEmailAddress(SubmitContent.Email, Null.NullInteger)) //{ // Email.ReplyTo = SubmitContent.Email;//替换发件人地址为提交用户 //} /** 因为要加入ReplyTo,这个需要记录下,屏蔽掉之前的代码 2015.12.21 **/ String EmailReplyTo = String.Empty; Boolean ReplyTo = Settings["PowerForms_ReplyTo"] != null?Convert.ToBoolean(Settings["PowerForms_ReplyTo"]) : true; if (ReplyTo && !String.IsNullOrEmpty(SubmitContent.Email) && Mail.IsValidEmailAddress(SubmitContent.Email, Null.NullInteger)) { EmailReplyTo = SubmitContent.Email;//替换发件人地址为提交用户 } //添加管理员邮件到代发列表 if (!String.IsNullOrEmpty(Settings_AdminEmail)) { if (Settings_AdminEmail.IndexOf(";") >= 0) { List <String> adminMailTos = WebHelper.GetList(Settings_AdminEmail, ";"); foreach (var adminMailTo in adminMailTos) { if (!String.IsNullOrEmpty(adminMailTo) && Mail.IsValidEmailAddress(adminMailTo, Null.NullInteger)) { SendMailAddress.Add(adminMailTo); } } } else { SendMailAddress.Add(Settings_AdminEmail); } } //检索勾选的待发送角色 String AdminEmailRoles = Settings["PowerForms_AdminEmailRoles"] != null?Convert.ToString(Settings["PowerForms_AdminEmailRoles"]) : ""; if (!String.IsNullOrEmpty(AdminEmailRoles)) { List <String> RoleNames = Common.GetList(AdminEmailRoles); if (RoleNames != null && RoleNames.Count > 0) { foreach (var RoleName in RoleNames) { if (!String.IsNullOrEmpty(RoleName)) { //找出角色相关的用户信息 DotNetNuke.Security.Roles.RoleController roleController = new DotNetNuke.Security.Roles.RoleController(); ArrayList users = roleController.GetUsersByRoleName(PortalId, RoleName); if (users != null && users.Count > 0) { foreach (UserInfo user in users) { //判断邮件地址是否符合 if (!String.IsNullOrEmpty(user.Email) && Mail.IsValidEmailAddress(user.Email, Null.NullInteger) && !(SendMailAddress.IndexOf(user.Email) >= 0)) { SendMailAddress.Add(user.Email); } } } } } } } //发送给所有的管理用户 foreach (var SendMail in SendMailAddress) { Email = new EmailInfo(); Email.Settings = Settings; Email.PushSettings(this); if (!String.IsNullOrEmpty(EmailReplyTo)) { Email.ReplyTo = EmailReplyTo; } //构造邮件的主题和内容 Email.Subject = FormatContent(SubmitContent, Template.ReceiversSubject, ContentList); Email.Content = FormatContent(SubmitContent, Template.ReceiversTemplate, ContentList); Email.MailTo = SendMail; MailScheduler.AssignMessage(Email);//加到待发队列 } } //2.构造提交者的邮件并发送 if (Settings_SendToSubmitUser) { if (!String.IsNullOrEmpty(SubmitContent.Email) && SubmitContent.Email.IndexOf("Anonymous e-mail", StringComparison.CurrentCultureIgnoreCase) < 0) { Email = new EmailInfo(); Email.Settings = Settings; Email.PushSettings(this); Email.MailTo = SubmitContent.Email; //构造邮件的主题和内容 Email.Subject = FormatContent(SubmitContent, Template.ReplySubject, ContentList); Email.Content = FormatContent(SubmitContent, Template.ReplyTemplate, ContentList); MailScheduler.AssignMessage(Email);//加到待发队列 //NetHelper.SendMail(Email); } } //3.检查有无下拉列表发邮件的字段 foreach (DNNGo_PowerForms_Field fieldItem in FieldList) { if (fieldItem.FieldType == (Int32)EnumViewControlType.DropDownList_SendEmail) { DNNGo_PowerForms_ContentItem contentItem = ContentList.Find(r1 => r1.FieldName == fieldItem.Name); if (contentItem != null && !String.IsNullOrEmpty(contentItem.FieldName)) { Email = new EmailInfo(); Email.Settings = Settings; Email.PushSettings(this); Email.MailTo = contentItem.ContentValue; //构造邮件的主题和内容 Email.Subject = FormatContent(SubmitContent, Template.ReceiversSubject, ContentList); Email.Content = FormatContent(SubmitContent, Template.ReceiversTemplate, ContentList); MailScheduler.AssignMessage(Email);//加到待发队列 //NetHelper.SendMail(Email); } } } }
/// <summary> /// 指派一个新的邮件任务 /// </summary> /// <param name="task"></param> /// <returns></returns> public int AssignMessage(EmailInfo message) { this.Start(); this.queueMessage.Enqueue(message); return(this.Count); }
/// <summary> /// 向任务处理器指派新的邮件发送任务 /// </summary> /// <param name="message"></param> /// <returns></returns> public static int AssignMessage(EmailInfo message) { return(worker.AssignMessage(message)); }