1 using System.Net.Mail;
2 using System.Text;
3 using System.Net;
4 #region 邮件发送
5 /// <summary>
6 /// 邮件发送
7 /// </summary>
8 /// <param name="str">字符串</param>
9 /// <returns></returns>
10 public static string SendMail(string mailtitle, string mailcontent, string toemail, string toname)
11 {
12 ////设置发件人信箱,及显示名字
13 MailAddress from = new MailAddress("xxx@xxx.com", "xxx");
14 //设置收件人信箱,及显示名字
15 MailAddress to = new MailAddress(toemail, toname);
16 //创建一个MailMessage对象
17 MailMessage oMail = new MailMessage(from, to);
18 oMail.Subject = mailtitle; //邮件标题
19 oMail.Body = mailcontent; //邮件内容
20 oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
21 oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
22 oMail.Priority = MailPriority.High;//设置邮件的优先级为高
23 //发送邮件服务器
24 SmtpClient client = new SmtpClient();
25 client.Host = "mail.xxxx.com"; //指定邮件服务器
26 client.Credentials = new NetworkCredential("xxx@xxxx.com", "xxxxx");//指定服务器邮件,及密码
27 //发送
28 try
29 {
30 client.Send(oMail); //发送邮件
31 oMail.Dispose(); //释放资源
32 return "1";
33 }
34 catch (Exception ex)
35 {
36 oMail.Dispose(); //释放资源
37 return ex.Message;
38 }
39 }
40 #endregion
41 #region 自定义邮件发送
42 /// <summary>
43 /// 邮件发送
44 /// </summary>
45 /// <param name="str">字符串</param>
46 /// <returns></returns>
47 public static string SendMail(string fromEmail,string fromName,string host,string username,string password,string mailtitle, string mailcontent, string toemail, string toname)
48 {
49 ////设置发件人信箱,及显示名字
50 MailAddress from = new MailAddress(fromEmail, fromName);
51 //设置收件人信箱,及显示名字
52 MailAddress to = new MailAddress(toemail, toname);
53 //创建一个MailMessage对象
54 MailMessage oMail = new MailMessage(from, to);
55 oMail.Subject = mailtitle; //邮件标题
56 oMail.Body = mailcontent; //邮件内容
57 oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
58 oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
59 oMail.Priority = MailPriority.High;//设置邮件的优先级为高
60 //发送邮件服务器
61 SmtpClient client = new SmtpClient();
62 client.Host = host; //指定邮件服务器
63 client.Credentials = new NetworkCredential(username,password);//指定服务器邮件,及密码
64 //发送
65 try
66 {
67 client.Send(oMail); //发送邮件
68 oMail.Dispose(); //释放资源
69 return "1";
70 }
71 catch (Exception ex)
72 {
73 oMail.Dispose(); //释放资源
74 return ex.Message;
75 }
76 }
77 #endregion