发送电子邮件

来源:http://www.cnblogs.com/damonlan/archive/2012/04/28/2473525.html

作者:浪迹天涯

 

用的dll是微软自带的,觉得挺好用的!!

 1 public class SimpleEmailHelper
 2     {
 3         private string _SmtpAdd;
 4         private string _UserID;
 5         private string _UserPsw;
 6 
 7         public SimpleEmailHelper(string smtpAddress, string userID, string userPsw)
 8         {
 9             _SmtpAdd = smtpAddress;
10             _UserID = userID;
11             _UserPsw = userPsw;
12         }
13 
14         public bool Send(string from, string to, string subject, string message,string cc)
15         {
16             return Send(from, from, to, to, subject, message,cc);
17         }
18 
19         public bool Send(string from, string fromDisplay, string sendTo, string sendToDisplay,string subject, string message,string cc)
20         {
21 
22             bool ret = true;
23 
24             SmtpClient client = new SmtpClient();
25             client.Host = _SmtpAdd;//邮件服务器 比如 网易的是 smtp.163.COM
26             client.Port = 25;//端口号,也可不写
27             client.DeliveryMethod = SmtpDeliveryMethod.Network;//发送方式
28             client.Credentials = new NetworkCredential(_UserID, _UserPsw);//用户名和密码
29 
30             MailMessage myMessage = new MailMessage();
31             myMessage.Priority = MailPriority.Normal;//优先级
32             myMessage.From = new MailAddress(from, fromDisplay, Encoding.GetEncoding("gb2312"));
33             myMessage.To.Add(sendTo);
34             if (cc != "")
35             {
36                 myMessage.CC.Add(cc);
37             }
38             myMessage.Subject = subject;//邮件主题
39             myMessage.SubjectEncoding = Encoding.GetEncoding("gb2312");
40             myMessage.IsBodyHtml = true;
41             myMessage.BodyEncoding = Encoding.GetEncoding("gb2312");
42             myMessage.Body = message;//正文
43             myMessage.Attachments.Add(new Attachment(@"C:\Users\lando\Desktop\Flex问题集结号.txt"));//加入附件。。。
44             client.Send(myMessage);//开始发送。
45             return ret;
46         }
47     }

页面调用:

SQ.FrameWork.SimpleEmailHelper emailHelper = new SQ.FrameWork.SimpleEmailHelper(stmpServerIpAddress, userId, psw);
emailHelper.Send(from, distEmailAddress, TextBoxTopic.Text.Trim(),TextBoxContent.Text.Trim(),txtCCCleint.Text);
ShowMessage("邮件发送成功。");

需要注意一下的是:

stmpServerIpAddress:是收邮件的服务器地址,比如我用网易的,那么就是 smtp.163.com  等等
userId:你发电子邮件的用户名
psw:你发电子邮件的密码
from:发送人姓名
distEmailAddress:收件人列表,可以有多个,用逗号分隔开来。。都很好理解!~。

 

作者:Lanny☆兰东才
出处:http://www.cnblogs.com/damonlan
Q Q:*********
E_mail:Damon_lan@163.com or Dongcai.lan@hp.com

本博文欢迎大家浏览和转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,在『参考』的文章中,我会表明参考的文章来源,尊重他人版权。若您发现我侵犯了您的版权,请及时与我联系。

 

posted on 2015-02-07 19:36  华山青竹  阅读(310)  评论(0编辑  收藏  举报

导航