今天有一个模块需要自动发送邮件的功能,就随便写了一个,记录一下作为积累。
|
今天有一个模块需要自动发送邮件的功能,就随便写了一个,记录一下作为积累。
一、首先需要配置web.config文件:
1 <system.net>
2 <mailSettings>
3 <smtp from="Emailname">
4 <network host="smtp.163.com" userName="Emailname" password="Emailpassword"
5 port="25" defaultCredentials="false"/>
6 </smtp>
7 </mailSettings>
8 </system.net>
二、然后编写发送邮件的函数:
1 Asp.net 自动发送邮件的方法
2 今天有一个模块需要自动发送邮件的功能,就随便写了一个,记录一下作为积累。
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5 一、首先需要配置web.config文件:
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8 <system.net>
9 <mailSettings>
10 <smtp from="Emailname">
11 <network host="smtp.163.com" userName="Emailname" password="Emailpassword"
12 port="25" defaultCredentials="false"/>
13 </smtp>
14 </mailSettings>
15 </system.net>
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18 二、然后编写发送邮件的函数:
19![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**///// <summary>
22 /// 邮件发送方法(带附件)
23 /// </summary>
24 /// <param name="mailto">收件人地址。如:receiver@163.com</param>
25 /// <param name="mailsubject">邮件标题</param>
26 /// <param name="mailbody">邮件正文</param>
27 /// <param name="mailFrom">邮件发送人地址。如:sender@163.com</param>
28 /// <param name="list">附件路径</param>
29 /// <returns></returns>
30 public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
32 try
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
34 //邮件发送人地址
35 System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom);
36 //如test@163.com,初步测试,用test@sina.com不行,用163的邮件服务器,就必须用163邮箱的用户名
37 //收件人地址
38 System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如test@tom.com
39 System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
40 mail.Subject = mailsubject;
41 mail.Body = mailbody;
42 //以下设置服务器
43 System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient();
44 //以下为增加附件
45 int count = list.Count;
46 for (int i = 0; i < count; i++)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
48 System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list[i].ToString());
49 mail.Attachments.Add(data);
50 }
51 mySmth.Send(mail);
52 mail.Dispose();
53 return true;
54 }
55 catch
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
57 return false;
58 }
59 }
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
62 三、最后就是对函数的调用了:
63![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65 //自动发送邮件
66 string mailSubject = "会员注册确认函";
67 string mailBody = "正文内容。";
68 string mailFrom = ConfigurationManager.AppSettings["SendMail"];
69 ArrayList List = new ArrayList();
70 List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"]));
71 if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom, List))
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
73 ![](https://www.cnblogs.com/Images/dot.gif)
74 //发送成功,进行相应处理
75 }
76 else
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
78 ![](https://www.cnblogs.com/Images/dot.gif)
79 //发送失败,进行相应处理
80 return;
81 }
82
83![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三、最后就是对函数的调用了:
1 //自动发送邮件
2 string mailSubject = "会员注册确认函";
3 string mailBody = "正文内容。";
4 string mailFrom = ConfigurationManager.AppSettings["SendMail"];
5 ArrayList List = new ArrayList();
6 List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"]));
7 if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom, List))
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
9 ![](https://www.cnblogs.com/Images/dot.gif)
10 //发送成功,进行相应处理
11 }
12 else
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
14 ![](https://www.cnblogs.com/Images/dot.gif)
15 //发送失败,进行相应处理
16 return;
17 }
|
posted on
2008-06-08 07:57
阿神
阅读(
1820)
评论()
编辑
收藏
举报