先来第一种
代码语言:javascript复制 1 // 版权所有 ZhuoYue Co.,Ltd 卓越一通秘密信息
2 // 文件名称:MyEmailByCDO.cs
3 // 作 者:huangzh
4 // 创建日期:2015-08-20 16:18:27
5 // 功能描述:发送邮件MyEmailByCDO
6 // 任务编号:
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text;
11
12 namespace ZhuoYueE.Bpc.Core.Email
13 {
14
15 /// <summary>
16 /// 功能描述:发送邮件MyEmailByCDO
17 /// 作 者:huangzh
18 /// 创建日期:2015-08-20 16:18:24
19 /// 任务编号:
20 /// </summary>
21 public class MyEmailByCDO
22 {
23 ///<summary>
24 /// 构造函数
25 ///</summary>
26 ///<param name="strserver">发件箱的邮件服务器地址</param>
27 ///<param name="strtoMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
28 ///<param name="strfromMail">发件人地址</param>
29 ///<param name="strsubject">邮件标题</param>
30 ///<param name="stremailBody">邮件内容(可以以html格式进行设计)</param>
31 ///<param name="strpassword">发件人邮箱密码</param>
32 ///<param name="intport">发送邮件所用的端口号(htmp协议默认为25)</param>
33 ///<param name="blnsslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
34 ///<param name="strCC">抄送人</param>
35 public static void SendMyEmailByCDO(string strserver,
36 string strtoMail,
37 string strfromMail,
38 string strsubject,
39 string stremailBody,
40 string strpassword,
41 int intport,
42 bool blnsslEnable,
43 string strCC)
44 {
45 SendMyEmailByCDO(strserver,
46 strtoMail,
47 strfromMail,
48 strsubject,
49 stremailBody,
50 strpassword,
51 intport,
52 blnsslEnable,
53 strCC,
54 null);
55 }
56
57 ///<summary>
58 /// 构造函数
59 ///</summary>
60 ///<param name="strserver">发件箱的邮件服务器地址</param>
61 ///<param name="strtoMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
62 ///<param name="strfromMail">发件人地址</param>
63 ///<param name="strsubject">邮件标题</param>
64 ///<param name="stremailBody">邮件内容(可以以html格式进行设计)</param>
65 ///<param name="strpassword">发件人邮箱密码</param>
66 ///<param name="intport">发送邮件所用的端口号(htmp协议默认为25)</param>
67 ///<param name="blnsslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
68 /// <param name="strCC">抄送人</param>
69 /// <param name="files">附件</param>
70 public static void SendMyEmailByCDO(
71 string strserver,
72 string strtoMail,
73 string strfromMail,
74 string strsubject,
75 string stremailBody,
76 string strpassword,
77 int intport,
78 bool blnsslEnable,
79 string strCC,
80 List<string> files)
81 {
82 string stryourEmail = strfromMail;
83 CDO.Message message = new CDO.Message();
84 CDO.IConfiguration configuration = message.Configuration;
85 ADODB.Fields fields = configuration.Fields;
86
87 Console.WriteLine(String.Format("Configuring CDO settings..."));
88
89 ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
90 field.Value = strserver;
91
92 field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
93 field.Value = intport;
94
95
96 field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
97 field.Value = CDO.CdoSendUsing.cdoSendUsingPort;
98
99 field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
100 field.Value = CDO.CdoProtocolsAuthentication.cdoBasic;
101
102 field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
103 field.Value = stryourEmail;
104
105 field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
106 field.Value = strpassword;
107
108 field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
109 field.Value = blnsslEnable.ToString().ToLower();
110
111 fields.Update();
112
113 Console.WriteLine(String.Format("Building CDO Message..."));
114
115 message.From = stryourEmail;
116 message.To = strtoMail;
117 message.Subject = strsubject;
118 message.BodyPart.Charset = "utf-8";
119
120
121 if (!string.IsNullOrEmpty(strCC))
122 {
123 message.CC = strCC;
124 }
125
126 message.HTMLBody = stremailBody;
127 if (files != null)
128 {
129 foreach (string stritem in files)
130 {
131 message.AddAttachment(stritem);
132 }
133 }
134
135 Console.WriteLine(String.Format("Attempting to connect to remote server..."));
136
137 // Send message.
138 message.Send();
139 }
140 }
141 }
第二种
代码语言:javascript复制 1 // 版权所有 ZhuoYue Co.,Ltd 卓越一通秘密信息
2 // 文件名称:MyEmail.cs
3 // 作 者:huangzh
4 // 创建日期:2015-08-20 08:37:07
5 // 功能描述:邮件辅助类
6 // 任务编号:
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text;
11 using System.Net.Mail;
12 using System.Net.Mime;
13 using System.IO;
14
15 namespace ZhuoYueE.Bpc.Core.Email
16 {
17 /// <summary>
18 /// 功能描述:邮件辅助类
19 /// 作 者:huangzh
20 /// 创建日期:2015-08-20 08:36:52
21 /// 任务编号:
22 /// </summary>
23 public class MyEmailBySmtp
24 {
25 /// <summary>
26 /// 主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
27 /// </summary>
28 private MailMessage m_mailMessage;
29 /// <summary>
30 /// 主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
31 /// </summary>
32 private SmtpClient m_smtpClient;
33 /// <summary>
34 /// 发送邮件所用的端口号(htmp协议默认为25)
35 /// </summary>
36 private int m_intsenderPort;
37 /// <summary>
38 /// 发件箱的邮件服务器地址(IP形式或字符串形式均可)
39 /// </summary>
40 private string m_strsenderServerHost;
41 /// <summary>
42 /// 发件箱的密码
43 /// </summary>
44 private string m_strsenderPassword;
45 /// <summary>
46 /// 发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)
47 /// </summary>
48 private string m_strsenderUsername;
49 /// <summary>
50 /// 是否对邮件内容进行socket层加密传输
51 /// </summary>
52 private bool m_blnsnableSsl;
53 /// <summary>
54 /// 抄送
55 /// </summary>
56 private string m_strCC;
57
58 ///<summary>
59 /// 构造函数
60 ///</summary>
61 ///<param name="strserver">发件箱的邮件服务器地址</param>
62 ///<param name="strtoMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
63 ///<param name="strfromMail">发件人地址</param>
64 ///<param name="strsubject">邮件标题</param>
65 ///<param name="stremailBody">邮件内容(可以以html格式进行设计)</param>
66 ///<param name="strusername">发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)</param>
67 ///<param name="strpassword">发件人邮箱密码</param>
68 ///<param name="intport">发送邮件所用的端口号(htmp协议默认为25)</param>
69 ///<param name="blnsslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
70 ///<param name="strCC">抄送</param>
71 public MyEmailBySmtp(
72 string strserver,
73 string strtoMail,
74 string strfromMail,
75 string strsubject,
76 string stremailBody,
77 string strusername,
78 string strpassword,
79 int intport,
80 bool blnsslEnable,
81 string strCC)
82 {
83 try
84 {
85
86 m_mailMessage = new MailMessage();
87 m_mailMessage.To.Add(strtoMail);
88 m_mailMessage.From = new MailAddress(strfromMail, strfromMail, Encoding.UTF8);
89 m_mailMessage.Subject = strsubject;
90 m_mailMessage.Body = stremailBody;
91 m_mailMessage.IsBodyHtml = true;
92 m_mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
93 m_mailMessage.Priority = MailPriority.High;
94 m_mailMessage.CC.Add(strCC);
95 this.m_strsenderServerHost = strserver;
96 this.m_strsenderUsername = strusername;
97 this.m_strsenderPassword = strpassword;
98 this.m_intsenderPort = intport;
99 this.m_blnsnableSsl = blnsslEnable;
100 }
101 catch
102 {
103 throw;
104 }
105 }
106
107 ///<summary>
108 /// 添加附件
109 ///</summary>
110 ///<param name="strattachmentsPath">附件的路径集合,以分号分隔</param>
111 public void AddAttachments(string strattachmentsPath)
112 {
113 try
114 {
115 //以什么符号分隔可以自定义
116 string[] path = strattachmentsPath.Split(';');
117 Attachment data;
118 ContentDisposition disposition;
119 for (int i = 0; i < path.Length; i )
120 {
121 data = new Attachment(path[i], MediaTypeNames.Application.Octet);
122 disposition = data.ContentDisposition;
123 disposition.CreationDate = File.GetCreationTime(path[i]);
124 disposition.ModificationDate = File.GetLastWriteTime(path[i]);
125 disposition.ReadDate = File.GetLastAccessTime(path[i]);
126 m_mailMessage.Attachments.Add(data);
127 }
128
129 }
130 catch
131 {
132 throw;
133 }
134 }
135
136 ///<summary>
137 /// 邮件的发送
138 ///</summary>
139 public void Send()
140 {
141 try
142 {
143
144 if (m_mailMessage != null)
145 {
146 m_smtpClient = new SmtpClient();
147 m_smtpClient.Host = this.m_strsenderServerHost;
148 m_smtpClient.Port = m_intsenderPort;
149 m_smtpClient.UseDefaultCredentials = true;
150 m_smtpClient.EnableSsl = this.m_blnsnableSsl;
151 m_smtpClient.Credentials = new System.Net.NetworkCredential(this.m_strsenderUsername, this.m_strsenderPassword);
152 m_smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
153
154 m_smtpClient.Send(m_mailMessage);
155 }
156
157 }
158 catch
159 {
160 throw;
161 }
162 }
163 }
164 }
为什么这里写了2中方式呢???
我相信大多数人发送邮件使用第二种,但是有时候第二种方式是发不出去的,那么什么情况发布出去呢???
答曰:
当你使用公司内部邮件服务器发送邮件,并且需要SSL加密的时候,你就呵呵一下,第二种不行了,然后你可以尝试第一种了,最少我这里是这种情况。
我现在发送邮件的一般都用第一种的代码了。
有什么问题,请留言吧。