查看: 321|回复: 0

[.NET开发] C# SendMail发送邮件功能实现

发表于 2018-2-9 08:00:00

最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法

  1、利用新浪邮箱发送

  2、利用公司邮箱发送

  3、利用CDO发送,这种方式要引用Interop.ADODB.dll(http://www.nodevice.com/dll/Interop_ADODB_dll/item20357.html)和Interop.CDO.dll()两个文件

具体代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Mail;
  7. using System.Data;
  8. using CDO;
  9. using ADODB;
  10. namespace SendMailTest
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. SendMail();
  17. }
  18. public static string SendMsg()
  19. {
  20. DataTable dt = new DataTable();
  21. dt.Columns.Add("name");
  22. dt.Columns.Add("date");
  23. dt.Columns.Add("area");
  24. dt.Columns.Add("orgnizer");
  25. dt.Columns.Add("keyword");
  26. for (int i = 0; i < 10; i++)
  27. {
  28. DataRow dr = dt.NewRow();
  29. dr["name"] = "北文中心影视产权交易平台?影视项目路演季---路演项目征集" + i;
  30. dr["date"] = "2017-06-30";
  31. dr["area"] = "北京市 北京电影学院文创园(平房园区)" + i;
  32. dr["orgnizer"] = "北文中心影视产权交易" + i;
  33. dr["keyword"] = "影视" + i;
  34. dt.Rows.Add(dr);
  35. }
  36. string MailBody = "<p style=\"font-size: 10pt\">以下内容为系统自动发送,请勿直接回复,谢谢。</p><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">";
  37. MailBody += "<div align=\"center\">";
  38. MailBody += "<tr>";
  39. for (int hcol = 0; hcol < dt.Columns.Count; hcol++)
  40. {
  41. MailBody += "<td bgcolor=\"999999\"> ";
  42. MailBody += dt.Columns[hcol].ColumnName;
  43. MailBody += " </td>";
  44. }
  45. MailBody += "</tr>";
  46. for (int row = 0; row < dt.Rows.Count; row++)
  47. {
  48. MailBody += "<tr>";
  49. for (int col = 0; col < dt.Columns.Count; col++)
  50. {
  51. MailBody += "<td bgcolor=\"dddddd\"> ";
  52. MailBody += dt.Rows[row][col].ToString();
  53. MailBody += " </td>";
  54. }
  55. MailBody += "</tr>";
  56. }
  57. MailBody += "</table>";
  58. MailBody += "</div>";
  59. return MailBody;
  60. }
  61. public static void SendMail()
  62. {
  63. MailMessage msg = new MailMessage();
  64. msg.To.Add("xxx@ctrchina.cn");
  65. msg.CC.Add("xxxx@sina.com");
  66. msg.From = new MailAddress("ffff@ctrchina.cn", "ffff", System.Text.Encoding.UTF8);
  67. /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
  68. msg.Subject = "这是测试邮件";//邮件标题
  69. msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
  70. //msg.Body = "邮件内容";//邮件内容
  71. msg.Body = SendMsg();
  72. msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
  73. msg.IsBodyHtml = true;//是否是HTML邮件
  74. msg.Priority = MailPriority.High;//邮件优先级
  75. SmtpClient client = new SmtpClient();
  76. //client.Host = "smtp.ctrchina.cn";
  77. client.Host = "210.77.136.200";
  78. client.Port = 465;
  79. //client.EnableSsl = true;//经过ssl加密
  80. client.Credentials = new System.Net.NetworkCredential("xxx@ctrchina.cn", "password");
  81. object userState = msg;
  82. try
  83. {
  84. //client.SendAsync(msg, userState);
  85. client.Send(msg);
  86. }
  87. catch (System.Net.Mail.SmtpException ex)
  88. {
  89. return;
  90. }
  91. }
  92. public static void SendSinaMail()
  93. {
  94. MailMessage msg = new MailMessage();
  95. msg.To.Add("xxx@ctrchina.cn");
  96. //msg.To.Add("xxx@sina.com");
  97. msg.CC.Add("xxx@sina.com");
  98. msg.From = new MailAddress("xxx@sina.com", "shao_sks", System.Text.Encoding.UTF8);
  99. /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
  100. msg.Subject = "这是测试邮件";//邮件标题
  101. msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
  102. //msg.Body = "邮件内容";//邮件内容
  103. msg.Body = SendMsg();
  104. msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
  105. msg.IsBodyHtml = true;//是否是HTML邮件
  106. msg.Priority = MailPriority.High;//邮件优先级
  107. SmtpClient client = new SmtpClient();
  108. client.Host = "smtp.sina.com";
  109. client.Port = 25;
  110. //client.EnableSsl = true;//经过ssl加密
  111. client.Credentials = new System.Net.NetworkCredential("username", "password");
  112. object userState = msg;
  113. try
  114. {
  115. //client.SendAsync(msg, userState);
  116. client.Send(msg);
  117. }
  118. catch (System.Net.Mail.SmtpException ex)
  119. {
  120. return;
  121. }
  122. }
  123. public static void SenMail1()
  124. {
  125. try
  126. {
  127. CDO.Message oMsg = new CDO.Message();
  128. Configuration MyConfig = new ConfigurationClass();
  129. Fields MyFields = MyConfig.Fields;
  130. MyFields[@"http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
  131. MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;
  132. MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "210.77.136.200";
  133. MyFields.Update();
  134. oMsg.Configuration = MyConfig;
  135. oMsg.Subject = "Test SMTP2911111";
  136. oMsg.HTMLBody = SendMsg();
  137. oMsg.From = "shaoks@ctrchina.cn";
  138. oMsg.To = "shaoks@ctrchina.cn";
  139. oMsg.Send();
  140. }
  141. catch (Exception ex)
  142. {
  143. return;
  144. }
  145. }
  146. }
  147. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • C#中发送邮件代码
  • C#.NET发送邮件的实例代码
  • c# SendMail发送邮件实例代码
  • c#调用qq邮箱smtp发送邮件修改版代码分享
  • c#实现服务器性能监控并发送邮件保存日志
  • c#使用netmail方式发送邮件示例
  • c#利用webmail邮件系统发送邮件示例分享
  • c#异步发送邮件的类
  • C#实现发送邮件的三种方法
  • C#编程实现发送邮件的方法(可添加附件)


回复

使用道具 举报