Mail对象
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
class MailObject
{
private string _MailFrom = "";
private string _MailTo = "";
private string _MailCC = "";
private string _Subject = "";
private string _MailBody = "";
private string[] _Attachments = null;
private bool _IsHtmlMail = false;
private string _POP3 = "";
private string _SMTP = "";
private string _UserID = "";
private string _PassWord = "";
private string _MailEncoding = "GB2312";
private System.Net.Mail.MailPriority _Priority = MailPriority.Normal;
/// <summary>
/// The Mail Come From sb.
/// </summary>
public string MailFrom
{
get { return _MailFrom; }
set { _MailFrom = value; }
}
/// <summary>
/// Send Mail To sb.
/// </summary>
public string MailTo
{
get { return _MailTo; }
set { _MailTo = value; }
}
/// <summary>
/// CC Mail To sb.
/// </summary>
public string MailCC
{
get { return _MailCC; }
set { _MailCC = value; }
}
/// <summary>
/// Subject
/// </summary>
public string Subject
{
get { return _Subject; }
set { _Subject = value; }
}
/// <summary>
/// The Body of Mail
/// </summary>
public string MailBody
{
get { return _MailBody; }
set { _MailBody = value; }
}
/// <summary>
/// Attachments
/// </summary>
public string[] Attachments
{
get { return _Attachments; }
set { _Attachments = value; }
}
/// <summary>
/// IsHtmlMail
/// </summary>
public bool IsHtmlMail
{
get { return _IsHtmlMail; }
set { _IsHtmlMail = value; }
}
/// <summary>
/// POP3
/// </summary>
public string POP3
{
get { return _POP3; }
set { _POP3 = value; }
}
/// <summary>
/// SMTP
/// </summary>
public string SMTP
{
get { return _SMTP; }
set { _SMTP = value; }
}
/// <summary>
/// UserID
/// </summary>
public string UserID
{
get { return _UserID; }
set { _UserID = value; }
}
/// <summary>
/// PassWord
/// </summary>
public string PassWord
{
get { return _PassWord; }
set { _PassWord = value; }
}
/// <summary>
/// MailEncoding
/// </summary>
public string MailEncoding
{
get { return _MailEncoding; }
set { _MailEncoding = value; }
}
/// <summary>
/// MailPriority
/// </summary>
public System.Net.Mail.MailPriority Priority
{
get { return _Priority; }
set { _Priority = value; }
}
}
发送Mail的方法.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static void SendEmail(MailObject mailObj)
{
//smtp
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(mailObj.SMTP);
//Credentials
smtp.Credentials = new NetworkCredential(mailObj.UserID, mailObj.PassWord);
//MailMessage
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.Subject = mailObj.Subject;
message.SubjectEncoding = System.Text.Encoding.GetEncoding(mailObj.MailEncoding);
message.BodyEncoding = System.Text.Encoding.GetEncoding(mailObj.MailEncoding);
//From
message.From = new System.Net.Mail.MailAddress(mailObj.MailFrom);
//Priority
message.Priority = mailObj.Priority;
//IsBodyHtml
message.IsBodyHtml = mailObj.IsHtmlMail;
//Body
message.Body = mailObj.MailBody;
//To
message.To.Add(mailObj.MailTo);
//cc
if (mailObj.MailCC != null && mailObj.MailCC != string.Empty)
{
//cc
message.CC.Add(mailObj.MailCC);
}
if (mailObj.Attachments != null && mailObj.Attachments.Length > 0)
{
foreach (string attachFile in mailObj.Attachments)
{
Attachment data = new Attachment(attachFile, MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
}
}
//SendCompleted
smtp.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
object userState = message;
smtp.SendAsync(message, userState);
}