.net 发送邮件
.net 发送邮件
Code
1
2 /**//// <summary>
3 /// 发送邮件(多个人以,分开)
4 /// </summary>
5 /// <param name="from">发件人</param>
6 /// <param name="to">收件人</param>
7 /// <param name="title">标题/主题</param>
8 /// <param name="content">信件内容</param>
9 /// <param name="cc">抄送人</param>
10 /// <param name="sFileName">文件名称</param>
11 /// <returns></returns>
12 public static string SendMail(string from, string to, string title, string content, string cc, string sFileName)
13 {
14 HttpContext context = HttpContext.Current;
15 string sMsg = "";
16 try
17 {
18 string sPath = Global.UTDocPath + "Email/";
19 SmtpClient smtp = new SmtpClient("testEmail");
20 smtp.UseDefaultCredentials = true;
21 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
22 MailMessage msg = new MailMessage(from, to, title, content);
23 if (sFileName != "")
24 {
25 string[] sfile = sFileName.Split(new char[] { ',' });
26 for (int i = 0; i < sfile.GetLength(0); i++)
27 {
28 if (sfile[i].Trim() != "")
29 {
30 System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(context.Server.MapPath(sPath + "/" + sfile[i].Trim()));
31 msg.Attachments.Add(attachment);
32 }
33 }
34
35 }
36
37 msg.BodyEncoding = System.Text.Encoding.UTF8;
38 msg.SubjectEncoding = System.Text.Encoding.UTF8;
39 if (cc != "")
40 msg.CC.Add(cc);
41 msg.IsBodyHtml = true;
42 smtp.Send(msg);
43 sMsg = "发送成功";
44 }
45 catch (System.Net.Mail.SmtpException ex)
46 {
47 sMsg = ex.Message.ToString();
48 }
49 return sMsg;
50 }
51
1
2 /**//// <summary>
3 /// 发送邮件(多个人以,分开)
4 /// </summary>
5 /// <param name="from">发件人</param>
6 /// <param name="to">收件人</param>
7 /// <param name="title">标题/主题</param>
8 /// <param name="content">信件内容</param>
9 /// <param name="cc">抄送人</param>
10 /// <param name="sFileName">文件名称</param>
11 /// <returns></returns>
12 public static string SendMail(string from, string to, string title, string content, string cc, string sFileName)
13 {
14 HttpContext context = HttpContext.Current;
15 string sMsg = "";
16 try
17 {
18 string sPath = Global.UTDocPath + "Email/";
19 SmtpClient smtp = new SmtpClient("testEmail");
20 smtp.UseDefaultCredentials = true;
21 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
22 MailMessage msg = new MailMessage(from, to, title, content);
23 if (sFileName != "")
24 {
25 string[] sfile = sFileName.Split(new char[] { ',' });
26 for (int i = 0; i < sfile.GetLength(0); i++)
27 {
28 if (sfile[i].Trim() != "")
29 {
30 System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(context.Server.MapPath(sPath + "/" + sfile[i].Trim()));
31 msg.Attachments.Add(attachment);
32 }
33 }
34
35 }
36
37 msg.BodyEncoding = System.Text.Encoding.UTF8;
38 msg.SubjectEncoding = System.Text.Encoding.UTF8;
39 if (cc != "")
40 msg.CC.Add(cc);
41 msg.IsBodyHtml = true;
42 smtp.Send(msg);
43 sMsg = "发送成功";
44 }
45 catch (System.Net.Mail.SmtpException ex)
46 {
47 sMsg = ex.Message.ToString();
48 }
49 return sMsg;
50 }
51