http://www.cnblogs.com/jessezhao/archive/2007/01/16/621946.html

解决方案 »

  1.   

    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Threading;
    using System.ComponentModel;
    namespace Examples.SmptExamples.Async
    {
        public class SimpleAsynchronousExample
        {
            static bool mailSent = false;
            public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
            {
                // Get the unique identifier for this asynchronous operation.
                 String token = (string) e.UserState;
               
                if (e.Cancelled)
                {
                     Console.WriteLine("[{0}] Send canceled.", token);
                }
                if (e.Error != null)
                {
                     Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
                } else
                {
                    Console.WriteLine("Message sent.");
                }
                mailSent = true;
            }
            public static void Main(string[] args)
            {
                // Command line argument must the the SMTP host.
                SmtpClient client = new SmtpClient(args[0]);
                // Specify the e-mail sender.
                // Create a mailing address that includes a UTF8 character
                // in the display name.
                MailAddress from = new MailAddress("[email protected]", 
                   "Jane " + (char)0xD8+ " Clayton", 
                System.Text.Encoding.UTF8);
                // Set destinations for the e-mail message.
                MailAddress to = new MailAddress("[email protected]");
                // Specify the message content.
                MailMessage message = new MailMessage(from, to);
                message.Body = "This is a test e-mail message sent by an application. ";
                // Include some non-ASCII characters in body and subject.
                string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
                message.Body += Environment.NewLine + someArrows;
                message.BodyEncoding =  System.Text.Encoding.UTF8;
                message.Subject = "test message 1" + someArrows;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                // Set the method that is called back when the send operation ends.
                client.SendCompleted += new 
                SendCompletedEventHandler(SendCompletedCallback);
                // The userState can be any object that allows your callback 
                // method to identify this send operation.
                // For this example, the userToken is a string constant.
                string userState = "test message1";
                client.SendAsync(message, userState);
                Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
                string answer = Console.ReadLine();
                // If the user canceled the send, and mail hasn't been sent yet,
                // then cancel the pending operation.
                if (answer.StartsWith("c") && mailSent == false)
                {
                    client.SendAsyncCancel();
                }
                // Clean up.
                message.Dispose();
                Console.WriteLine("Goodbye.");
            }
        }
    }
     
      

  2.   

    不过这个是1.1的,参数自定义MailMessage myMail = new MailMessage(); 
    myMail.From = EMAIL_ADDRESS;
    myMail.To = strEmail;
    myMail.Subject = "邮件确认";
    myMail.BodyFormat = MailFormat.Text;
    myMail.Body = "内容";
    myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", EMAIL_ADDRESS);myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", EMAIL_PASSWORD);
    SmtpMail.SmtpServer = EMAIL_SERVER;
    SmtpMail.Send(myMail);
      

  3.   

    谢谢楼上的,不过我还是想要2.0的请问需不需要设置自己的STMP服务器啊,因为我从网上找到的代码,测试都不通过,现在又出错了,提示:邮箱不可用。 服务器响应为: Óû§±»Ëø¶¨
      

  4.   

    /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="sMailServer"></param>
            /// <param name="sMailServerUserName"></param>
            /// <param name="sMailServerUserPassword"></param>
            /// <param name="sFrom"></param>
            /// <param name="sTo"></param>
            /// <param name="sCc"></param>
            /// <param name="sSubject"></param>
            /// <param name="sBody"></param>
            /// <param name="isBodyHtml"></param>
            /// <param name="mailPriority"></param>
            /// <returns></returns>
            public static ReturnValue SendEmail(string sMailServer,string sMailServerUserName,string sMailServerUserPassword,string sFrom, string sTo, string sCc, string sSubject, string sBody,bool isBodyHtml,System.Net.Mail.MailPriority mailPriority)
            {
                ReturnValue retValue = new ReturnValue();            if (sMailServer == "")
                {
                    retValue.HasError = true;
                    retValue.Message = "系统没有配置发送邮件的服务器名称!";
                    return retValue;
                }
                if (sFrom == "")
                {
                    retValue.HasError = true;
                    retValue.Message = "系统没有配置发送邮件的帐号!";
                    return retValue;
                }            string sBodyEncoding;            sBodyEncoding = Encoding.Default.EncodingName;
                
                MailMessage MyMail = new MailMessage(sFrom,sTo);
                MyMail.Subject = sSubject;
                MyMail.Body = sBody;
                //MyMail.CC = sCc;            // if (sBodyEncoding== Encoding.UTF7.EncodingName)
                // MyMail.BodyEncoding = Encoding.UTF7;
                // else if (sBodyEncoding== Encoding.UTF8.EncodingName)
                // MyMail.BodyEncoding = Encoding.UTF8;
                // else
                // MyMail.BodyEncoding = Encoding.ASCII;            MyMail.IsBodyHtml = isBodyHtml;
                
                MyMail.Priority = mailPriority;            try
                {                
                    SmtpClient smtpClient=new SmtpClient();
                    smtpClient.Credentials = new System.Net.NetworkCredential(sMailServerUserName, sMailServerUserPassword);
                    smtpClient.Host=sMailServer;
                    if (SmtpServerPort > 0) smtpClient.Port = SmtpServerPort;
                    smtpClient.Send(MyMail);
                }
                catch (Exception ee)
                {
                    retValue.HasError = true;
                    retValue.Message = "发送邮件不成功,可能是因为邮件目标地址不存在!详细错误信息是:" + ee.Message;
                }
                return retValue;
            }
        }
      

  5.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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.Mail;
    using System.Net;public partial class sendmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
           
            string s_from = "[email protected]";
            string s_to = "TextBox1.Text";
            string pwd = TextBox2.Text;        int i = s_from.IndexOf("@");
            string username = s_from.Substring(0, i);        MailAddress from = new MailAddress(s_from);
            MailAddress to = new MailAddress(s_to);
            MailMessage mailobj = new MailMessage(from, to);
            mailobj.Subject = "aaa";
            mailobj.Body = "aaa";
            mailobj.IsBodyHtml = true;
            mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            mailobj.Priority = MailPriority.High;
           
            SmtpClient smtp = new SmtpClient("smtp.163.com");
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(username, pwd);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;        try
            {
                smtp.Send(mailobj);
                Response.Write("ok!");        }
            catch
            {
                Response.Write("sorry!");
            }    }}
      

  6.   

    楼上的你有没有测试过啊我怎么不可以啊,还是这个错:【邮箱不可用。 服务器响应为: &Oacute;&Atilde;&raquo;§±&raquo;&Euml;&oslash;&para;】