C#邮件发送的具体实现
啥也不多说了,看代码吧!这是小可自己写好测试过的代码!
1
using System;
2
using System.Reflection;
3
using System.Web.Mail;
4
using System.Data;
5
using System.IO;
6
using System.Text.RegularExpressions;
7
//using Logs;
8
9
namespace Jetcom.Mails
10
{
11
/// <summary>
12
/// SendMail 的摘要描述。
13
/// </summary>
14
public abstract class SendMail
15
{
16
//private Log log;
17
18
public SendMail()
19
{
20
//log = new Log();
21
}
22
23
/// <summary>
24
/// 发送邮件
25
/// </summary>
26
/// <param name="sAttach">附件,文件全路径,多个请用","隔开</param>
27
/// <param name="server">MAIL服务器名称或IP都行(比如:smtp.sohu.com)</param>
28
/// <param name="to">接收者邮箱 (比如:XXX@sohu.com),这里当然也可以是多个,以;来分开</param>
29
/// <param name="cc">同时暗送给那些接受者</param>
30
/// <param name="from">发送的邮件帐号 (比如:XXX@sohu.com)</param>
31
/// <param name="pwd">邮件主题</param>
32
/// <param name="subject">发送的帐号密码</param>
33
/// <param name="MailBody">邮件主体内容</param>
34
public static void SendEmail(string sAttach,string server,string to,string cc,string from,string pwd,string subject,string MailBody)
35
{
36
System.Web.Mail.MailMessage m_Mail;
37
38
m_Mail = new MailMessage();
39
SmtpMail.SmtpServer = server;
40
m_Mail.From = from;
41
m_Mail.To = to;
42
m_Mail.Cc = cc;
43
m_Mail.Subject = subject;
44
m_Mail.BodyFormat=MailFormat.Html;
45
m_Mail.Body= MailBody;
46
47
// 取得帐号ID
48
string user = "";
49
Regex r = new Regex("(?<account>.*?)@.*");
50
Match m = r.Match(from);
51
if (m != null)
52
{
53
user = m.Result("${account}");
54
}
55
56
// Concatenate a list of attachment files in a string.
57
58
// Build an IList of mail attachments using the files named in the string.
59
char[] delim = new char[] {','};
60
foreach (string sSubstr in sAttach.Split(delim))
61
{
62
MailAttachment myAttachment = new MailAttachment(sSubstr);
63
m_Mail.Attachments.Add(myAttachment);
64
}
65
66
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
67
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", user);
68
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd);
69
70
try
71
{
72
SmtpMail.Send(m_Mail);
73
//log.SaveLog("发送Email成功", from + " 已经发送邮件给 " + to + ",并且抄送(cc)给" + cc + " 成功!");
74
}
75
catch(Exception ex)
76
{
77
//log.SaveLog("发送Email失败",ex.ToString());
78
throw new Exception(ex.ToString(),ex);
79
}
80
81
}
82
/// <summary>
83
/// 发送邮件,服务器信息写在存储不过程中
84
/// 以以下格式:
85
/// <add key="SmtpServer" value=""/> MAIL服务器名称或IP都行(比如:smtp.sohu.com)
86
/// <add key="MailTo" value=""/> 接收者邮箱 (比如:XXX@sohu.com),这里当然也可以是多个,以;来分开
87
/// <add key="MailCc" value=""/> 同时暗送给那些接受者
88
/// <add key="MailFrom" value=""/> 发送的邮件帐号 (比如:XXX@sohu.com)
89
/// <add key="SmtpPwd" value=""/> 发送的帐号密码
90
/// <add key="MailSubject" value=""/> 邮件主题
91
/// <add key="MailBody" value=""/> 邮件主体内容
92
/// </summary>
93
/// <param name="sAttach">附件,文件全路径,多个请用","隔开</param>
94
public static void SendEmail(string sAttach)
95
{
96
// MAIL服务器名称或IP都行(比如:smtp.sohu.com)
97
string server = System.Configuration.ConfigurationSettings.AppSettings["SmtpServer"];
98
// 接收者邮箱 (比如:XXX@sohu.com),这里当然也可以是多个,以;来分开
99
string to = System.Configuration.ConfigurationSettings.AppSettings["MailTo"];
100
// 同时暗送给那些接受者
101
string cc = System.Configuration.ConfigurationSettings.AppSettings["MailCc"];
102
// 发送的邮件帐号 (比如:XXX@sohu.com)
103
string from = System.Configuration.ConfigurationSettings.AppSettings["MailFrom"];
104
// 邮件主题
105
string subject = System.Configuration.ConfigurationSettings.AppSettings["MailSubject"];
106
107
// 发送的帐号密码
108
string pwd = System.Configuration.ConfigurationSettings.AppSettings["SmtpPwd"];
109
110
System.Web.Mail.MailMessage m_Mail;
111
112
m_Mail = new MailMessage();
113
SmtpMail.SmtpServer = server;
114
m_Mail.From = from;
115
m_Mail.To = to;
116
m_Mail.Cc = cc;
117
m_Mail.Subject = subject;
118
m_Mail.BodyFormat=MailFormat.Html;
119
m_Mail.Body= System.Configuration.ConfigurationSettings.AppSettings["MailBody"];
120
121
// 取得帐号ID
122
string user = "";
123
Regex r = new Regex("(?<account>.*?)@.*");
124
Match m = r.Match(from);
125
if (m != null)
126
{
127
user = m.Result("${account}");
128
}
129
130
// Concatenate a list of attachment files in a string.
131
132
// Build an IList of mail attachments using the files named in the string.
133
char[] delim = new char[] {','};
134
foreach (string sSubstr in sAttach.Split(delim))
135
{
136
MailAttachment myAttachment = new MailAttachment(sSubstr);
137
m_Mail.Attachments.Add(myAttachment);
138
}
139
140
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
141
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", user);
142
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd);
143
144
try
145
{
146
SmtpMail.Send(m_Mail);
147
//log.SaveLog("发送Email成功", from + " 已经发送邮件给 " + to + ",并且抄送(cc)给" + cc + " 成功!");
148
}
149
catch(Exception ex)
150
{
151
//log.SaveLog("发送Email失败",ex.ToString());
152
throw new Exception(ex.ToString(),ex);
153
}
154
155
}
156
}
157
}
using System;2
using System.Reflection;3
using System.Web.Mail;4
using System.Data;5
using System.IO;6
using System.Text.RegularExpressions;7
//using Logs;8
9
namespace Jetcom.Mails10
{11
/// <summary>12
/// SendMail 的摘要描述。13
/// </summary>14
public abstract class SendMail15
{16
//private Log log;17
18
public SendMail()19
{20
//log = new Log();21
}22
23
/// <summary>24
/// 发送邮件25
/// </summary>26
/// <param name="sAttach">附件,文件全路径,多个请用","隔开</param>27
/// <param name="server">MAIL服务器名称或IP都行(比如:smtp.sohu.com)</param>28
/// <param name="to">接收者邮箱 (比如:XXX@sohu.com),这里当然也可以是多个,以;来分开</param>29
/// <param name="cc">同时暗送给那些接受者</param>30
/// <param name="from">发送的邮件帐号 (比如:XXX@sohu.com)</param>31
/// <param name="pwd">邮件主题</param>32
/// <param name="subject">发送的帐号密码</param>33
/// <param name="MailBody">邮件主体内容</param>34
public static void SendEmail(string sAttach,string server,string to,string cc,string from,string pwd,string subject,string MailBody)35
{36
System.Web.Mail.MailMessage m_Mail;37
38
m_Mail = new MailMessage();39
SmtpMail.SmtpServer = server;40
m_Mail.From = from;41
m_Mail.To = to;42
m_Mail.Cc = cc;43
m_Mail.Subject = subject;44
m_Mail.BodyFormat=MailFormat.Html;45
m_Mail.Body= MailBody;46
47
// 取得帐号ID48
string user = "";49
Regex r = new Regex("(?<account>.*?)@.*");50
Match m = r.Match(from);51
if (m != null)52
{53
user = m.Result("${account}");54
}55
56
// Concatenate a list of attachment files in a string.57
58
// Build an IList of mail attachments using the files named in the string.59
char[] delim = new char[] {','};60
foreach (string sSubstr in sAttach.Split(delim))61
{62
MailAttachment myAttachment = new MailAttachment(sSubstr);63
m_Mail.Attachments.Add(myAttachment);64
}65
66
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");67
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", user);68
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd);69
70
try71
{72
SmtpMail.Send(m_Mail);73
//log.SaveLog("发送Email成功", from + " 已经发送邮件给 " + to + ",并且抄送(cc)给" + cc + " 成功!");74
}75
catch(Exception ex)76
{77
//log.SaveLog("发送Email失败",ex.ToString());78
throw new Exception(ex.ToString(),ex);79
}80
81
}82
/// <summary>83
/// 发送邮件,服务器信息写在存储不过程中84
/// 以以下格式:85
/// <add key="SmtpServer" value=""/> MAIL服务器名称或IP都行(比如:smtp.sohu.com)86
/// <add key="MailTo" value=""/> 接收者邮箱 (比如:XXX@sohu.com),这里当然也可以是多个,以;来分开87
/// <add key="MailCc" value=""/> 同时暗送给那些接受者88
/// <add key="MailFrom" value=""/> 发送的邮件帐号 (比如:XXX@sohu.com)89
/// <add key="SmtpPwd" value=""/> 发送的帐号密码90
/// <add key="MailSubject" value=""/> 邮件主题91
/// <add key="MailBody" value=""/> 邮件主体内容92
/// </summary>93
/// <param name="sAttach">附件,文件全路径,多个请用","隔开</param>94
public static void SendEmail(string sAttach)95
{96
// MAIL服务器名称或IP都行(比如:smtp.sohu.com)97
string server = System.Configuration.ConfigurationSettings.AppSettings["SmtpServer"];98
// 接收者邮箱 (比如:XXX@sohu.com),这里当然也可以是多个,以;来分开99
string to = System.Configuration.ConfigurationSettings.AppSettings["MailTo"];100
// 同时暗送给那些接受者101
string cc = System.Configuration.ConfigurationSettings.AppSettings["MailCc"];102
// 发送的邮件帐号 (比如:XXX@sohu.com)103
string from = System.Configuration.ConfigurationSettings.AppSettings["MailFrom"]; 104
// 邮件主题105
string subject = System.Configuration.ConfigurationSettings.AppSettings["MailSubject"];106
107
// 发送的帐号密码108
string pwd = System.Configuration.ConfigurationSettings.AppSettings["SmtpPwd"];109
110
System.Web.Mail.MailMessage m_Mail;111
112
m_Mail = new MailMessage();113
SmtpMail.SmtpServer = server;114
m_Mail.From = from;115
m_Mail.To = to;116
m_Mail.Cc = cc;117
m_Mail.Subject = subject;118
m_Mail.BodyFormat=MailFormat.Html;119
m_Mail.Body= System.Configuration.ConfigurationSettings.AppSettings["MailBody"];120
121
// 取得帐号ID122
string user = "";123
Regex r = new Regex("(?<account>.*?)@.*");124
Match m = r.Match(from);125
if (m != null)126
{127
user = m.Result("${account}");128
}129
130
// Concatenate a list of attachment files in a string.131
132
// Build an IList of mail attachments using the files named in the string.133
char[] delim = new char[] {','};134
foreach (string sSubstr in sAttach.Split(delim))135
{136
MailAttachment myAttachment = new MailAttachment(sSubstr);137
m_Mail.Attachments.Add(myAttachment);138
}139
140
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");141
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", user);142
m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd);143
144
try145
{146
SmtpMail.Send(m_Mail);147
//log.SaveLog("发送Email成功", from + " 已经发送邮件给 " + to + ",并且抄送(cc)给" + cc + " 成功!");148
}149
catch(Exception ex)150
{151
//log.SaveLog("发送Email失败",ex.ToString());152
throw new Exception(ex.ToString(),ex);153
}154
155
}156
}157
}



浙公网安备 33010602011771号