- A+
所属分类:.NET技术
1. 实现功能:
通过程序自动发送邮件。使用QQ邮箱(个人/企业)或阿里云邮箱(企业)。
2. 编码过程中的尝试结果:
2.1 测试配置:见3.1 app.config配置
2.2 测试结果
3. 代码实现
3.1 app.config配置
3.1.1 QQ邮箱配置
1 <connectionStrings> 2 <add name="MailServerIP" connectionString="smtp.exmail.qq.com" /> 3 <add name="MailServerPort" connectionString="465" /> 4 <add name="fromMailAddress" connectionString="xxx@yyy.com" /> 5 <add name="toMailAddress" connectionString="xxx2@yyy.com"/> 6 <add name="mailUsername" connectionString="usernamexxx" /> 7 <add name="mailPassword" connectionString="passwordxxx" /> 8 <add name="ccMailAddress" connectionString="usercc@yyy.com" /> 9 </connectionStrings>
注:若是QQ个人邮箱,则mailPassword需配置成授权码
3.1.2 阿里云邮箱配置
1 <connectionStrings> 2 <add name="MailServerIP" connectionString="smtp.qiye.aliyun.com" /> 3 <add name="MailServerPort" connectionString="25" /> 4 <add name="fromMailAddress" connectionString="xxx@yyy.com" /> 5 <add name="toMailAddress" connectionString="xxx1@yyy.com"/> 6 <add name="mailUsername" connectionString="usernamexxx" /> 7 <add name="mailPassword" connectionString="passwordxxx" /> 8 <add name="ccMailAddress" connectionString="usercc@yyy.com" /> 9 </connectionStrings>
3.2 调用
1 public void EventOccured(string EventID) 2 { 3 string subjectInfo = "一些要发送邮件的事件发生咯"; 4 string bodyInfo = "<p style ="font-size: 10pt">Hi,all</p>"; 5 bodyInfo += "<p style ="font-size: 10pt;color:red">事件:" + EventID + "已经发生,请注意监控。</p>"; 6 bodyInfo += "<p style ="font-size: 10pt">以上内容为系统自动发送,请勿直接回复,谢谢。</p>"; 7 8 SendMailHelper.Sendmail_(subjectInfo, bodyInfo); 9 }
1 public static void Sendmail_(string subjectInfo, string bodyInfo) 2 { 3 try 4 { 5 string senderServerIp = ConfigurationManager.ConnectionStrings["MailServerIP"].ConnectionString; 6 string toMailAddress = ConfigurationManager.ConnectionStrings["toMailAddress"].ConnectionString; 7 string fromMailAddress = ConfigurationManager.ConnectionStrings["fromMailAddress"].ConnectionString; 8 string mailUsername = ConfigurationManager.ConnectionStrings["mailUsername"].ConnectionString; 9 string mailPassword = ConfigurationManager.ConnectionStrings["mailPassword"].ConnectionString; 10 string mailPort = ConfigurationManager.ConnectionStrings["MailServerPort"].ConnectionString; 11 string cc = ConfigurationManager.ConnectionStrings["ccMailAddress"].ConnectionString; 12 13 MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, cc, true, false); //注: aliyun需将最后参数改为:false,true 14 email.Send(); 15 } 16 catch (Exception ex) 17 { 18 Console.WriteLine(ex.ToString()); 19 } 20 }
3.3 具体实现
3.3.1 QQ邮箱:使用System.Web.Mail
1 private System.Web.Mail.MailMessage mMessage; 2 3 public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port,string cc, bool sslEnable, bool pwdCheckEnable) 4 { 5 try 6 { 7 //for System.Web.Mail: QQ MAIL 8 mMessage = new System.Web.Mail.MailMessage(); 9 mMessage.Priority = System.Web.Mail.MailPriority.Normal; 10 mMessage.From = fromMail; 11 mMessage.To = toMail; 12 mMessage.Cc = cc; 13 mMessage.Subject = subject; 14 mMessage.BodyFormat = System.Web.Mail.MailFormat.Html; 15 mMessage.BodyEncoding = Encoding.UTF8; 16 mMessage.Body = emailBody; 17 mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication 18 mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", fromMail); //set your username here 19 mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); //set your password here 20 mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);//set port 21 mMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", sslEnable);//set is ssl 22 System.Web.Mail.SmtpMail.SmtpServer = server; 23 } 24 catch (Exception ex) 25 { 26 } 27 }
1 public void Send() 2 { 3 try 4 { 5 //for System.Web.Mail 6 if (mMessage != null) 7 { 8 System.Web.Mail.SmtpMail.Send(mMessage); 9 } 10 } 11 catch (Exception ex) 12 { 13 Console.WriteLine(ex.ToString()); 14 } 15 }
3.3.2 阿里云邮箱:使用System.Net.Mail
1 private MailMessage mMailMessage; 2 private SmtpClient mSmtpClient; 3 private int mSenderPort; 4 private string mSenderServerHost; 5 private string mSenderPassword; 6 private string mSenderUsername; 7 private bool mEnableSsl; 8 private bool mEnablePwdAuthentication; 9 10 public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port,string cc, bool sslEnable, bool pwdCheckEnable) 11 { 12 try 13 { 14 //for System.Net.Mail: Aliyun 15 mMailMessage = new MailMessage(); 16 mMailMessage.To.Add(toMail); 17 mMailMessage.From = new MailAddress(fromMail); 18 mMailMessage.Subject = subject; 19 mMailMessage.Body = emailBody; 20 mMailMessage.IsBodyHtml = true; 21 mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; 22 mMailMessage.Priority = MailPriority.Normal; 23 if (!string.IsNullOrEmpty(cc)) 24 mMailMessage.CC.Add(cc); 25 this.mSenderServerHost = server; 26 this.mSenderUsername = fromMail; 27 this.mSenderPassword = password; 28 this.mSenderPort = Convert.ToInt32(port); 29 this.mEnableSsl = sslEnable; 30 this.mEnablePwdAuthentication = pwdCheckEnable; 31 } 32 catch (Exception ex) 33 { 34 Console.WriteLine(ex.ToString()); 35 } 36 }
1 public void Send() 2 { 3 try 4 { 5 //for System.Net.Mail 6 if (mMailMessage != null) 7 { 8 mSmtpClient = new SmtpClient(); 9 //mSmtpClient.Host = "smtp." + mMailMessage.From.Host; 10 mSmtpClient.Host = this.mSenderServerHost; 11 mSmtpClient.Port = this.mSenderPort; 12 mSmtpClient.UseDefaultCredentials = false; 13 mSmtpClient.EnableSsl = this.mEnableSsl; 14 if (this.mEnablePwdAuthentication) 15 { 16 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); 17 //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); 18 //NTLM: Secure Password Authentication in Microsoft Outlook Express 19 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM"); 20 } 21 else 22 { 23 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); 24 } 25 mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 26 mSmtpClient.Send(mMailMessage); 27 } 28 } 29 catch (Exception ex) 30 { 31 Console.WriteLine(ex.ToString()); 32 } 33 }