使用Net.Mail、CDO组件、JMail组件三种方式发送邮件

简介: 原文:使用Net.Mail、CDO组件、JMail组件三种方式发送邮件一、使用Net.Mail          需要服务器认证,大部分服务器端口为25. View Code 1 /// 2 /// 用MailMessage通过需要认证的SMTP服务器发送邮件,可以发送附件 3 /// 4 /// 发件箱地址,例:myaccount@163.
原文: 使用Net.Mail、CDO组件、JMail组件三种方式发送邮件

一、使用Net.Mail

         需要服务器认证,大部分服务器端口为25.

View Code
 1         /// <summary>
2 /// 用MailMessage通过需要认证的SMTP服务器发送邮件,可以发送附件
3 /// </summary>
4 /// <param name="frmAddress">发件箱地址,例:myaccount@163.com</param>
5 /// <param name="password">发件箱登录密码</param>
6 /// <param name="toAddress">收件箱地址,多个地址使用";"隔开,例:youraccount@sina.com</param>
7 /// <param name="copyTo">抄送地址,多个地址使用";"隔开,例:hisaccount@QQ.com</param>
8 /// <param name="mailSubject">邮件主题,例:MailTest</param>
9 /// <param name="mailContent">邮件内容,例:Hello</param>
10 /// <param name="mailserver">发件箱所在的SMTP服务器,例:smtp.163.com</param>
11 public void NetSendMail(string frmAddress, string password, string toAddress, string copyTo, string mailSubject, string mailContent, string mailserver)
12 {
13 ///添加发件人地址
14 MailMessage mailMsg = new MailMessage();
15 mailMsg.From = new MailAddress(frmAddress);
16 ///添加收件人地址
17 string split = ";";
18 string[] toList = toAddress.Trim().Split(split.ToCharArray());
19 for (int i = 0; i < toList.Length; i++)
20 {
21 mailMsg.To.Add(toList[i].Trim());
22 }
23
24 ///添加抄送地址
25 string[] ccList = copyTo.Trim().Split(split.ToCharArray());
26 for (int i = 0; i < ccList.Length; i++)
27 {
28 if (ccList[i].Trim().Length > 0)
29 {
30 mailMsg.CC.Add(ccList[i].Trim());
31 }
32 }
33
34 ///添加邮件主题
35 mailMsg.Subject = mailSubject.Trim();
36 mailMsg.SubjectEncoding = Encoding.UTF8;
37
38 ///添加邮件内容
39 mailMsg.Body = mailContent;
40 mailMsg.BodyEncoding = Encoding.UTF8;
41 mailMsg.IsBodyHtml = true; //正文是否为html样式
42
43 ///添加邮件附件
44 HttpFileCollection fileList = HttpContext.Current.Request.Files;
45 for (int i = 0; i < fileList.Count; i++)
46 { ///添加单个附件
47 HttpPostedFile file = fileList[i];
48 if (file.FileName.Length <= 0 || file.ContentLength <= 0)
49 {
50 break;
51 }
52 string path = Server.MapPath("~/FileUpload/"); //附件保存在程序所在的目录FileUpload下
53 string name = System.IO.Path.GetFileName(file.FileName);
54 file.SaveAs(path + name);
55 mailMsg.Attachments.Add(new System.Net.Mail.Attachment(file.FileName));
56 }
57 try
58 {
59 //实例化SmtpClient邮件发送类对象
60 SmtpClient client = new SmtpClient(mailserver, 25); //大部分smtp服务器的端口是25
61 //设置用于验证发件人身份的凭据
62 client.Credentials = new System.Net.NetworkCredential(frmAddress, password);
63 //发送邮件
64 client.Send(mailMsg);
65 Response.Write("<script type='text/javascript'>alert('发送成功!')</script>");
66 }
67 catch
68 {
69 Response.Write("<script type='text/javascript'>alert('发送失败')</script>");
70 }
71 }

 

二、使用CDO组件

View Code
 1 /// <summary>
2 /// 用CDO组件通过需要认证的SMTP服务器发送邮件。
3 /// 添加cdosys.dll引用,可以在系统目录(如c:\winnt或c:\windows)的system32子目录中找到它(cdosys.dll)。
4 /// </summary>
5 /// <param name="frmAddress">发件箱地址,例:myaccount@163.com</param>
6 /// <param name="password">发件箱登录密码</param>
7 /// <param name="toAddress">收件箱地址,多个地址使用";"隔开,例:youraccount@sina.com</param>
8 /// <param name="copyTo">抄送地址,多个地址使用";"隔开,例:hisaccount@QQ.com</param>
9 /// <param name="mailSubject">邮件主题,例:MailTest</param>
10 /// <param name="mailContent">邮件内容,例:Hello</param>
11 /// <param name="mailserver">发件箱所在的SMTP服务器,例:smtp.163.com</param>
12 public void CDOSendMail(string frmAddress, string password, string toAddress, string copyTo, string mailSubject, string mailContent, string mailserver)
13 {
14 try
15 {
16 CDO.Message oMsg = new CDO.Message();
17
18 oMsg.From = frmAddress; //添加发件人
19
20 oMsg.To = toAddress; //多人用“;”,“,”分开,自动识别,
21
22 oMsg.CC = copyTo;
23 oMsg.Subject = mailSubject;
24 oMsg.HTMLBody = "<html><body>" + mailContent + "</body></html>";
25 CDO.IConfiguration iConfg = oMsg.Configuration;
26 ADODB.Fields oFields = iConfg.Fields;
27
28 oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
29 oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = frmAddress;
30 oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = toAddress;
31 oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = frmAddress;
32 oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;
33 oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
34 //value=0 代表Anonymous验证方式(不需要验证)
35 //value=1 代表Basic验证方式(使用basic (clear-text) authentication.
36 //The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
37 //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
38 oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
39 oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = mailserver;
40
41 oFields.Update();
42 oMsg.BodyPart.Charset = "gb2312";
43 oMsg.HTMLBodyPart.Charset = "gb2312";
44
45
46 //添加邮件附件
47 HttpFileCollection fileList = HttpContext.Current.Request.Files;
48 for (int i = 0; i < fileList.Count; i++)
49 { ///添加单个附件
50 HttpPostedFile file = fileList[i];
51 if (file.FileName.Length <= 0 || file.ContentLength <= 0)
52 {
53 break;
54 }
55 string path = Server.MapPath("~/FileUpload/"); //附件保存在程序所在的目录FileUpload下
56 string name = System.IO.Path.GetFileName(file.FileName);
57 file.SaveAs(path + name);
58 oMsg.AddAttachment(file.FileName);
59 }
60
61 oMsg.Send();
62 oMsg = null;
63 }
64 catch (Exception e)
65 {
66 throw e;
67 }
68 }

 

三、使用JMail组件

View Code
 1         /// <summary>
2 /// 用JMail组件发送邮件。
3 /// 添加jmail.dll引用
4 /// </summary>
5 /// <param name="frmAddress">发件箱地址,例:myaccount@163.com</param>
6 /// <param name="password">发件箱登录密码</param>
7 /// <param name="toAddress">收件箱地址,多个地址使用";"隔开,例:youraccount@sina.com</param>
8 /// <param name="copyTo">抄送地址,多个地址使用";"隔开,例:hisaccount@QQ.com</param>
9 /// <param name="mailSubject">邮件主题,例:MailTest</param>
10 /// <param name="mailContent">邮件内容,例:Hello</param>
11 /// <param name="mailserver">发件箱所在的SMTP服务器,例:smtp.163.com</param>
12 public bool JMailSendMail(string frmAddress, string password, string toAddress, string copyTo, string mailSubject, string mailContent, string mailserver)
13 {
14 try
15 {
16 MessageClass jmMessage = new MessageClass();
17 jmMessage.Charset = "gb2312";
18 jmMessage.ISOEncodeHeaders = false; //信头编码iso-8859-1字符集
19 jmMessage.Encoding = "base64"; //附件的编码格式
20 //jmMessage.ContentType = "text/html"; //正文类型,去掉,否则正文出现乱码
21
22 jmMessage.MailServerUserName = frmAddress; //发件箱登录名
23 jmMessage.MailServerPassWord = password; //发件箱密码
24
25 jmMessage.From = frmAddress; //发件箱
26
27 jmMessage.Subject = mailSubject;
28 jmMessage.Body = mailContent;
29
30 //回执,当对方阅读了邮件后提醒是否发送回执
31 jmMessage.ReturnReceipt = true;
32 jmMessage.AddNativeHeader("Disposition-Notification-To", frmAddress);//回执接受人的邮件地址
33
34 //收件箱
35 string split = ";";
36 string[] toList = toAddress.Trim().Split(split.ToCharArray());
37 for (int i = 0; i < toList.Length; i++)
38 {
39 jmMessage.AddRecipient(toList[i].Trim(), "", "");
40 }
41
42 //抄送
43 string[] coList = copyTo.Trim().Split(split.ToCharArray());
44 for (int i = 0; i < coList.Length; i++)
45 {
46 jmMessage.AddRecipientCC(coList[i].Trim(), "", "");
47 }
48
49 ///添加邮件附件
50 HttpFileCollection fileList = HttpContext.Current.Request.Files;
51 for (int i = 0; i < fileList.Count; i++)
52 { ///添加单个附件
53 HttpPostedFile file = fileList[i];
54 if (file.FileName.Length <= 0 || file.ContentLength <= 0)
55 {
56 break;
57 }
58 string path = Server.MapPath("~/FileUpload/"); //附件保存在程序所在的目录FileUpload下
59 string name=System.IO.Path.GetFileName(file.FileName);
60 file.SaveAs(path + name);
61 jmMessage.AddAttachment(file.FileName);
62 }
63
64 if (jmMessage.Send(mailserver, false))
65 {
66 return true;
67 }
68 else
69 {
70 return false;
71 }
72 }
73 catch (Exception)
74 {
75
76 throw;
77 }
78 }

 

      对于JMail组件,通常我们遇到的错误是:'The message was undeliverable. All servers failed to receive the message ',这其实是JMAIL返回的错误,并不是ASP代码产生的,根本原因是MAIL SERVER拒绝了JMAIL的请求.

  究其原因,是那些服务器不提供SMTP服务或者没有开启smtp服务;或是在服务器端开启了'禁止邮件中继服务'选项,也就是说不在其允许的IP段或指定范围内的空间里的程序是无法使用其SMTP服务的。解决方案:使用支持smtp的邮件服务器. 使用支持外来jmail申请验证身份,发送邮件的邮件服务器。 最好:使用自己的待遇smtp功能的企业邮局。因为外面的免费的邮局可能会有一些特殊设置,不如防止垃圾邮件,防止盗用邮件身份等等!

Jmail发送首先要通过邮件服务器验证。如果你的服务器不支持SMTP或者你的账号不能使用SMTP服务那么就无法发送。163以前的用户默认是开通POP和SMTP服务的,但新用户都不开通,需要付费才能使用。要想确定某一邮箱是否可以使用POP和SMTP,你可以用foxmail等邮件软件看能否收取该邮箱信件。

目前发现可以通过的stmp服务器有:smtp.qq.com、smtp.163.com,也就是说可以使用该类的邮箱给其他邮箱发送邮件。

目录
相关文章
|
2月前
|
前端开发 C# 数据库
.NET中使用BootstrapBlazor组件库Table实操篇
.NET中使用BootstrapBlazor组件库Table实操篇
|
3月前
|
开发框架 前端开发 .NET
七天.NET 8操作SQLite入门到实战 - (1)第七天BootstrapBlazor UI组件库引入
七天.NET 8操作SQLite入门到实战 - (1)第七天BootstrapBlazor UI组件库引入
|
7月前
|
移动开发 网络协议 NoSQL
.NET Core WebSocket实现简易、高性能、集群即时通讯组件
.NET Core WebSocket实现简易、高性能、集群即时通讯组件
122 0
|
API
.net core工具组件系列之Autofac—— 第二篇:Autofac的3种依赖注入方式(构造函数注入、属性注入和方法注入),以及在过滤器里面实现依赖注入
本篇文章接前一篇,建议可以先看前篇文章,再看本文,会有更好的效果。前一篇跳转链接:https://www.cnblogs.com/weskynet/p/15046999.html
410 0
.net core工具组件系列之Autofac—— 第二篇:Autofac的3种依赖注入方式(构造函数注入、属性注入和方法注入),以及在过滤器里面实现依赖注入
|
15天前
|
搜索推荐 API C#
.NET开源快速、强大、免费的电子表格组件
.NET开源快速、强大、免费的电子表格组件
|
4月前
|
SQL 数据库 开发工具
“.NET视频总结:认识框架的结构和组件,掌握开发工具的奥妙“
“.NET视频总结:认识框架的结构和组件,掌握开发工具的奥妙“
56 0
|
5月前
|
编解码 JSON 算法
一个支持.Net 7的WinForm开源UI组件框架
一个支持.Net 7的WinForm开源UI组件框架
81 0
|
7月前
|
容器
.NET Core - 选项框架:服务组件集成配置的最佳实践
.NET Core - 选项框架:服务组件集成配置的最佳实践
|
9月前
|
开发框架 JavaScript 前端开发
.NET 8新预览版本使用 Blazor 组件进行服务器端呈现
.NET 8新预览版本使用 Blazor 组件进行服务器端呈现
160 0
.NET 8新预览版本使用 Blazor 组件进行服务器端呈现
|
10月前
【vb.net机房收费系统】之没有包含要从继承的组件的已生成程序集
【vb.net机房收费系统】之没有包含要从继承的组件的已生成程序集
25 0