using System.Web.Mail;...
pulbic class MyEmailService
{
 .....
   public void SendMail(string from, string to, string bcc, string subject, string body)
  {
      MailMessage mail=new MailMessage();
      mail.From=from;
      mail.To=to;
     if( bcc!=string.Empty)
mail.Bcc=bcc;
     mail.Subject=subject;
     mail.Body=body;
     SmtpMail.SmtpServer=smtpServer; //smtpServer是你的smtp的邮件服务器地址
     try
    {
SmtpMail.Send(mail);
     }
     catch
     {
//配置错误处理
     }
  }
  ...
}//以上代码是我项目中的片断,供参考

解决方案 »

  1.   

    在这篇文章中,我们将演示如何编写asp.net来发送emails。这将要讨论到诸如email 格式,优先级别,附件和邮件加密。
          Asp.net通过使用SmtpMail对象来发送邮件。SmtpMail对象发送邮件是通过以下几步来完成的。
          # 导入与mail相关的名称空间。
          # 建立一个message对象并设置属性。
          # 通过SmtpMail对象实例的'Send'方法来发送邮件。      现在让我们一步步的来了解从asp.net页面发送邮件的过程。
    一. 导入名称空间
          在asp.net页面中导入System.Web.Util名称空间,这个名称空间包含了所有发送邮件所必须的对象。这些对象是:
    对象 简介 
    SmtpMail 声明邮件系统(mail system)来发送邮件。 
    MailMessage 声明一个消息(message),包含发送地址,接收地址等属性。 
    MailFormat 声明消息的格式-Html,Text 等等。 
    MailAttachment 声明邮件的附件。 
    MailEncoding enum 声明加密方式:Base64 or UUencode.  
    MailPriority enum 设置邮件的优先级别:值:高,底,一般 
    <% @Import Namespace = "System.Web.Util" %> 二。初始化MailMessage 对象
           使用以下语句可以实现初始化MailMessage对象。
           Dim mailObj AS new MailMessage 
           MailMessage对象具有以下属性。属性 描述 
    From 发送方的email地址 
    To 接收方的email地址 
    Subject Email的主题 
    Body Email的内容 
    CC List of recipients copied on the email 
    BCC List of recipients blind-copied on the email 
    Priority 邮件的优先级别
    高,底,一般 
    BodyEncoding 加密邮件的内容
    Base64 或 UUencode 
    BodyFormat 内容的格式
    Html 或文本 
    Attachments 附件列表 以下几行代码演示了如何使用MailMessage对象的属性的方法。他描述了我们将要创建的消息Message, 而这个消息(message)是用SmtpMail对象来发送的。在我们的这个例子中,mailObj就是MailMeessage对象的实例。 mailObj.From = "[email protected]"
    mailObj.To = Request.Form ("to")
    mailObj.Subject = "subject of the mail"
    mailObj.Body = "Message of the mail"三。发送邮件
          最后通过SmtpMail对象的'send'方法来发送邮件。下面的代码就是用来发送邮件的。
    SmtpMail.Send(mailObj)
    最后我们把以上的这些应用到一个完整的例子当中来。
    下面是用Asp.net+C#编写的。<%@page language="C#" %>
    <%@Import Namespace="System.Web.Util" %>
    <HTML><BODY>
    <SCRIPT LANGUAGE="C#" RUNAT="server"> 
    // This method is called on the server when the submit
    // button is clicked on the client and when the page
    // posts back to itself 
    public void SendMail (Object Obj, EventArgs E)
    {
      
      MailMessage mailObj = new MailMessage();  // 设置email的'from'和'to'的地址
      mailObj.From = Request.Form("From");
      mailObj.To = Request.Form("To");  mailObj.Subject = "Subject Of the Mail";
      mailObj.Body = "Body of the Mail";  // 可选: 使用html格式的Email
      mailObj.BodyFormat = MailFormat.Html;  // 可选: 对邮件进行加密
      mailObj.BodyEncoding = MailFormat.Base64;  // 可选: 设置邮件的优先级别为高
      mailObj.Priority = MailPriority.High;  // 可选: 附件
      // 注意这里我们创建了一个MailAttachment对象来附加一个文件到email。
      mailObj.Attachments.Add(new MailAttachment("c:\\test.doc"));  // 使用SmtpMail对象来发送邮件。
      SmtpMail.Send(mailObj);
    }
    </SCRIPT><asp:label ID="Headingmsg" Text="Enter Your Email Address:" RUNAT="server"/><FORM METHOD="post" RUNAT="server">Email Recipient: <INPUT TYPE="text" NAME="to"> <br>
    Email Sender: <INPUT TYPE="text" NAME="from"><INPUT TYPE="submit" NAME="Submit" VALUE="Send Mail" RUNAT="server" OnServerClick="SendMail"></FORM>
    </BODY>
      

  2.   

    基于.NET的邮件解决方案第一次开始使用.net的System.Web.Mail消息组件,觉得很好用,而且简单。但后来发现.net自带的SmtpMail不支持SMTP认证,而且许多许多SMTP服务器不只需要登陆验证,还对MAIL From作了验证(比如263),如果和登陆用的用户名不符也不能发信。再有就是pop3邮件的信息解析问题,比如说解析出中文。 总结起来邮件问题也就2个问题:一是smtp的身份验证,二是pop3邮件的信息解析问题。 以上两个问题,也正是ms没有提供部分的核心问题。我也问过ms的专家,有关mail解决方案的问题,他们也没给我一个什么样答复。网上也有不少高手倾情奉献自己的源码,可上述的两个问题始终都没有(或我没在网上找到)。看有些coder们现在正在找这些,所以,还是把我的解决方案提供给大家,只是给大家一个参考。 用Jmail 组件:可以解决上述两个问题,Jmail组件(Ver 4.3)发送时提供身份验证,而且接收的信息解析问题也能得到很好解决! 以下是两个WEB Service 方法     '发送Email    <WebMethod()> Public Function Jmail_Send(ByVal fromEmailAddress As String, _                        ByVal toEmailAddress As String, _                        ByVal ccEmailAddress As String, _                        ByVal bccEmailAddress As String, _                        ByVal subject As String, _                        ByVal body As String, ByVal MailServer As String) As Boolean         Dim email As New jmail.Message()        email.Logging = True        email.Silent = True        '   email.MailServerUserName = ""  'smtp服务器用户名        '   email.MailServerPassWord = ""  'smtp服务器密码        email.From = fromEmailAddress        email.Subject = subject        email.Body = body        email.AddRecipient(toEmailAddress)        If email.Send(MailServer) Then            Return True        Else            Return False        End If    End Function     '接收Email    <WebMethod()> Public Function Jmail_Receivemail(ByVal UserName As String, _                        ByVal Password As String, ByVal MailServer As String) As Boolean         Dim email As New jmail.POP3()        Dim MSG As New jmail.Message()         email.Connect(UserName, Password, MailServer, 110)         If email.Count() > 0 Then            MSG = email.Messages.Item(1)             Debug.Write(MSG.FromName)            Debug.Write(MSG.Subject)            Debug.Write(MSG.Date)            Debug.Write(MSG.Body)            Return True        End If        email.Disconnect()    End Function  以上代码是利用Jmail发送、接收邮件代码的一个原形,大家可以在此基础上扩充。Jmail提供了强大的邮件发送及处理功能,你不用自己费太长的时间,就可以完成所有需要的功能,不是很好么?当然,你也可以自己写组件,接收邮件可以用Imports System.Convert来进行Base 64的处理,进行解码!但愿大家都能找到适合自己的解决方案!
      

  3.   

    谢谢信誉大哥,我也是使用了你说的方法,但是发送完成后,邮箱中并未有邮件,另外希望大虾们给一些在web下接收邮件的程序,我找了很多资料都是用windows窗体编的!万分感激!!!
     MailMessage aa=new MailMessage();
    private void Button1_Click(object sender, System.EventArgs e)
    {

    aa.To=ToTextBox.Text;
    aa.Cc=CcTextBox.Text;
    aa.Bcc=BccTextBox.Text;
    aa.From=FromTextBox.Text;
    aa.Subject=SubjectTextBox.Text;
    aa.Body=BodyTextBox.Text;
        //SmtpMail.SmtpServer="smtp.163.com";
    SmtpMail.Send(aa);

    }
    如果加了SmtpMail.SmtpServer="smtp.163.com"则报错
    “/NLFOFFICE2.0_1”应用程序中的服务器错误。
    --------------------------------------------------------------------------------The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available 源错误: 
    行 97:  aa.Body=TextBox8.Text;
    行 98:      “/NLFOFFICE2.0_1”应用程序中的服务器错误。
    --------------------------------------------------------------------------------The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available 源错误: 
    行 97:  aa.Body=TextBox8.Text;
    行 98:      SmtpMail.SmtpServer="smtp.163.com";
    行 99:  SmtpMail.Send(aa);
    行 100:
    行 101: }
     行 99:  SmtpMail.Send(aa);
    行 100:
    行 101: }
     
    如果不加SmtpMail.SmtpServer="smtp.163.com";则发送成功但是,邮箱中并没有邮件。
    希望大虾们指点一二!
      

  4.   

    如果是內部網發不出去,必須有SmtpServer
      

  5.   

    to pengweihua
    我已经安装了smtpservice ,我用cdo组件在windows窗体中可以发送,但是在web窗体中不行?
      

  6.   

    刚学.net时,试着做一个文章管理系统,第一次开始使用.net的System.Web.Mail消息组件,觉得很好用,而且简单。但后来发现.net自带的SmtpMail不支持SMTP认证,而且许多许多SMTP服务器不只需要登陆验证,还对MAIL From作了验证(比如263),如果和登陆用的用户名不符也不能发信,所以决定自己用C#写一个SendMail程序,其中参考了Huolx的一部分思想,取了他的一些优点。 
    http://www.lionsky.net/club/dispbbs.asp?boardID=3&ID=1133
      

  7.   

    ------------------------------------------------------------------
    个人专栏:http://www.csdn.net/develop/author/netauthor/lihonggen0/
    ------------------------------------------------------------------
      

  8.   

    to houjianjun(三千弱水,独取一瓢清泉)
        谢谢大虾,我用了你的方法可以发送了,但是你的另一个方法可以在windows窗体下接收
    但是不能在web 窗体下接收?希望你帮我看看?不胜感激!
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Net; 
       
    using System.Net.Sockets; 
       
    using System.IO; 
    namespace NLFOFFICE2._0.测试用
    {
    /// <summary>
    /// 接收邮件 的摘要说明。
    /// </summary>
    public class 接收邮件 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.CheckBox BackupChBox;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.TextBox PopServer;
    protected System.Web.UI.WebControls.TextBox Username;
    protected System.Web.UI.WebControls.TextBox Password;
    protected System.Web.UI.WebControls.TextBox Message;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.TextBox MailNum;
    protected System.Web.UI.WebControls.Button Connect;
    protected System.Web.UI.WebControls.Button Disconnect;
    protected System.Web.UI.WebControls.Button Retrieve;
    protected System.Web.UI.WebControls.ListBox Status;
    protected System.Web.UI.WebControls.Label Label6;
    public TcpClient Server; 
    public NetworkStream NetStrm;
    public StreamReader  RdStrm;
    public string Data;
    public byte[] szData;
    public string CRLF = "\r\n";
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
                Server = new TcpClient(PopServer.Text,110);
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Connect.Click += new System.EventHandler(this.Connect_Click);
    this.Disconnect.Click += new System.EventHandler(this.Disconnect_Click);
    this.Retrieve.Click += new System.EventHandler(this.Retrieve_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Connect_Click(object sender, System.EventArgs e)
    {
    //Cursor cr = Cursor.Current;
    //Cursor.Current = Cursors.WaitCursor; // create server POP3 with port 110
    Server = new TcpClient(PopServer.Text,110);
    Status.Items.Clear(); try
    {
    // initialization
    NetStrm = Server.GetStream();
    RdStrm= new StreamReader(Server.GetStream());
    Status.Items.Add(RdStrm.ReadLine()); // Login Process
    Data = "USER "+ Username.Text+CRLF;
    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
    NetStrm.Write(szData,0,szData.Length);
    Status.Items.Add(RdStrm.ReadLine()); Data = "PASS "+ Password.Text+CRLF;
    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
    NetStrm.Write(szData,0,szData.Length);
    Status.Items.Add(RdStrm.ReadLine()); // Send STAT command to get information ie: number of mail and size
    Data = "STAT"+CRLF;
    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
    NetStrm.Write(szData,0,szData.Length);
    Status.Items.Add(RdStrm.ReadLine()); // change enabled - disabled button
    Connect.Enabled = false;
    Disconnect.Enabled = true;
    Retrieve.Enabled = true; // back to normal cursor
    //Cursor.Current = cr;
    }
    catch(InvalidOperationException err)
    {
    Status.Items.Add("Error: "+err.ToString());
    }
        
    } private void Disconnect_Click(object sender, System.EventArgs e)
    {
    // change cursor into wait cursor
    //Cursor cr = Cursor.Current;
    //Cursor.Current = Cursors.WaitCursor; // Send QUIT command to close session from POP server
    Data = "QUIT"+CRLF;
    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
    NetStrm.Write(szData,0,szData.Length);
    Status.Items.Add(RdStrm.ReadLine()); //close connection
    NetStrm.Close();
    RdStrm.Close(); // change enabled - disabled button
    Connect.Enabled = true;
    Disconnect.Enabled = false;
    Retrieve.Enabled = false; // back to normal cursor
    //Cursor.Current = cr; } private void Retrieve_Click(object sender, System.EventArgs e)
    {
    //Cursor cr = Cursor.Current;
    //Cursor.Current = Cursors.WaitCursor;
    string szTemp;
    Message.Text="";
    try
    {
    // retrieve mail with number mail parameter
    Data = "RETR "+ MailNum.Text+CRLF;
    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
    NetStrm.Write(szData,0,szData.Length); szTemp = RdStrm.ReadLine();
    if(szTemp[0]!='-') 
    {
            
    while(szTemp!=".")
    {
    Message.Text += szTemp;
    szTemp = RdStrm.ReadLine();
    }
    }
    else
    {
    Status.Items.Add(szTemp);
    }
            
    // back to normal cursor
    //Cursor.Current = cr; }
    catch(InvalidOperationException err)
    {
    Status.Items.Add("Error: "+err.ToString());
    } }
    }
    }运行时报错如下
    编译错误 
    说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0117: “System.Net.Sockets.TcpClient”并不包含对“ScriptTimeout”的定义源错误: 行 50:                 ASP.接收邮件_aspx.__intialized = true;
    行 51:             }
    行 52:             this.Server.ScriptTimeout = 30000000;
    行 53:         }
    行 54:         
     源文件: C:\WINNT\Microsoft.NET\Framework\v1.0.3705\Temporary ASP.NET Files\nlfoffice2.0_1\de230125\40ae4307\l5hh3lcl.0.cs    行: 52 
      

  9.   

    用下载的jmail没有办法实现pop3,因为有一个重要的函数被屏蔽了!
      

  10.   

    我已经实现了代码如下:
     jmail.POP3 email = new jmail.POP3();
                 email.Logging=true;
                 email.Connect(UserName.Text, Password.Text, PopServer.Text, 110);
     if (email.Count > 0) 
     {  Messages mm=email.Messages;
     try
     {
    this.savemail(mm);
     }
     catch(Exception ex)
     {
     Message.Text=ex.Message;
     }
     return ;
    }
                          email.Disconnect();