MailMessage 类

本文介绍.NET Framework中的MailMessage类,该类用于构造可通过SmtpClient类发送的电子邮件。文章提供了构造邮件并添加附件的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  欢迎您  |  登录   
中国 - 简体中文  Dropdown Arrow
 | 
MSDN
class="tocFrame" id="tocIFrame" src="/zh-cn/library/system.net.mail.mailmessage(vs.80,d=toc).aspx" frameborder="0" onload="TocLoader_HideMessage();">
         
类库参考
MailMessage 类 (System.Net...
.NET Framework 类库 
MailMessage 类 

注意:此类在 .NET Framework 2.0 版中是新增的。

表示可以使用 SmtpClient 类发送的电子邮件。

命名空间:System.Net.Mail
程序集:System(在 system.dll 中)

语法语法
Visual Basic(声明)
Public Class MailMessage
    Implements IDisposable
Visual Basic(用法)
Dim instance As MailMessage
C#
public class MailMessage : IDisposable
C++
public ref class MailMessage : IDisposable
J#
public class MailMessage implements IDisposable
JScript
public class MailMessage implements IDisposable
备注备注

MailMessage 类的实例用于构造可使用 SmtpClient 类传输到 SMTP 服务器以便传递的电子邮件。

若要指定电子邮件的发件人、收件人和内容,请使用 MailMessage 类的关联属性。

 

邮件部分

属性

发件人

From

收件人

To

抄送 (CC)

CC

密件抄送 (BCC)

Bcc

附件

Attachments

主题

Subject

邮件正文

Body

使用 AlternateViews 属性指定一个电子邮件不同格式的副本。例如,如果您发送 HTML 格式的邮件,您可能希望同时提供邮件的纯文本格式,以防一些收件人使用的电子邮件阅读程序无法显示 HTML 内容。有关演示如何用替代视图创建邮件的示例,请参见 AlternateViews

组织好电子邮件后,可以使用 SendSendAsync 方法发送邮件。

示例示例

下面的代码示例演示如何创建和发送包括附件的电子邮件。

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();
}
继承层次结构继承层次结构
System.Object
   System.Net.Mail.MailMessage
线程安全线程安全
此类型的任何公共静态(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

©2006 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明

 

 

定义

 

 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>");
    }
   }
  
  }

 
Page view tracker
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值