using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;using System.Windows.Forms;namespace Storm2.CommonLib
{
    public delegate void MailEventDelegate(EventArgs e);    public class SendMail
    {
        #region Variable declaration
        private string m_from;
        private string[] m_too;
        private string[] m_cc;
        private string m_subject;
        private string m_body;
        private string[] m_filename;
        private string m_host;
        private string m_user;
        private string m_password;
        #endregion        public delegate bool MethodDelegate();        public event MailEventDelegate MailEvent;        public static bool outFlag;        public SendMail()
        {
        }
        public string From
        {
            get { return m_from; }
            set { m_from = value; }
        }
        public string[] Too
        {
            get { return m_too; }
            set { m_too = value; }
        }
        public string[] Cc
        {
            get { return m_cc; }
            set { m_cc = value; }
        }
        public string Subject
        {
            get { return m_subject; }
            set { m_subject = value; }
        }
        public string Body
        {
            get { return m_body; }
            set { m_body = value; }
        }
        public string[] FileName
        {
            get { return m_filename; }
            set { m_filename = value; }
        }
        public string Host
        {
            get { return m_host; }
            set { m_host = value; }
        }
        public string User
        {
            get { return m_user; }
            set { m_user = value; }
        }
        public string Password
        {
            get { return m_password; }
            set { m_password = value; }
        }
        public bool CreateMessageWithAttachmen()
        {
            if (string.IsNullOrEmpty(m_from.TrimEnd())) return false;            if (m_too == null) return false;            for (int i = 0; i <= m_too.Length - 1; i++)
                if (string.IsNullOrEmpty(m_too[i])) return false;            MailAddress fromAddress = new MailAddress(m_from);
            MailAddress toAddress = new MailAddress(m_too[0]);            MailMessage message = new MailMessage(fromAddress, toAddress);            message.Subject = m_subject;
            message.IsBodyHtml = true;
            message.Body = m_body;
            //To
            if (m_too.Length > 1)
            {
                for (int i = 1; i <= m_too.Length - 1; i++)
                {
                    message.To.Add(m_too[i]);
                }
            }
            //Cc
            if (m_cc != null)
            {
                for (int i = 0; i <= m_cc.Length - 1; i++)
                {
                    if (m_cc[i].Trim() != string.Empty)
                        message.CC.Add(m_cc[i]);
                }
            }
            //Attachmen
            if (m_filename != null)
            {
                for (int i = 0; i <= m_filename.Length - 1; i++)
                {
                    if (!string.IsNullOrEmpty(m_filename[i]))
                    {
                        if (File.Exists(m_filename[i]) == true)
                        {
                            ContentType ct = new ContentType(MediaTypeNames.Application.Octet);                            Attachment data = new Attachment(m_filename[i], ct);                            ContentDisposition disposition = data.ContentDisposition;                            disposition.CreationDate = File.GetCreationTime(m_filename[i]);
                            disposition.ModificationDate = File.GetLastWriteTime(m_filename[i]);
                            disposition.ReadDate = File.GetLastAccessTime(m_filename[i]);
                            message.Attachments.Add(data);
                        }
                        else
                        {
                            //File Not Exist
                            return false;
                        }
                    }
                }
            }            SmtpClient client = new SmtpClient(m_host);            client.Credentials = new NetworkCredential(m_user, m_password);
            client.Send(message);            return true;
        }        private void IsAsyncCompleted(IAsyncResult asyncResult)
        {            if (asyncResult == null) return;            MethodDelegate md = (MethodDelegate)asyncResult.AsyncState;            outFlag = (bool)md.EndInvoke(asyncResult);            RaiseMailEvent(EventArgs.Empty);        }        private void RaiseMailEvent(EventArgs e)
        {
            if (MailEvent != null)
            {
                MailEvent(e);
            }
        }        public void AsyncSendMail()
        {
            MethodDelegate dlgt = new MethodDelegate(this.CreateMessageWithAttachmen);
            IAsyncResult ar = dlgt.BeginInvoke(IsAsyncCompleted, dlgt);
        }    }}

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Mail;
    using System.Net;namespace MailTest
    {
         public partial class Form1 : Form
         {
             public static string server;
             public static string name;
             public static string pwd;
             public static string fromer;
            
             public Form1()
             {
                 InitializeComponent();
             }
                    
             private void Form1_Load(object sender, EventArgs e)
             {         }
            
             private void btnSetting_Click(object sender, EventArgs e)
             {
                 Setting set = new Setting();
                 set.Show();
             }         private void btnSend_Click(object sender, EventArgs e)
             {
                 try
                 {
                     MailAddress to = new MailAddress(this.txtTo.Text.Trim());
                     MailAddress from = new MailAddress(fromer);
                     MailMessage msg = new MailMessage(from, to);
                     msg.Subject = txtSubject.Text.Trim();
                     msg.Body = txtBody.Text.Trim();
                     msg.IsBodyHtml = true;
                     SmtpClient smtp = new SmtpClient(server, 25);
                     smtp.Credentials = new NetworkCredential(name, pwd);
                     smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                     smtp.Send(msg);
                     MessageBox.Show("邮件发送成功!");
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
                 finally
                 {
                     this.txtBody.Text = "";
                     this.txtSubject.Text = "";
                     this.txtTo.Text = "";
                 }
             }        
         }
    }
      

  2.   

     private void Sendmail(string mailto,string body,string title)
            {
                MailMessage m_msg=new MailMessage();
                m_msg.From="张三<[email protected]>";
                m_msg.To=mailto;//收件人
                m_msg.Body=body;//邮件正文
                m_msg.Subject=title;//邮件标题
                m_msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//是否需要验证,一般是要的
                m_msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]");//自己邮箱的用户名
                m_msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "abcd");//自己邮箱的password
                SmtpMail.SmtpServer="pop3.163.com";
                try
                {
                    SmtpMail.Send(m_msg);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
      

  3.   

       ASP.Net环境下使用Jmail组件发送邮件
       ASP.NET JMail 接收邮件