C#发送邮件
2010-08-19 09:42 calm_水手 阅读(247) 评论(0) 编辑 收藏 举报代码
1 /// <summary>
2 /// 发送邮件
3 /// </summary>
4 /// <param name="MailFromAddress">发件人地址</param>
5 /// <param name="MailFromUserName">用户名</param>
6 /// <param name="MailFormPwd">密码</param>
7 /// <param name="MailTitle">邮件标题</param>
8 /// <param name="MailBody">邮件内容</param>
9 /// <param name="MailTo">收件人地址</param>
10 /// <returns>发送成功返回True否则返回False</returns>
11 public static Boolean SendMail(string MailFromAddress, string MailFormPwd, string MailTitle, string MailBody, string MailTo)
12 {
13 try
14 {
15 MailMessage objMail = new MailMessage();
16 //发件人地址
17 objMail.From = new MailAddress(MailFromAddress);
18 //邮件标题
19 objMail.Subject = MailTitle;
20 //邮件标题编码
21 objMail.SubjectEncoding = System.Text.Encoding.UTF8;
22 //邮件内容
23 objMail.Body = MailBody;
24 //邮件内容编码
25 objMail.BodyEncoding = System.Text.Encoding.UTF8;
26 //收件人地址在这里可以加多个
27 objMail.To.Add(MailTo);
28 //用SMTP发送邮件的方式
29 SmtpClient client = new SmtpClient();
30 //用户名和密码
31 client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFormPwd);
32 string StrMail = "";
33 for (int i = 0; i < MailFromAddress.Length; i++)
34 {
35 if (MailFromAddress.Substring(i, 1).ToString().Trim() == "@")
36 {
37 StrMail = MailFromAddress.Substring(i + 1, MailFromAddress.Length - i - 1);
38 }
39 }
40 //服务器名
41 client.Host = "smtp." + StrMail.ToString().Trim();
42 //发送
43 client.Send(objMail);
44 return true;
45 }
46 catch (Exception)
47 {
48 return false;
49 }
50 }
51
2 /// 发送邮件
3 /// </summary>
4 /// <param name="MailFromAddress">发件人地址</param>
5 /// <param name="MailFromUserName">用户名</param>
6 /// <param name="MailFormPwd">密码</param>
7 /// <param name="MailTitle">邮件标题</param>
8 /// <param name="MailBody">邮件内容</param>
9 /// <param name="MailTo">收件人地址</param>
10 /// <returns>发送成功返回True否则返回False</returns>
11 public static Boolean SendMail(string MailFromAddress, string MailFormPwd, string MailTitle, string MailBody, string MailTo)
12 {
13 try
14 {
15 MailMessage objMail = new MailMessage();
16 //发件人地址
17 objMail.From = new MailAddress(MailFromAddress);
18 //邮件标题
19 objMail.Subject = MailTitle;
20 //邮件标题编码
21 objMail.SubjectEncoding = System.Text.Encoding.UTF8;
22 //邮件内容
23 objMail.Body = MailBody;
24 //邮件内容编码
25 objMail.BodyEncoding = System.Text.Encoding.UTF8;
26 //收件人地址在这里可以加多个
27 objMail.To.Add(MailTo);
28 //用SMTP发送邮件的方式
29 SmtpClient client = new SmtpClient();
30 //用户名和密码
31 client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFormPwd);
32 string StrMail = "";
33 for (int i = 0; i < MailFromAddress.Length; i++)
34 {
35 if (MailFromAddress.Substring(i, 1).ToString().Trim() == "@")
36 {
37 StrMail = MailFromAddress.Substring(i + 1, MailFromAddress.Length - i - 1);
38 }
39 }
40 //服务器名
41 client.Host = "smtp." + StrMail.ToString().Trim();
42 //发送
43 client.Send(objMail);
44 return true;
45 }
46 catch (Exception)
47 {
48 return false;
49 }
50 }
51
转自:http://www.cnblogs.com/sufei/archive/2009/03/28/1423891.html