一、使用Net.Mail

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

View Code

1 /// 2 ///用MailMessage通过需要认证的SMTP服务器发送邮件,可以发送附件3 /// 4 /// 发件箱地址,例:myaccount@163.com5 /// 发件箱登录密码6 /// 收件箱地址,多个地址使用";"隔开,例:youraccount@sina.com7 /// 抄送地址,多个地址使用";"隔开,例:hisaccount@QQ.com8 /// 邮件主题,例:MailTest9 /// 邮件内容,例:Hello10 /// 发件箱所在的SMTP服务器,例:smtp.163.com11 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服务器的端口是2561 //设置用于验证发件人身份的凭据62 client.Credentials = new System.Net.NetworkCredential(frmAddress, password);

63 //发送邮件64 client.Send(mailMsg);

65 Response.Write("");

66 }

67 catch

68 {

69 Response.Write("");

70 }

71 }

二、使用CDO组件

View Code

1 /// 2 ///用CDO组件通过需要认证的SMTP服务器发送邮件。3 ///添加cdosys.dll引用,可以在系统目录(如c:\winnt或c:\windows)的system32子目录中找到它(cdosys.dll)。4 /// 5 /// 发件箱地址,例:myaccount@163.com6 /// 发件箱登录密码7 /// 收件箱地址,多个地址使用";"隔开,例:youraccount@sina.com8 /// 抄送地址,多个地址使用";"隔开,例:hisaccount@QQ.com9 /// 邮件主题,例:MailTest10 /// 邮件内容,例:Hello11 /// 发件箱所在的SMTP服务器,例:smtp.163.com12 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 = "

" + mailContent + "";

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 /// 2 ///用JMail组件发送邮件。3 ///添加jmail.dll引用4 /// 5 /// 发件箱地址,例:myaccount@163.com6 /// 发件箱登录密码7 /// 收件箱地址,多个地址使用";"隔开,例:youraccount@sina.com8 /// 抄送地址,多个地址使用";"隔开,例:hisaccount@QQ.com9 /// 邮件主题,例:MailTest10 /// 邮件内容,例:Hello11 /// 发件箱所在的SMTP服务器,例:smtp.163.com12 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,也就是说可以使用该类的邮箱给其他邮箱发送邮件。

cdo收取邮件_使用Net.Mail、CDO组件、JMail组件三种方式发送邮件相关推荐

  1. cdo收取邮件_【Excel VBA】- 使用CDO批量发送邮件(二)

    前一期为大家介绍了如何使用VBA结合Outlook批量发送邮件,需要批量为不同的人发送不同的附件等,可以很方便的批量发送.但缺点是需要事先配置Outlook邮箱.那有没有不需要配置Outlook即可批 ...

  2. cdo收取邮件_使用cdo组件发送邮件

    服务的大大小小的网站都有他们自己的邮件服务器,但并非每个都那么慷慨地免费提供给我们的这个小程序使用,Yahoo!不可以,但163的可以,也就是说,为了完成我们这个程序,你应该注册一个163邮件或找到其 ...

  3. cdo收取邮件_利用CDO发邮件 报错怎么处理

    import win.ui; import com; //引用com库 /*DSG{{*/ mainForm = win.form(text="aardio工程11";right= ...

  4. cdo收取邮件_使用CDO发送电子邮件

    使用CDO发送电子邮件 CDO是Collaboration Data Objects的简称,它是一组高层的COM对象集合,并经历了好几个版本的演化,现在在Windows2000和Exchange200 ...

  5. cdo收取邮件_利用CDO实现邮件回执功能

    http://blog.csdn.net/irvine007/archive/2006/02/22/606117.aspx 引用CDO组件,SYSTEM32下的CDOSYS.DLL,增加一个包装器 u ...

  6. cdo收取邮件_使用 CDO 发送测试电子邮件消息

    此脚本使用 CDO 发送测试电子邮件消息. Visual Basic Set objEmail = CreateObject("CDO.Message") set objConf ...

  7. python函数调用的三种方式_判断python对象是否可调用的三种方式及其区别

    基本上判断python对象是否为可调用的函数,有三种方法: 1.使用内置的callable函数 callable(func) 用于检查对象是否可调用,返回True也可能调用失败,但是返回False一定 ...

  8. mysql mybatis模糊查询语句_详解MyBatis模糊查询LIKE的三种方式

    模糊查询也是数据库SQL中使用频率很高的SQL语句,使用MyBatis来进行更加灵活的模糊查询. 直接传参法 直接传参法,就是将要查询的关键字keyword,在代码中拼接好要查询的格式,如%keywo ...

  9. java 字节码对象_获得类的字节码对象的三种方式

    java源码(xxx.java)通过编译后形成字节码文件,字节码文件通过类加载器获得字节码对象, 通过字节码对象可以操作源码中的属性和方法. 方式一,使用类的class属性: Class clazz1 ...

最新文章

  1. LeetCode 268. Missing Number--Python解法--数学题
  2. 为电商而生的知识图谱,如何感应用户需求?
  3. tftp下载内核和文件系统
  4. c++多线程队列 类对象
  5. 推荐一个 React 技术揭秘的项目,自顶向下的 React 源码分析
  6. (八)数据结构之“树”
  7. Android8.0适配-Only fullscreen opaque activities can request orientation
  8. Python学习之旅:使用Python实现Linux中的ls命令
  9. android MVC和MVP探讨
  10. 省市三级联动(数据库)
  11. halo_Halo 3评论
  12. ffmpeg 命令的使用
  13. 简述——什么是软件质量的六大特性?
  14. HTML中<img src=““>中图片路径的用法
  15. 【韩松】Deep Gradient Comression_一只神秘的大金毛_新浪博客
  16. python输入文字光标变成黑块_pycharm鼠标光标变成黑色方块
  17. 活在当下,何必去想过去和将来
  18. (转)《杂 文》 之 教你制作一份属于自己的简历
  19. Oracle存储过程以及游标嵌套实际使用
  20. 复古纹路绩效考核培训PPT模板

热门文章

  1. 7-11 树最近公共祖先
  2. 业余无线电相关操作备忘
  3. Adversary在计算机安全领域的含义
  4. 小白入门的网络安全常用术语
  5. 钛碳化铝(Ti3AlC2)在实验检测领域中的应用
  6. Java基础(不定时更新)
  7. 天猫商城源代码变更对爬虫的影响
  8. 创龙TI AM437x ARM Cortex-A9 CPU开发板JTAG仿真器接口、LED指示灯
  9. RobotFramework安装完成后怎么在桌面显示ride图标
  10. Hibernate连接达梦数据库不带模式名,解决无效的表或视图名,DmDialect方言