System.Net.Mail 傳送郵件
主要是用到了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 = “[email protected]“;
mail.To = “[email protected]“;
mail.Subject = “
mail.Body = “這是一封測試郵件”;
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.BodyEncoding = System.Text.Encoding.UTF8; //郵件內容編碼
//構造新增附件(可以發多個附件給多個收件人)
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的應該是:
例如:
//傳送郵件(含附件)
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();
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;
}
}