2. SmtpMail.Send(MailMessage) 此方法复杂、灵活,适合发送附件,而且可以设置MailMessage对象的各种属性值。 如果我们用ASP.NET写一个邮件发送的程序,那么首先应该如何得到SMTP。有两种方法:第一种方法调用目前知名的邮件服务提供商的SMTP,比如新浪、搜狐、网易的免费电子邮箱的SMTP;第二种方法是自己装一个SMTP虚拟服务器,这个在安装IIS时一起装上去的。
MailAttachment
与邮件附件有关的对象类,主要用来提供属性和方法来创建一个邮件附件对象。
构造函数
创建一个附件对象
MailAttachment objMailAttachment = new MailAttachment( "d:\test。txt" );//发送邮件的附件
调用形式
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;
}
}
}