第一种方法:
using System.Web.Mail; public void sendMail() { MailMessage mail1 = new MailMessage(); mail1.Body="body here"; mail1.From="[email protected]"; mail1.To="[email protected]"; mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1); mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","[email protected]"); mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********"); SmtpMail.SmtpServer="mail.xxx.com"; SmtpMail.Send(mail1); } 以上添加的几个 Fields 是用来作SMTP发信认证的,如果你的发信服务器不需要认证,就可以省略这几句。 第二种方法: using System.Net.Mail; 方法一:向单个地址发送邮件,不设置web.config文件 public void SendMail() { string mailto = "[email protected]"; string mailfrom = "[email protected]"; System.Net.NetworkCredential credential = new System.Net.NetworkCredential("from_username", "from_password"); SmtpClient smtp = new SmtpClient("smtp.company.com"); smtp.Credentials = credential; MailMessage message = new MailMessage(mailfrom, mailto); message.SubjectEncoding = System.Text.Encoding.UTF8; message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = "subject here"; message.Body = "body here"; smtp.Send(message); message.Dispose(); } 方法二、向单个地址发送邮件,设置web.config文件 public void SendMail() { string mailto = "[email protected]"; string mailfrom = "[email protected]"; MailMessage message = new MailMessage(mailfrom, mailto); message.SubjectEncoding = System.Text.Encoding.UTF8; message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = "subject here"; message.Body = "body here"; smtp.Send(message); message.Dispose(); } 在web.config中添加如下: <system.net> <mailSettings> <smtp from="[email protected]"> <network host="smtp.company.com" port="25" userName="from_username" password="from_password"/> </smtp> </mailSettings> </system.net> 方法三:群发邮件,设置web.config文件 public void SendEmail() { string mailto = "[email protected],[email protected]"; string title = "mail title here"; string content = "mail content here"; SmtpClient smtp = new SmtpClient(); MailMessage message = new MailMessage(); MailAddressCollection address = new MailAddressCollection(); string[] mailtos = mailto.Split(','); for (int i = 0; i < mailtos.Length; i++) { address.Add(mailtos[i]); } foreach (MailAddress add in address) { message.To.Add(add); } message.SubjectEncoding = System.Text.Encoding.UTF8; message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = title; message.Body = content; smtp.Send(message); message.Dispose(); address.Clear(); } 在web.config中添加如下: <system.net> <mailSettings> <smtp from="[email protected]"> <network host="smtp.company.com" port="25" userName="from_username" password="from_password"/> </smtp> </mailSettings> </system.net> 采用以上方法,如果运行发信程序的计算机上装有邮件监控等杀毒软件,会有失败的警告,但实际已发送成功。解决办法是关闭杀毒软件的监控功能。 下面这个记得加上命名空间: using System.Net; 适用的框架:asp.net framework 2.0/.net framework3.0/.net framework3.5 在web.config里设置.net framework的网络连接
XML/HTML代码
如下:设置stmp邮件发送的配置
XML/HTML代码
以编程的方式获取web.config里的smtp配置 NetSectionGroup类 命名空间 定义: 说明: 在程序里读取configuration/system.net/mailSettings/stmp配置
C#代码
发送电子邮件
C#代码
[转]:http://www.cnblogs.com/matrix/archive/2004/05/20/10495.aspx
现在的邮件发送大多数需要STMP的身份验证,
private void Button1_Click(object sender, System.EventArgs e) DateTime t=DateTime.Now; Asp.net 自动发送邮件的方法 今天有一个模块需要自动发送邮件的功能,就随便写了一个,记录一下作为积累。 一、首先需要配置web.config文件: <system.net> <mailSettings> <smtp from="Emailname"> <network host="smtp.163.com" userName="Emailname" password="Emailpassword" port="25" defaultCredentials="false"/> </smtp> </mailSettings> </system.net> 二、然后编写发送邮件的函数: //// <summary> /// 邮件发送方法(带附件) /// </summary> /// <param name="mailto">收件人地址。如:[email protected]</param> /// <param name="mailsubject">邮件标题</param> /// <param name="mailbody">邮件正文</param> /// <param name="mailFrom">邮件发送人地址。如:[email protected]</param> /// <param name="list">附件路径</param> /// <returns></returns> public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list) { try { //邮件发送人地址 System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom); //如[email protected],初步测试,用[email protected]不行,用163的邮件服务器,就必须用163邮箱的用户名 //收件人地址 System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如[email protected] System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to); mail.Subject = mailsubject; mail.Body = mailbody; //以下设置服务器 System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient(); //以下为增加附件 int count = list.Count; for (int i = 0; i < count; i++) { System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list[i].ToString()); mail.Attachments.Add(data); } mySmth.Send(mail); mail.Dispose(); return true; } catch { return false; } } 三、最后就是对函数的调用了: //自动发送邮件 string mailSubject = "会员注册确认函"; string mailBody = "正文内容。"; string mailFrom = ConfigurationManager.AppSettings["SendMail"]; ArrayList List = new ArrayList(); List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"])); if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom, List)) { ... //发送成功,进行相应处理 } else { ... //发送失败,进行相应处理 return; } |