.NET Framework 类库
MailMessage 类
注意:此类在 .NET Framework 2.0 版中是新增的。
表示可以使用 SmtpClient 类发送的电子邮件。
命名空间:System.Net.Mail
程序集:System(在 system.dll 中)
语法
Public Class MailMessage
Implements IDisposable
Dim instance As MailMessage
public class MailMessage : IDisposable
public ref class MailMessage : IDisposable
public class MailMessage implements IDisposable
public class MailMessage implements IDisposable
备注
MailMessage 类的实例用于构造可使用 SmtpClient 类传输到 SMTP 服务器以便传递的电子邮件。
若要指定电子邮件的发件人、收件人和内容,请使用 MailMessage 类的关联属性。
使用 AlternateViews 属性指定一个电子邮件不同格式的副本。例如,如果您发送 HTML 格式的邮件,您可能希望同时提供邮件的纯文本格式,以防一些收件人使用的电子邮件阅读程序无法显示 HTML 内容。有关演示如何用替代视图创建邮件的示例,请参见 AlternateViews。
组织好电子邮件后,可以使用 Send 或 SendAsync 方法发送邮件。
示例
下面的代码示例演示如何创建和发送包括附件的电子邮件。
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = "data.xls";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane@contoso.com",
"ben@contoso.com",
"Quarterly data report.",
"See the attached spreadsheet.");
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
// Display the values in the ContentDisposition for the attachment.
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
data.Dispose();
}
线程安全
此类型的任何公共静态(Visual Basic 中的
Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0
定义
private void sendemail(string kano,string pwd)
{
MailMessage mymail=new MailMessage();
mymail.To=this.Session["Email"].ToString();
mymail.From="lfjdc001@163.com";
mymail.Subject=":您好!请查收您在天诚游戏网购买的点卡账号及密码!";
mymail.Body="点卡账号:"+kano+"点卡密码:"+pwd;
mymail.BodyFormat=MailFormat.Html;
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "lfjdc001" );
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "2881056" );
SmtpMail.SmtpServer ="smtp.163.com";
try
{
SmtpMail.Send(mymail);
this.Response.Write("<script>alert('点卡账号及密码已经发送到您的邮箱中,请注意查收!')</script>");
}
catch(System.Exception ex)
{
throw new Exception(ex.Message);
}
}
使用
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.Button1.Text=="返回首页")
{
this.Response.Redirect("default.aspx");
}
using(WebShop.Common common=new WebShop.Common())
{
DataSet ds=null;
ds=common.full("pro_selectusersbyemail",CommandType.StoredProcedure,"users",new SqlParameter("@email",this.txt_Email.Value.Trim()),new SqlParameter("@username",this.txt_LoginId.Value.Trim()));
if(ds.Tables[0].Rows.Count==1)
{
MailMessage mymail=new MailMessage();
mymail.To=this.txt_Email.Value.Trim();
mymail.From="lfjdc001@163.com";
mymail.Subject=this.txt_LoginId.Value.Trim()+":您好!请查收您的密码!";
if(this.ViewState["pass"].ToString()=="0")
{
mymail.Body="您的登陆密码是:"+ds.Tables[0].Rows[0][2].ToString();
}
else
{
mymail.Body="您的支付密码是:"+ds.Tables[0].Rows[0][3].ToString();
}
mymail.BodyFormat=MailFormat.Html;
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "lfjdc001" );
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "2881056" );
SmtpMail.SmtpServer ="smtp.163.com";
try
{
SmtpMail.Send(mymail);
this.Response.Write("<script>alert('您的密码已经发送到您的邮箱中,请注意查收!')</script>");
this.Button1.Text="返回首页";
}
catch(System.Exception ex)
{
throw new Exception(ex.Message) ;
}
}
else
{
this.Response.Write("<script>alert('您的用户名或邮箱不正确!')</script>");
}
}
}