using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress("[email protected]");
            mailMsg.To.Add("[email protected]");
           
            mailMsg.Subject = "邮件主题";
            mailMsg.Body = "邮件主体内容";
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.IsBodyHtml = false;
            mailMsg.Priority = MailPriority.High;            SmtpClient smtp = new SmtpClient();
            // 提供身份验证的用户名和密码 
            // 网易邮件用户可能为:username password 
            // Gmail 用户可能为:[email protected] password 
            smtp.Credentials = new NetworkCredential("[email protected]", "yanzhifeng");
            smtp.Port = 25; // Gmail 使用 465 和 587 端口 
            smtp.Host = "[email protected]"; // 如 smtp.163.com, smtp.gmail.com             smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true 
            smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted);
            try
            {
                smtp.SendAsync(mailMsg, mailMsg);
                Response.Write("<script>alert('发送成功')</script>");
            }
            catch (SmtpException ex)
            {
                Console.WriteLine(ex.ToString());
                Response.Write("<script>alert('发送失败')</script>");
            } 
        }    }
    void SendMailCompleted(object sender, AsyncCompletedEventArgs e) 
   { 
    MailMessage mailMsg = (MailMessage)e.UserState; 
    string subject = mailMsg.Subject; 
    if (e.Cancelled) // 邮件被取消 
    { 
        Console.WriteLine(subject + " 被取消。"); 
    } 
    if (e.Error != null) 
    { 
        Console.WriteLine("错误:" + e.Error.ToString()); 
    } 
    else 
    { 
        Console.WriteLine("发送完成。"); 
    } 
   }}
怎么才能给这段代码 改的能成功发送邮件啊。

解决方案 »

  1.   


    public static int SandMail(string FriendMail)
            {
                int result = 0;
                string YueSaiBody = "XXX内容";//正文            try
                {
                    SmtpClient smtp = new SmtpClient();
                    MailMessage mail = new MailMessage();
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Host = "XX邮箱HOST";
                    smtp.Credentials = new System.Net.NetworkCredential("XXX邮箱用户名", "邮箱密码");
                    mail.From = new MailAddress("XX发送人");
                    mail.To.Add(FriendMail); //接收人
                    mail.Subject = "XXXX标题";
                    mail.Body = YueSaiBody;
                    mail.BodyEncoding = System.Text.Encoding.UTF8;
                    mail.IsBodyHtml = true;
                    mail.Priority = MailPriority.Normal;                smtp.Send(mail);
                    result = 1;
                }
                catch (Exception ex)
                {
                    result = 2;
                }              return result;
                }    
      

  2.   

      我试了 呢这段 代码 也是不行啊 代码如下 请我是我 写的不对吗
     public static  int SandMail(string FriendMail)
        {
            int result = 0;
            string YueSaiBody = "XXX内容";//正文        try
            {
                SmtpClient smtp = new SmtpClient();
                MailMessage mail = new MailMessage();
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Host = "[email protected]";
                smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "yanzhifeng");
                mail.From = new MailAddress("[email protected]");
                mail.To.Add("[email protected]"); //接收人
                mail.Subject = "XXXX标题";
                mail.Body = YueSaiBody;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.Normal;            smtp.Send(mail);
                result = 1;
            }
            catch (Exception ex)
            {
                result = 2;
            }        return result;
        }    
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;using System.Text;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Threading;
    using System.ComponentModel; public partial class MailSend : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                MailMessage mailMsg = new MailMessage(); 
                mailMsg.From = new MailAddress("[email protected]"); 
                mailMsg.To.Add("[email protected]"); 
              
                mailMsg.Subject = "邮件主题"; 
                mailMsg.Body = "邮件主体内容"; 
                mailMsg.BodyEncoding = Encoding.UTF8; 
                mailMsg.IsBodyHtml = false; 
                mailMsg.Priority = MailPriority.High;             SmtpClient smtp = new SmtpClient(); 
                // 提供身份验证的用户名和密码 
                // 网易邮件用户可能为:username password 
                // Gmail 用户可能为:[email protected] password 
                smtp.Credentials = new NetworkCredential("[email protected]", "1"); 
                smtp.Port = 25; // Gmail 使用 465 和 587 端口 
                smtp.Host = "192.168.16.84"; // 如 smtp.163.com, smtp.gmail.com             smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true 
                smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted); 
                try 
                { 
                    smtp.Send(mailMsg); 
                    Response.Write(" <script>alert('发送成功') </script>"); 
                } 
                catch (SmtpException ex) 
                { 
                    Console.WriteLine(ex.ToString()); 
                    Response.Write(" <script>alert('发送失败') </script>"); 
                } 
            }     } 
        void SendMailCompleted(object sender, AsyncCompletedEventArgs e) 
      { 
        MailMessage mailMsg = (MailMessage)e.UserState; 
        string subject = mailMsg.Subject; 
        if (e.Cancelled) // 邮件被取消 
        { 
            Console.WriteLine(subject + " 被取消。"); 
        } 
        if (e.Error != null) 
        { 
            Console.WriteLine("错误:" + e.Error.ToString()); 
        } 
        else 
        { 
            Console.WriteLine("发送完成。"); 
        } 
      } } 这个调成功率  是用的自己的邮件服务器
      

  4.   

    你的 smtp.Host 地址貌似不对 
      

  5.   

    又是邮件发送的问题,你可以把错误代码贴出来,163的邮箱有个问题是否支持smtp服务调用,新注册的好像有问题
      

  6.   

    在Visual C#中正确使用发送电子邮件相关的对象: 
    (1).要调用对象,当然首先就要在程序的最前面导入封装对象的名称空间,具体如下: 
    using System.Web.Mail ; 
    (2).正确定义MailMessage对象的属性: 
    MailMessage对象中和电子邮件相关的属性可以用下表来表示: 属性名称  代表意义  
    From  源地址  
    To  目的地址  
    Subject  邮件主题  
    Priority  邮件优先级 ( High , Low , Normal )  
    Attachments  附件  
    Bcc  暗送地址  
    Cc  抄送地址  
    Body  邮件内容主体  
    Bodyformat  邮件格式( Html , Text )  
    Bodyencoding  邮件编码( Base64 , Uuencode )  在程序中,具体的实现语句如下: 
    MailMessage aMessage = new MailMessage ( ) ; 
    //新建一个MailMessage对象 
    aMessage.From = FromTextBox.Text ; 
    //定义发信人地址,如果是多人,可以用","分开 
    aMessage.To = ToTextBox.Text ; 
    //定义收信人地址,如果是多人,可以用","分开 
    aMessage.Cc = CCTextBox.Text ; 
    //定义抄送人地址,如果是多人,可以用","分开 
    aMessage.Bcc = BCCTextBox.Text ; 
    //定义暗送人地址,如果是多人,可以用","分开 
    aMessage.Subject = SubjectTextBox.Text ; 
    //定义邮件的主题 
    aMessage.Body = MessageTextBox.Text ; 
    //定义邮件的内容 
    if ( AttachmentTextBox.Text.Length > 0 ) 
    aMessage.Attachments.Add ( new MailAttachment ( AttachmentTextBox.Text , MailEncoding.Base64 ) ) ; 
    //给邮件增加一个附件  
    注:"="右边是程序中定义的文本框的"Text"值。 
    (3).用SmtpMail对象正确发送电子邮件: 
    在Visual C#中调用SmtpMail对象的Send ( )方法有多种方式。本文介绍的只是其中的一种比较常用的调用方式,即:SmtpMail.Send ( MailMessage对象 )。 在程序中的实现语句如下: 
    SmtpMail.Send ( aMessage ) ; 
    来自:http://developer.51cto.com/art/200509/4133.htm
      

  7.   

    思想: 
    1.创建邮件发送客户端实例(需要服务器地址) 
    2.创建邮件验证信息(需要name和psw) 
    3.创建邮件实例(包括邮件主题,内容,附件等) 
    4.发送; 
    代码: 
    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.Net; 
    using System.Net.Mail; 
    namespace Mail 

        class Mail 
        { 
            private SmtpClient mySmtp = null; 
            private MailMessage myMailMessage = null; 
            private NetworkCredential myCredential = null; //System.net下 
            private Attachment myAttachment = null; 
            /// <summary> 
            /// 邮件发送方法(张伟波) 
            /// </summary> 
            /// <param name="host">服务器 </param> 
            /// <param name="userName">用户名 </param> 
            /// <param name="password">密码 </param> 
            /// <param name="mailFrom">发送方 </param> 
            /// <param name="mailTo">接收方 </param> 
            /// <param name="mailContent">邮件正文 </param> 
            /// <param name="mailSubject">邮件主题 </param> 
            /// <param name="addFile">添加附件 </param> 
            /// <returns> </returns> 
            public bool SendMail(string host, string userName, string password, string mailFrom, string mailTo, string mailContent, string mailSubject, string addFile) 
            { 
                try 
                { 
                    mySmtp = new SmtpClient(host);      
                    mySmtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
                    myCredential = new NetworkCredential(userName,password); 
                    myMailMessage = new MailMessage(mailFrom,mailTo); 
                    myMailMessage.Subject = mailSubject; 
                    myMailMessage.Body() = mailContent; 
                    if (addFile!="") 
                    { 
                        myAttachment = new Attachment(addFile); 
                        myMailMessage.Attachments.Add(myAttachment); 
                    } 
                    mySmtp.Send(myMailMessage); 
                    return true; 
                } 
                catch 
                { 
                    return false; 
                } 
            } 
        } 

    来自:http://student.csdn.net/space.php?uid=40072&do=blog&id=572
      

  8.   

     发不出去的原因很有可能就是stmp服务器的问题  。  多尝试几个 
      

  9.   

    可以用第三方的控件 jmail也可以