2. SmtpMail.Send(MailMessage) 此方法復雜、靈活,適合發(fā)送附件,而且可以設置MailMessage對象的各種屬性值。 如果我們用ASP.NET寫一個郵件發(fā)送的程序,那么首先應該如何得到SMTP。有兩種方法:第一種方法調用目前知名的郵件服務提供商的SMTP,比如新浪、搜狐、網(wǎng)易的免費電子郵箱的SMTP;第二種方法是自己裝一個SMTP虛擬服務器,這個在安裝IIS時一起裝上去的。
MailAttachment
與郵件附件有關的對象類,主要用來提供屬性和方法來創(chuàng)建一個郵件附件對象。
構造函數(shù)
創(chuàng)建一個附件對象
MailAttachment objMailAttachment = new MailAttachment( "d:\test。txt" );//發(fā)送郵件的附件
調用形式
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>
/// 發(fā)送人郵箱用戶名
/// /summary>
public string SmtpUsername
...{
get ...{ return _smtpUsername; }
set ...{ _smtpUsername = value; }
}
private string _sendemail;
/**//// summary>
/// 發(fā)送人郵箱帳號(只接收加密串,發(fā)郵件時解密)
/// /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>
/// 發(fā)送人郵箱密碼(只接收加密串,發(fā)郵件時解密)
/// /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>
///發(fā)送郵件
/// /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;
}
}
}