Asp.net 自动发送邮件的方法

 http://www.cnblogs.com/291099657/archive/2009/03/12/1409365.html

 

 

一、首先需要配置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
 4
 5 一、首先需要配置web.config文件:
 6
 7
 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
17
18 二、然后编写发送邮件的函数:
19
20
21 /**/ ///<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 {
32  try
33  {
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    {
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  {
57    return false;
58  }

59}

60
61
62 三、最后就是对函数的调用了:
63
64
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 {
73  
74  //发送成功,进行相应处理
75}

76 else
77 {
78  
79  //发送失败,进行相应处理
80  return;
81}

82  
83

 


三、最后就是对函数的调用了:


 

 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 {
 9  
10  //发送成功,进行相应处理
11}

12 else
13 {
14  
15  //发送失败,进行相应处理
16  return;
17}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值