通过.NET实现后台自动发送Email功能的代码示例

本文介绍了一种使用.NET框架后台自动发送电子邮件的方法。通过配置web.config文件中的SMTP信息,并利用System.Net.Mail命名空间中的MailMessage和SmtpClient类,可以轻松实现邮件的自动发送功能。
原文: 通过.NET实现后台自动发送Email功能的代码示例

 

通过.NET实现后台自动发送邮件功能的代码,可以将一些基础信息放到web.config文件中进行保存:


Web.config文件信息段:

<system.net> <mailSettings> <smtp deliveryMethod="Network" from="待发送邮箱地址"> <network host="待发送邮箱SMTP地址" userName="待发送邮箱地址" password="邮箱密码" defaultCredentials="true"/> </smtp> </mailSettings> </system.net>


后台实现:

using System.Net.Mail; using System.Configuration; using System.Net.Configuration; using System.Web.Configuration; // addresses for send email. string[] address = new string[] { "接收邮箱地址1", "接收邮箱地址2" }; // email body. string emailBody = "Hello! Guys!"; // email subject. string subject = "This is a test!"; // get the config info. SmtpSection smtpSection = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp; // save the mail object. MailMessage mm = new MailMessage(); foreach (string item in address) mm.To.Add(item); mm.From = new MailAddress(smtpSection.From); mm.BodyEncoding = System.Text.Encoding.UTF8; mm.SubjectEncoding = System.Text.Encoding.UTF8; mm.IsBodyHtml = true; mm.Body = emailBody; mm.Subject = subject; // set and send email. SmtpClient sc = new SmtpClient(smtpSection.Network.Host); sc.DeliveryMethod = SmtpDeliveryMethod.Network; sc.Credentials = new System.Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password); sc.Send(mm);

posted on 2014-04-29 16:42 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/3699567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值