标签:host context ddr 匿名 下一步 reac count win 中继
1 //1. 新建EmailHelper 类 2 using System; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.Linq; 6 using System.Net.Mail; 7 using System.Web; 8 9 namespace WebAPI.Common 10 { 11 public class EmailHelper 12 { 13 private MailMessage msg; 14 private string[] emailfrom; 15 private string[] emailto; 16 private string[] emailcc; 17 private string[] emailbcc; 18 private string emailsubject; 19 private System.Text.StringBuilder emailcontent; 20 public EmailHelper() 21 { 22 msg = new MailMessage(); 23 Emailpara email = System.Configuration.ConfigurationManager.GetSection("email") as Emailpara; 24 25 emailfrom = email.emailfrom.Split(‘;‘); 26 emailto = email.emailto.Split(‘;‘); 27 emailcc = email.emailcc.Split(‘;‘); 28 emailbcc = email.emailbcc.Split(‘;‘); 29 emailsubject = email.emailsubject; 30 31 emailcontent = new System.Text.StringBuilder(); 32 33 //emailcontent.Append("<Table class=\"gridtable\">"); 34 //emailcontent.Append("<tr>"); 35 //emailcontent.Append("<th>Req_ID</th>"); 36 //emailcontent.Append("<th>Requester</th>"); 37 //emailcontent.Append("<th>Country</th>"); 38 //emailcontent.Append("<th>Env</th>"); 39 //emailcontent.Append("</tr>"); 40 } 41 public void setContent(string str) 42 { 43 //ErrorList.AddLast(request); 44 //emailcontent.Append("<tr>"); 45 //emailcontent.Append("<td>" + request.Req_ID + "</td>"); 46 //emailcontent.Append("<td>" + request.Requester + "</td>"); 47 //emailcontent.Append("<td>" + request.Country +"</td>"); 48 //emailcontent.Append("<td>" + request.Env +"</td>"); 49 //emailcontent.Append("</tr>"); 50 51 emailcontent.Append(str); 52 53 emailcontent.Append("<br />"); 54 } 55 56 public void Send() 57 { 58 // emailcontent.Append("</Table>"); 59 //set emailTo 60 foreach (string tostr in emailto) 61 { 62 msg.To.Add(tostr); 63 } 65 //set emailCC 66 if (emailcc.Length > 0 && !String.Empty.Equals(emailcc[0])) 67 { 68 foreach (string ccstr in emailcc) 69 { 70 msg.CC.Add(ccstr); 71 } 72 } 73 //set emailBcc 74 if (emailbcc.Length > 0 && !String.Empty.Equals(emailbcc[0])) 75 { 76 77 foreach (string bccstr in emailbcc) 78 { 79 msg.Bcc.Add(bccstr); 80 } 81 } 82 // set emailFrom 83 if (emailfrom.Length > 0 && !String.Empty.Equals(emailfrom)) 84 msg.From = new MailAddress(emailfrom[0]); 85 else 86 msg.From = new MailAddress("DO.NOT.REPLY.TO@Test.COM"); 87 88 msg.Subject = emailsubject; 89 msg.SubjectEncoding = System.Text.Encoding.UTF8; 90 91 msg.BodyEncoding = System.Text.Encoding.UTF8; 92 msg.IsBodyHtml = true; 93 msg.Priority = MailPriority.Normal; 94 string content = "<!DOCTYPE html><html><head><style type=\"text/css\">table.gridtable {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;}"; 95 content = content + "table.gridtable th {border-width: 1px;padding: 8px; border-style: solid;border-color: #666666;background-color: #dedede;}table.gridtable td {border-width: 1px; padding: 8px;border-style: solid;border-color: #666666; background-color: #ffffff;}</style></head>"; 96 content = content + "<body><span style=\"font-family:Arial;font-size:16px;color:blue\"><br />An error has occurred <b></b> <br /><br />Following error happened. <br /> Details as below . <br /><br /></span>"; 97 content = content + emailcontent.ToString(); 98 content = content + "<hr width=50% align=left /><span style=\"font-family:Arial;font-size:16px;color:blue\">Team Support <br /></span></body></html>"; 99 100 msg.Body = content; 101 msg.BodyEncoding = System.Text.Encoding.UTF8; 102 SmtpClient client = new SmtpClient(); 103 client.Host = "localhost"; // important thing, 104 try 105 { 106 client.Send(msg); 107 } 108 catch (Exception ex) 109 { 110 111 } 113 } 115 } 116 public class EmailSectionHandler : IConfigurationSectionHandler 117 { 118 public object Create(object parent, object configContext, System.Xml.XmlNode section) 119 { 120 string emailfrom = section.SelectSingleNode("EmailFrom").InnerText; 121 string emailto = section.SelectSingleNode("EmailTo").InnerText; 122 string emailcc = section.SelectSingleNode("EmailCC").InnerText; 123 string emailbcc = section.SelectSingleNode("EmailBCC").InnerText; 124 string emailsubject = section.SelectSingleNode("EmailSubject").InnerText; 125 Emailpara email = new Emailpara(); 126 email.emailfrom = emailfrom; 127 email.emailto = emailto; 128 email.emailcc = emailcc; 129 email.emailbcc = emailbcc; 130 email.emailsubject = emailsubject; 131 132 return email; 133 } 134 } 135 136 public class Emailpara 137 { 138 public string emailfrom { get; set; } 139 public string emailto { get; set; } 140 public string emailcc { get; set; } 141 public string emailbcc { get; set; } 142 public string emailsubject { get; set; } 143 144 } 145 } 146 147 //2。配置Web.config 148 149 <configuration> 150 <configSections> 151 <section name ="email" type ="WebAPI.Common.EmailSectionHandler" /> 152 </configSections> 153 154 <email> 155 <EmailFrom>DO.NOT.REPLY.TO@Test.COM</EmailFrom> 156 <EmailTo>Email Address</EmailTo> 157 <EmailCC></EmailCC> 158 <EmailBCC></EmailBCC> 159 <EmailSubject>ERROR Message</EmailSubject> 160 </email> 161 </configuration> 162 163 //3。调用EmailHelper,并发送相关信息 164 EmailHelper email = new EmailHelper(); 165 email.setContent("ErrorMessage"); 166 email.Send();
// 以上内容在目前项目SMTP 服务器开启的情况下,可以正常发送邮件
//4. 配置server 端的SMTP(以下内容:转载,未经自己测试)
以下是安装 SMTP 服务器功能的步骤:
以下是使用 IIS 6.0 管理器配置 SMTP 虚拟服务器的步骤:
标签:host context ddr 匿名 下一步 reac count win 中继
原文地址:https://www.cnblogs.com/allenzhang/p/11104070.html