剛開始學習c#,有個問題問下前輩們,c#中如何實現發送郵件功能呢?找了好多例子調試不成功,請幫幫忙,感謝!

解决方案 »

  1.   

    可以了解下SMTP协议!
      还有这个命名空间using System.Threading;
      
      

  2.   

    using System.Net.Mail;(命名空间)<http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient%28v=VS.80%29.aspx>            MailMessage mainMsg = new MailMessage();
                   mainMsg.Body ="要发送的消息";
                   mainMsg.Subject = "主题";
                   mainMsg.From = new MailAddress("发送者邮箱","发送者昵称", Encoding.UTF8);//("[email protected]", "397324300", Encoding.UTF8);
                       mainMsg.To.Add("添加收件人邮箱地址");
                  //("[email protected]");
                   SmtpClient client = new SmtpClient();
                   client.Host ="smtp.qq.com";
                   client.Credentials = new System.Net.NetworkCredential("发送者邮箱登录名", "发送者邮箱密码");//("[email protected]", "123456");
                   object obj = mainMsg;
                   client.SendAsync(mainMsg, obj);
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Mail;//(命名空间)<http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient%28v=VS.80%29.aspx>  
    namespace ConsoleApplication1
    {
      public  class EmailSend
        {
            public void Send()
            {
                try
                {
                    MailMessage mainMsg = new MailMessage();
                    mainMsg.Body = "bendan";
                    mainMsg.Subject = "bendan";
                    mainMsg.From = new MailAddress("[email protected]", "昵称", Encoding.UTF8);//("[email protected]", "397324300", Encoding.UTF8);
                    mainMsg.To.Add("[email protected]");//收件人地址
                    //("[email protected]");
                    SmtpClient client = new SmtpClient();
                    client.Host = "smtp.163.com";//如QQ 把 163 改成QQ 
                    client.Credentials = new System.Net.NetworkCredential("帐号", "密码");//("[email protected]", "123456");
                    object obj = mainMsg;
                    client.SendAsync(mainMsg, obj);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
    }
      

  4.   

    http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient%28v=VS.80%29.aspx
    要不你再参考下这个地址
    关于这个类的使用
      

  5.   

    JMail.Net/系统自带的System.Net.Mail都可以,要是用JMail.Net的话自己修改一下源代码还可以实现快速多封连发