下面是vs2008中写的代码,测试成功,可以根据自己的需要稍作修改。如果是你是一的是.NET2.0,是不支持自带属性的,要把属性部分代码修改一下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace ISACA.Services.Common
{
public partial class MailEntity
{
/// <summary>
/// default constructor
/// </summary>
public MailEntity(){}
/// <summary>
/// Mail Entity constructor using 'GB2312' as default Encoding/timeout is 999999/IsHtmlBody is default as true
/// </summary>
/// <param name="mailFrom">the email address that will send the email</param>
/// <param name="userName">the name of the user who send the email</param>
/// <param name="pwd">the password of the user who send the email</param>
/// <param name="displaySenderName">the sender name</param>
/// <param name="mailTo">the receiver email address</param>
/// <param name="displayReceiverName">the receiver name</param>
/// <param name="subject">the subject of the email</param>
/// <param name="contentBody">the content of the email</param>
/// <param name="host">the host address that send email</param>
public MailEntity(string mailFrom, string userName, string pwd, string displaySenderName, string mailTo, string displayReceiverName, string subject, string contentBody, string host)
{
TimeOut = 999999;
Encoding = Encoding.GetEncoding("GB2312"); ;
MailFrom = mailFrom;
UserName = userName;
Pwd = pwd;
DisplaySenderName = displaySenderName;
MailTo = mailTo;
DisplayReceiverName = displayReceiverName;
Subject = subject;
ContentBody = contentBody;
Host = host;
IsBodyHtml = true;
}
/// <summary>
/// Mail Entity constructor
/// </summary>
/// <param name="timeOut">the email send timeout</param>
/// <param name="encoding">the Encoding type that will edcode the email content</param>
/// <param name="mailFrom">the email address that will send the email</param>
/// <param name="userName">the name of the user who send the email</param>
/// <param name="pwd">the password of the user who send the email</param>
/// <param name="displaySenderName">the sender name</param>
/// <param name="mailTo">the receiver email address</param>
/// <param name="displayReceiverName">the receiver name</param>
/// <param name="subject">the subject of the email</param>
/// <param name="contentBody">the content of the email</param>
/// <param name="host">the host address that send email</param>
/// <param name="isBodyHtml">if the content of the email is Html</param>
public MailEntity(System.Int32 timeOut, System.Text.Encoding encoding, string mailFrom, string userName, string pwd, string displaySenderName, string mailTo, string displayReceiverName, string subject, string contentBody, string host, System.Boolean isBodyHtml)
{
TimeOut = timeOut;
Encoding = encoding;
MailFrom = mailFrom;
UserName = userName;
Pwd = pwd;
DisplaySenderName = displaySenderName;
MailTo = mailTo;
DisplayReceiverName = displayReceiverName;
Subject = subject;
ContentBody = contentBody;
Host = host;
IsBodyHtml = isBodyHtml;
}
/// <summary>
/// Send mail
/// </summary>
/// <param name="mailEntity">MailEntity object</param>
/// <returns></returns>
public bool SendMail(MailEntity mailEntity)
{
try
{
Encoding encoding = mailEntity.Encoding;
MailMessage mailMessage = new MailMessage(new MailAddress(mailEntity.MailFrom, mailEntity.DisplaySenderName, encoding),
new MailAddress(mailEntity.MailTo, mailEntity.DisplayReceiverName, encoding));
mailMessage.SubjectEncoding = encoding;
mailMessage.Subject = mailEntity.Subject;
mailMessage.IsBodyHtml = mailEntity.IsBodyHtml;
mailMessage.Body = mailEntity.ContentBody;
SmtpClient smtpClient = new SmtpClient(mailEntity.Host);
smtpClient.Credentials = new NetworkCredential(mailEntity.UserName, mailEntity.Pwd);
smtpClient.Timeout = mailEntity.TimeOut;
smtpClient.Send(mailMessage);
return true;
}
catch
{
return false;
}
}
#region Properties
/// <summary>
/// the email send timeout
/// </summary>
public Int32 TimeOut { set; get; }
/// <summary>
/// the email address that will send the email
/// </summary>
public String MailFrom { set; get; }
/// <summary>
/// the email address that will receive the email
/// </summary>
public String MailTo { set; get; }
/// <summary>
/// the Encoding type that will edcode the email content
/// </summary>
public Encoding Encoding { set; get; }
/// <summary>
/// the name of the user who send the email
/// </summary>
public String UserName { set; get; }
/// <summary>
/// the password of the user who send the email
/// </summary>
public string Pwd { get; set; }
/// <summary>
/// the sender name that will display in the sender item
/// </summary>
public String DisplaySenderName { set; get; }
/// <summary>
/// the receiver name that will display in the receiver item
/// </summary>
public String DisplayReceiverName { set; get; }
/// <summary>
/// mail content
/// </summary>
public String ContentBody { set; get; }
/// <summary>
/// mail subject
/// </summary>
public String Subject { set; get; }
/// <summary>
/// mail host address
/// </summary>
public String Host { set; get; }
/// <summary>
/// if the mail body is in html format
/// </summary>
public Boolean IsBodyHtml { set; get; }
#endregion
}
}