.NET2.0中发送邮件
.cs文件中的SendMail方法

/**//// <summary>
/// 发送邮件
/// </summary>
public void SendMail()
{
try
{
//MailMessage(From “来自邮件”,To “发送到邮件”,Subject “主题”,Body “邮件正文”)
MailMessage Ms = new MailMessage("xxx@xxx.com", "xxx@xxx.com", "sfsdfsdf", "sdfsdfsd");
//Mail 抄送 (CC)
Ms.CC.Add("xxx@xxx.com");
//Mail 密件抄送 (BCC)
Ms.Bcc.Add("xxx@xxx.com");
//Mail 附件
Attachment At = new Attachment(@"C:\Documents and Settings\grant\Desktop\New Text Document.txt");
Ms.Attachments.Add(At);
//邮件发送
SmtpClient sc = new SmtpClient();
sc.Send(Ms);
}
catch
{
throw;
}
finally
{
//释放
if (At != null)
{
At.Dispose();
}
Ms.Dispose();
}
}Web.config文件中加入
1
<system.net>2
<mailSettings>3
<smtp>4
<!--5
host:邮件服务器6
port:延迟时间7
userName:登录服务器的帐号8
password:登录服务器的密码9
defaultCredentials:当我们不需要smtp服务器验证发送用户时,就将它设为false,反之就设为true10
-->11
<network host="xxx.com" port="25" userName="xxx" password="xxx" defaultCredentials="true"/>12
</smtp>13
</mailSettings>14
</system.net>
浙公网安备 33010602011771号