ASP.NET------邮件发送类

网站上用邮件服务进行收发邮件是再正常不过了,实现此功能之一的方法就是调用该类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace Core.Common.Service
{
/// <summary>
/// 邮件发送
/// </summary>
public static class EMail
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="to">收件人邮件地址</param>
/// <param name="from">发件人邮件地址</param>
/// <param name="displayName">发件人显示名</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="userName">登录smtp主机时用到的用户名,注意是邮件地址'@'以前的部分</param>
/// <param name="password">登录smtp主机时用到的用户密码</param>
/// <param name="smtpHost">发送邮件用到的smtp主机</param>
public static void Send(string to, string from, string displayName, string subject, string body, string userName, string password, string smtpHost)
{
try
{
MailAddress fromaddress
= new MailAddress(from, displayName);
MailAddress toaddress
= new MailAddress(to);
MailMessage message
= new MailMessage(fromaddress, toaddress);
message.Subject
= subject;//设置邮件主题
message.BodyEncoding = System.Text.UnicodeEncoding.UTF8;
message.IsBodyHtml
= true;//设置邮件正文为html格式
message.Body = body;//设置邮件内容
message.Priority = MailPriority.High;
SmtpClient client
= new SmtpClient(smtpHost);
client.DeliveryMethod
= SmtpDeliveryMethod.Network;
//设置发送邮件身份验证方式
//注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com
client.Credentials = new NetworkCredential(userName, password);
client.Send(message);
}
catch(Exception ex)
{
DirectoryAndFile.SaveFile(
"abc.txt", ex.GetType().ToString());
}
}
}
}

  

posted @ 2011-08-11 09:28  brainmao  阅读(908)  评论(2编辑  收藏  举报