主要是用到了System.Web.Mail命名空间,用到了此空间的三个类,分别是:
●MailMessage类,用于构造电子邮件
●MailAttachment类,用于构造电子邮件附件
●SmtpMail类,用于发送电子邮件及其附件
1、MailMessage类构造电子邮件
此类主要有以下属性和方法
★From 发件人的地址
★To 以分号分隔的收件人的地址列表
★Cc 以分号隔开的抄送的收件人的邮件地址列表
★Subject 电子邮件的主题
★Body 电子邮件的正文
★BodyFormat 电子邮件的正文内容类型,由MailFormat枚举值指定,MailFormat.Text或MailFormat.Html
★Attachments 电子邮件附件集合
★Priority 电子邮件的优先级,由MailPriority枚举值指定,可以是MailPriority.Low ,MailPriority.Normal或MailPriority.High三者之一
2、Attachment用来构造电子邮件附件.用此类构造了电子邮件附件然后添加到MailMessage对象的Attachments集合即可
3、使用SmtpMail类发送电子邮件,可以通过系统本身的SMTP邮件服务或者其它SMTP服务器来发送,发送电子邮件首先需要设置SmtpMail类的SmtpServer属性,然后使用Send方法发送就可以了
下面做个简单的邮件发送例子:
using System.Web.Mail;
/// <summary>
/// 给业务面试人发送面试通知邮件
/// </summary>
/// <returns>true/false</returns>
//发送邮件
bool sendmailbusi()
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.From = "aminta@sohu.com";
mail.To = "123@sohu.com";
mail.Subject = "测试邮件";
mail.Body = "这是一封测试邮件";
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码
mail.Headers.Add("Disposition-Notification-To", "aminta@sohu.com"); //邮件阅读回执
//构造添加附件(可以发多个附件给多个收件人)
System.Web.Mail.MailAttachment mailAttach_1 = new System.Web.Mail.MailAttachment(@"E:上岗考核表-入职.doc");
System.Web.Mail.MailAttachment mailAttach_2 = new System.Web.Mail.MailAttachment(@"E:填表说明.doc");
System.Web.Mail.MailAttachment mailAttach_3 = new System.Web.Mail.MailAttachment(@"E:上岗考核表-转正.doc");
mail.Attachments.Add (mailAttach_1);
mail.Attachments.Add(mailAttach_2);
mail.Attachments.Add(mailAttach_3);
System.Web.Mail.SmtpMail.SmtpServer = "smtp.sohu.com"; // 发送邮件服务器端口
//验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //是否需要验证,一般是要的
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "aminta"); //自己邮箱的用户名
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456"); //自己邮箱的密码
try
{
System.Web.Mail.SmtpMail.Send(mail);
return true;
}
catch
{
return false;
}
}
// 05的应该是:
//System.Net.Mail //有的邮件服务器支持,有的不支持
例如:
//发送邮件(含附件)
bool sendmail(string mailto,string mailfrom,string content,string loginname,string psd)
{
System.Net.Mail.MailMessage mail = new
System.Net.Mail.MailMessage();
mail.From = new System.Net.Mail.MailAddress(mailfrom.Trim());
mail.To.Add(mailto.Trim());
mail.Subject = "xx公司面试通知函";
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.UTF8;
//附件
string strFilePath = @"E:logo.jpg";
System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(strFilePath);//添加附件
attachment1.Name = System.IO.Path.GetFileName(strFilePath);
attachment1.NameEncoding = System.Text.Encoding.GetEncoding("gb2312");
attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
attachment1.ContentDisposition.Inline = true;
attachment1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = attachment1.ContentId;//关键性的地方,这里得到一个id数值
mail.Attachments.Add(attachment1);
//邮件正文
mail.Body
="<table width='100%'><tr><td><img src
='cid:"+cid+"'/></td></tr>" +content.Trim();
mail.Headers.Add("Disposition-Notification-To", "aminta@sohu.com"); //邮件阅读回执
System.Net.Mail.SmtpClient server = new System.Net.Mail.SmtpClient("mail.xxxx.xxx"); //之前一不留神把公司域名挂上了,在头儿的提醒下赶紧改了,不然惨了,^0^
server.Credentials = new System.Net.NetworkCredential(loginname.Trim(), psd.Trim()); //用户名和密码
try
{
server.Send(mail);
return true;
}
catch
{
return false;
}
}
转自:http://www.cnblogs.com/aminta/archive/2008/11/08/1281357.html