自动发送Email功能的代码示例

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

代码<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from=" yanggl@hotmail.com ">
<network host="smtp.hotmail.com" userName="yanggl@hotmail.com" 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[] { "guilin_gavin@hotmail.com", "glyang@isoftstone.com" };
// 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);

转载于:https://www.cnblogs.com/guowei1027/archive/2009/12/16/1625466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值