MailMessage
提供屬性和方法來建立一個郵件訊息物件。通常可以先構建好MailMessage物件,然後設定它的屬性的方式來構建郵件程式。
常用的屬性:
From -- 傳送郵件的地址
To -- 接受郵件的地址
Subject -- 郵件的標題
Priority -- 郵件的優先順序(有效值為High,Low,Normal)
Attachments -- 返回一個集合,代表附件
Bcc -- 密送地址
Cc -- 抄送地址
Body -- 獲取或是設定電子郵件訊息的內容
BodyFormat -- 獲取或是設定MailFormat的列舉值,此值指定訊息體郵件的格式(Html格式、Text格式)
Bodyencoding -- 指定訊息的編碼方式編碼(主要有Base64,UUencode)
UrlContentBase :在HTML格式郵件中的URL編碼方式
UrlContentLocation:郵件資訊的優先順序(High, Medium,Low)
SmtpMail
負責傳送郵件的SMTP協議,提供屬性和方法透過使用windows 2000 CDOSYS 的訊息元件的聯合資料物件來發送郵件訊息。
SmtpMail類用到的是Send方法,它的目的就是傳送郵件,有兩個過載方法。
1. SmtpMail.Send("傳送郵件的地址","接受郵件的地址","郵件的標題","郵件訊息的內容") 這個方法很簡單,不適合傳送帶附件的郵件。
MailAttachment
與郵件附件有關的物件類,主要用來提供屬性和方法來建立一個郵件附件物件。
建構函式
建立一個附件物件
MailAttachment objMailAttachment = new MailAttachment( "d:\test。txt" );//傳送郵件的附件
呼叫形式
MailMessage objMailMessage= new MailMessage();
objMailMessage.Attachments.Add( objMailAttachment );//將附件附加到郵件訊息物件中
封裝的郵件傳送類
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;
using System.Net.Mail;
using System.Text;
public class SendMail
...{
public SendMail()
}
private string _host;
/**//// <summary>
/// 伺服器地址
/// </summary>
public string Host
get ...{ return _host; }
set ...{ _host = value; }
private int _port;
/// 伺服器埠
public int Port
get ...{ return _port; }
set ...{ _port = value; }
private string _smtpUsername;
/// 傳送人郵箱使用者名稱
public string SmtpUsername
get ...{ return _smtpUsername; }
set ...{ _smtpUsername = value; }
private string _sendemail;
/// 傳送人郵箱帳號(只接收加密串,發郵件時解密)
public string SendEmail
get
return _sendemail;
set ...{ _sendemail = value; }
private string _replyToEmail;
/// 回覆人郵箱賬號
public string ReplyToEmail
get ...{ return _replyToEmail; }
set ...{ _replyToEmail = value; }
private string _replyUserName;
/// 回覆人使用者名稱
public string ReplyUserName
get ...{ return _replyUserName; }
set ...{ _replyUserName = value; }
private string _getemail;
/// 收件人郵箱帳號
public string GetEmail
get ...{ return _getemail; }
set ...{ _getemail = value; }
private string _smtpPassword;
/// 傳送人郵箱密碼(只接收加密串,發郵件時解密)
public string SmtpPassword
get ...{ return _smtpPassword; }
set ...{ _smtpPassword = value; }
private string _content;
/// 郵件內容
public string Content
get ...{ return _content; }
set ...{ _content = value; }
private string _title;
/// 郵件標題
public string Title
get ...{ return _title; }
set ...{ _title = value; }
private string[] _cc = null;
/// 抄送郵箱
public string[] cc
get ...{ return _cc; }
set ...{ _cc = value; }
private string[] _bcc = null;
/// 密送郵箱
public string[] bcc
get ...{ return _bcc; }
set ...{ _bcc = value; }
///傳送郵件
/// <returns>返回是否成功</returns>
public bool Send()
try
MailMessage objMailMessage;
objMailMessage = new MailMessage(SendEmail, _getemail, _title, _content);
if (!string.IsNullOrEmpty(_replyToEmail) && !string.IsNullOrEmpty(_replyUserName))
MailAddress reply = new MailAddress(_replyToEmail, _replyUserName);
objMailMessage.ReplyToList.Add(reply);
objMailMessage.BodyEncoding = Encoding.GetEncoding(936);
objMailMessage.IsBodyHtml = true;
if (cc != null && cc.Length > 0)
foreach (string ccAddress in cc)
objMailMessage.CC.Add(new MailAddress(ccAddress));
if (bcc != null && bcc.Length > 0)
foreach (string bccAddress in bcc)
objMailMessage.Bcc.Add(new MailAddress(bccAddress));
SmtpClient client = new SmtpClient(this._host, this._port);
if (!String.IsNullOrEmpty(this.SmtpUsername) && !String.IsNullOrEmpty(this.SmtpPassword))
client.Credentials = new NetworkCredential(this.SmtpUsername, this.SmtpPassword);
client.EnableSsl = false;
client.Send(objMailMessage);
objMailMessage.Dispose();
return true;
catch
return false;
MailMessage
提供屬性和方法來建立一個郵件訊息物件。通常可以先構建好MailMessage物件,然後設定它的屬性的方式來構建郵件程式。
常用的屬性:
From -- 傳送郵件的地址
To -- 接受郵件的地址
Subject -- 郵件的標題
Priority -- 郵件的優先順序(有效值為High,Low,Normal)
Attachments -- 返回一個集合,代表附件
Bcc -- 密送地址
Cc -- 抄送地址
Body -- 獲取或是設定電子郵件訊息的內容
BodyFormat -- 獲取或是設定MailFormat的列舉值,此值指定訊息體郵件的格式(Html格式、Text格式)
Bodyencoding -- 指定訊息的編碼方式編碼(主要有Base64,UUencode)
UrlContentBase :在HTML格式郵件中的URL編碼方式
UrlContentLocation:郵件資訊的優先順序(High, Medium,Low)
SmtpMail
負責傳送郵件的SMTP協議,提供屬性和方法透過使用windows 2000 CDOSYS 的訊息元件的聯合資料物件來發送郵件訊息。
SmtpMail類用到的是Send方法,它的目的就是傳送郵件,有兩個過載方法。
1. SmtpMail.Send("傳送郵件的地址","接受郵件的地址","郵件的標題","郵件訊息的內容") 這個方法很簡單,不適合傳送帶附件的郵件。
MailAttachment
與郵件附件有關的物件類,主要用來提供屬性和方法來建立一個郵件附件物件。
建構函式
建立一個附件物件
MailAttachment objMailAttachment = new MailAttachment( "d:\test。txt" );//傳送郵件的附件
呼叫形式
MailMessage objMailMessage= new MailMessage();
objMailMessage.Attachments.Add( objMailAttachment );//將附件附加到郵件訊息物件中
封裝的郵件傳送類
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;
using System.Net.Mail;
using System.Text;
public class SendMail
...{
public SendMail()
...{
}
private string _host;
/**//// <summary>
/// 伺服器地址
/// </summary>
public string Host
...{
get ...{ return _host; }
set ...{ _host = value; }
}
private int _port;
/**//// <summary>
/// 伺服器埠
/// </summary>
public int Port
...{
get ...{ return _port; }
set ...{ _port = value; }
}
private string _smtpUsername;
/**//// <summary>
/// 傳送人郵箱使用者名稱
/// </summary>
public string SmtpUsername
...{
get ...{ return _smtpUsername; }
set ...{ _smtpUsername = value; }
}
private string _sendemail;
/**//// <summary>
/// 傳送人郵箱帳號(只接收加密串,發郵件時解密)
/// </summary>
public string SendEmail
...{
get
...{
return _sendemail;
}
set ...{ _sendemail = value; }
}
private string _replyToEmail;
/**//// <summary>
/// 回覆人郵箱賬號
/// </summary>
public string ReplyToEmail
...{
get ...{ return _replyToEmail; }
set ...{ _replyToEmail = value; }
}
private string _replyUserName;
/**//// <summary>
/// 回覆人使用者名稱
/// </summary>
public string ReplyUserName
...{
get ...{ return _replyUserName; }
set ...{ _replyUserName = value; }
}
private string _getemail;
/**//// <summary>
/// 收件人郵箱帳號
/// </summary>
public string GetEmail
...{
get ...{ return _getemail; }
set ...{ _getemail = value; }
}
private string _smtpPassword;
/**//// <summary>
/// 傳送人郵箱密碼(只接收加密串,發郵件時解密)
/// </summary>
public string SmtpPassword
...{
get ...{ return _smtpPassword; }
set ...{ _smtpPassword = value; }
}
private string _content;
/**//// <summary>
/// 郵件內容
/// </summary>
public string Content
...{
get ...{ return _content; }
set ...{ _content = value; }
}
private string _title;
/**//// <summary>
/// 郵件標題
/// </summary>
public string Title
...{
get ...{ return _title; }
set ...{ _title = value; }
}
private string[] _cc = null;
/**//// <summary>
/// 抄送郵箱
/// </summary>
public string[] cc
...{
get ...{ return _cc; }
set ...{ _cc = value; }
}
private string[] _bcc = null;
/**//// <summary>
/// 密送郵箱
/// </summary>
public string[] bcc
...{
get ...{ return _bcc; }
set ...{ _bcc = value; }
}
/**//// <summary>
///傳送郵件
/// </summary>
/// <returns>返回是否成功</returns>
public bool Send()
...{
try
...{
MailMessage objMailMessage;
objMailMessage = new MailMessage(SendEmail, _getemail, _title, _content);
if (!string.IsNullOrEmpty(_replyToEmail) && !string.IsNullOrEmpty(_replyUserName))
...{
MailAddress reply = new MailAddress(_replyToEmail, _replyUserName);
objMailMessage.ReplyToList.Add(reply);
}
objMailMessage.BodyEncoding = Encoding.GetEncoding(936);
objMailMessage.IsBodyHtml = true;
if (cc != null && cc.Length > 0)
...{
foreach (string ccAddress in cc)
...{
objMailMessage.CC.Add(new MailAddress(ccAddress));
}
}
if (bcc != null && bcc.Length > 0)
...{
foreach (string bccAddress in bcc)
...{
objMailMessage.Bcc.Add(new MailAddress(bccAddress));
}
}
SmtpClient client = new SmtpClient(this._host, this._port);
if (!String.IsNullOrEmpty(this.SmtpUsername) && !String.IsNullOrEmpty(this.SmtpPassword))
...{
client.Credentials = new NetworkCredential(this.SmtpUsername, this.SmtpPassword);
}
client.EnableSsl = false;
client.Send(objMailMessage);
objMailMessage.Dispose();
return true;
}
catch
...{
return false;
}
}
}