c#應用system.net發送html格局郵件。本站提示廣大學習愛好者:(c#應用system.net發送html格局郵件)文章只能為提供參考,不一定能成為您想要的結果。以下是c#應用system.net發送html格局郵件正文
using System;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace LeeStudio.Basic
{
/// <summary>
/// 郵件發送類
/// </summary>
public class SendEmail
{
private MailMessage mailMessage = new MailMessage(); //實例化一個郵件類
#region 結構函數
/// <summary>
/// 結構函數
/// </summary>
/// <param name="toAddresses">收件人地址(多個以,號離開)</param>
/// <param name="fromAddress">發件人地址</param>
/// <param name="title">主題</param>
/// <param name="body">注釋</param>
public SendEmail(string toAddresses, string fromAddress, string title, string body)
: this(toAddresses, fromAddress, "", "", title, body, false)
{
}
/// <summary>
/// 結構函數
/// </summary>
/// <param name="toAddress">收件人地址</param>
/// <param name="fromAddress">發件人地址</param>
/// <param name="toName">收件人名字</param>
/// <param name="fromName">發件人姓名</param>
/// <param name="title">主題</param>
/// <param name="body">注釋</param>
/// <param name="isBodyHtml">注釋能否為html格局</param>
public SendEmail(string toAddress, string fromAddress, string toName, string fromName, string title, string body, bool isBodyHtml)
{
mailMessage.From = new MailAddress(fromAddress, fromName, Encoding.GetEncoding(936));
if (toName.Equals(""))
mailMessage.To.Add(toAddress);
else
mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936)));
mailMessage.Subject = title;
mailMessage.SubjectEncoding = Encoding.GetEncoding(936);
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
mailMessage.BodyEncoding = Encoding.GetEncoding(936);
}
#endregion
/// <summary>
/// 設置SMTP,而且將郵件發送出去
/// 一切參數都設置完成後再挪用該辦法
/// </summary>
/// <param name="password">發件人暗碼</param>
/// <param name="smtpHost">SMTP辦事器地址</param>
public void SetSmtp(string password, string smtpHost)
{
SetSmtp(mailMessage.From.Address, password, smtpHost, 25, false, MailPriority.Normal);
}
/// <summary>
/// 設置SMTP,而且將郵件發送出去
/// 一切參數都設置完成後再挪用該辦法
/// </summary>
/// <param name="address">發件人地址(必需為真實有用的email地址)</param>
/// <param name="password">發件人暗碼</param>
/// <param name="smtpHost">SMTP辦事器地址</param>
/// <param name="smtpPort">SMTP辦事器的端口</param>
/// <param name="isEnableSsl">SMTP辦事器能否啟用SSL加密</param>
/// <param name="priority">郵件的優先級</param>
public void SetSmtp(string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority)
{
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(address, password);
smtp.Host = smtpHost;
smtp.Port = smtpPort;
smtp.EnableSsl = isEnableSsl;
mailMessage.Priority = priority;
smtp.Send(mailMessage); //發送郵件
}
#region //設置郵件地址
/// <summary>
/// 設置更多收件人
/// </summary>
/// <param name="toAddresses">收件人地址</param>
public void SetMoreToAddress(string toAddresses)
{
mailMessage.To.Add(toAddresses);
}
/// <summary>
/// 設置更多收件人
/// </summary>
/// <param name="toAddresses">收件人地址</param>
/// <param name="toName">收件人名字</param>
public void SetMoreToAddress(string toAddress, string toName)
{
mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936)));
}
/// <summary>
/// 設置抄送者(多個以,號離開)
/// </summary>
/// <param name="ccAddresses">抄送者地址</param>
public void SetCarbonCopyFor(string ccAddresses)
{
mailMessage.CC.Add(ccAddresses);
}
/// <summary>
/// 設置抄送者
/// </summary>
/// <param name="ccAddress">抄送者地址</param>
/// <param name="ccName">抄送者名字</param>
public void SetCarbonCopyFor(string ccAddress, string ccName)
{
mailMessage.Bcc.Add(new MailAddress(ccAddress, ccName, Encoding.GetEncoding(936)));
}
/// <summary>
/// 設置密送者(多個以,號離開)
/// </summary>
/// <param name="bccAddresses">密送者</param>
public void SetBlindCarbonCopyFor(string bccAddresses)
{
mailMessage.Bcc.Add(bccAddresses);
}
/// <summary>
/// 設置密送者
/// </summary>
/// <param name="bccAddress">密送者</param>
/// <param name="bccName">密送者名字</param>
public void SetBlindCarbonCopyFor(string bccAddress, string bccName)
{
mailMessage.Bcc.Add(new MailAddress(bccAddress, bccName, Encoding.GetEncoding(936)));
}
#endregion
#region 添加附件
/// <summary>
/// 添加附件(主動辨認文件類型)
/// </summary>
/// <param name="fileName">單個文件的途徑</param>
public void Attachments(string fileName)
{
mailMessage.Attachments.Add(new Attachment(fileName));
}
/// <summary>
/// 添加附件(默許為富文本RTF格局)
/// </summary>
/// <param name="fileName">單個文件的途徑</param>
public void AttachmentsForRTF(string fileName)
{
mailMessage.Attachments.Add(new Attachment(fileName, MediaTypeNames.Application.Rtf));
}
#endregion
}
}