郵件發送類:html
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;服務器
/// <summary>
/// sendmail 的摘要說明
/// </summary>
namespace donny
{
public struct EmailParms
{
/// <summary>
/// 收件人郵箱地址
/// </summary>
public string ToEmailAddress;ide
/// <summary>
/// 發件人郵箱地址
/// </summary>
public string FromEmailAddress;函數
/// <summary>
/// 發件人郵箱密碼
/// </summary>
public string FromEmailPassword;this
/// <summary>
/// 郵件主題
/// </summary>
public string EmailSubject;編碼
/// <summary>
/// 郵件內容
/// </summary>
public string EmailBody;加密
/// <summary>
/// 發件人姓名
/// </summary>
public string EmailPersonName;spa
/// <summary>
/// SMTP主機名稱
/// 例:Gmail爲smtp.gmail.com
/// </summary>
public string EmailHostName;.net
/// <summary>
/// 郵件優先級:high(高)、low(低)、normal(正常)
/// 默認爲high
/// </summary>
public string EmailPriority;orm
/// <summary>
/// 郵箱端口號
/// 例:Gmail爲587,通常爲25
/// </summary>
public int EmailPort;
/// <summary>
/// 郵件是否加密:true(加密),false(不加密)
/// 默認爲true
/// </summary>
public bool isEnableSsl;
/// <summary>
/// 郵件內容是否爲HTML格式(true加密,false不加密),默認爲false
/// </summary>
public bool isBodyHtml;
/// <summary>
/// 郵件編碼類型:UTF七、UTF8(推薦)、UTF3二、ASCII和Default.
/// </summary>
public string EncodingType;
}
public class sendmail
{
public sendmail()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public static bool SendingEmail(EmailParms Email)
{
System.Net.Mail.MailMessage mms = new System.Net.Mail.MailMessage();
System.Text.Encoding emaiEncodingType;
//設置郵件編碼類型
switch (Email.EncodingType)
{
case "UTF7":
emaiEncodingType = System.Text.Encoding.UTF7;
break;
case "UTF8":
emaiEncodingType = System.Text.Encoding.UTF8;
break;
case "UTF32":
emaiEncodingType = System.Text.Encoding.UTF32;
break;
case "ASCII":
emaiEncodingType = System.Text.Encoding.ASCII;
break;
default:
emaiEncodingType = System.Text.Encoding.Default;
break;
}
mms.To.Add(Email.ToEmailAddress);
mms.From = new System.Net.Mail.MailAddress(Email.FromEmailAddress, Email.EmailPersonName, emaiEncodingType);
mms.Subject = Email.EmailSubject;
mms.SubjectEncoding = emaiEncodingType;
mms.Body = Email.EmailBody;
mms.BodyEncoding = emaiEncodingType;
//設置郵件是否爲HTML格式
if (Email.isBodyHtml == false)
{
mms.IsBodyHtml = Email.isBodyHtml;
}
else
{
mms.IsBodyHtml = true;
}
//設置郵件優級先級
switch (Email.EmailPriority)
{
case "normal":
mms.Priority = System.Net.Mail.MailPriority.Normal;
break;
case "low":
mms.Priority = System.Net.Mail.MailPriority.Low;
break;
default:
mms.Priority = System.Net.Mail.MailPriority.High;
break;
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Credentials = new System.Net.NetworkCredential(Email.FromEmailAddress, Email.FromEmailPassword);
client.Port = Email.EmailPort;
client.Host = Email.EmailHostName;
client.EnableSsl = Email.isEnableSsl;
try
{
client.Send(mms);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}
使用方法:
EmailParms mailstr = new EmailParms();
mailstr.EmailBody = "aaaa";
mailstr.EmailHostName = "smtp.XXXX.com";
mailstr.EmailPersonName = "admin";
mailstr.EmailPort = 25;
mailstr.EmailPriority = "high";
mailstr.EmailSubject = "主題";
mailstr.EncodingType = "UTF8";
mailstr.FromEmailAddress = "a@a.com";
mailstr.FromEmailPassword = "1111111";
mailstr.isBodyHtml = false;
mailstr.isEnableSsl = false;
mailstr.ToEmailAddress = "b@b.com";
sendmail.SendingEmail(mailstr);
使用時候注意:
有時候防火牆會攔截郵件。須要在防火牆裏設置端口開放,用那個就開發那個。不要頻繁大範圍的發送郵件,負責機房很快就會把你服務器的相關端口給封閉的。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/Donny_zhang/archive/2009/01/13/3769454.aspx