C#實現對郵件的發送

jopen 10年前發布 | 35K 次閱讀 C# .NET開發

首先是郵件幫助類

    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Net.Mail;  
    using System.Windows.Forms;  

    namespace zzEmail  
    {  
        //郵件幫助類  
        class MailHelper  
        {  
            SmtpClient smtpClient;  
            //郵件實體類,包含用戶名密碼  
            MailModel mail;  
            UserModel to;  
            public MailHelper()  
            {   
            }  
            public MailHelper(MailModel mail, UserModel t)  
            {  
                smtpClient = new SmtpClient();  
                this.mail = mail;  
                this.to = t;  
            }  
            public void send()  
            {  
                MailMessage msg=null;  
                smtpClient.Credentials = new System.Net.NetworkCredential(mail.from.Send_Address.Address, mail.from.password);//設置發件人身份的票據    
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;  
                smtpClient.Host = "smtp." + mail.from.Send_Address.Host;  
                try  
                {  
                    msg = mail.GetMail();  
                    msg.To.Add(to.Send_Address.Address);  
                    smtpClient.Send(msg);  
                    MessageBox.Show("發送成功");  
                }  
                catch (SmtpException err)  
                {  
                    //如果錯誤原因是沒有找到服務器,則嘗試不加smtp.前綴的服務器  
                    if (err.StatusCode == SmtpStatusCode.GeneralFailure)  
                    {  
                        try  
                        {  
                            //有些郵件服務器不加smtp.前綴  
                            smtpClient.Host = null;  
                            smtpClient.Send(mail.GetMail());  
                        }  
                        catch (SmtpException err1)  
                        {  
                            MessageBox.Show(err1.Message, "發送失敗");  
                        }  
                    }  
                    else  
                    {  
                        MessageBox.Show(err.Message, "發送失敗");  
                    }  
                }  
                finally  
                {  
                    //及時釋放占用的資源  
                    msg.Dispose();  
                }  
            }  
        }  
    }  
郵件實體類
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Net.Mail;  

    namespace zzEmail  
    {  
        //郵件實體類  
        class MailModel  
        {  
            //主題  
            public string Subject { get;set;}  
            public string SubjectEncoding { get; set; }  
            //內容  
            public string Body { get;set;}  
            public string BodyEncoding { get; set; }  
            //附件  
            public List<Attachment> Attachments=new List<Attachment>();  
            public MailMessage message;  
            //發送人  
            public UserModel from;  

            public MailModel(string subject,string body,UserModel f)  
            {  
                message = new MailMessage();  
                this.Subject = subject;  
                this.Body = body;  
                this.from = f;  
            }  
            //添加附件  
            public void AddAttach(Attachment file)  
            {  
                Attachments.Add(file);  
            }  
            //添加一群附件  
            public void AddAttach(List<Attachment> files)  
            {  
                foreach (Attachment item in files)  
                {  
                    if(item!=null)  
                        this.Attachments.Add(item);  
                }  
            }  
            //返回郵件實體  
            public MailMessage GetMail()  
            {  
                if (this.message != null)  
                {  
                    message.Subject = this.Subject;  
                    message.SubjectEncoding = System.Text.Encoding.UTF8;  
                    message.Body = this.Body;  
                    message.BodyEncoding = System.Text.Encoding.UTF8;  
                    message.From = from.Send_Address;//設置發郵件人地址  
                    foreach (Attachment item in Attachments)  
                    {  
                        if (item != null)  
                            this.message.Attachments.Add(item);  
                    }  
                    return message;  
                }  
                else  
                    return null;  
            }  
        }  
    }  
郵件的用戶類
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Net.Mail;  

    namespace zzEmail  
    {  
        //接收郵件的用戶實體類  
        class UserModel  
        {  
            public string nickname { get; set; }  
            public string password { get; set; }  
            public MailAddress Send_Address{get;set;}  
            public UserModel(string useraddr)  
            {  
                Send_Address = new MailAddress(useraddr);  
            }  
            public UserModel(string useraddr,string nickname)  
            {  
                this.nickname = nickname;  
                Send_Address = new MailAddress(useraddr);  
            }  
        }  
    }  
最后是具體的操作

使用多線程來對郵件進行發送

    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.Threading;  

    namespace zzEmail  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  

            private void btn_addFile_Click(object sender, EventArgs e)  
            {  
                OpenFileDialog myOpenFileDialog = new OpenFileDialog();  
                myOpenFileDialog.CheckFileExists = true;  
                //只接收有效的文件名  
                myOpenFileDialog.ValidateNames = true;  
                //允許一次選擇多個文件作為附件  
                myOpenFileDialog.Multiselect = true;  
                myOpenFileDialog.ShowDialog();  
                if (myOpenFileDialog.FileNames.Length > 0)  
                {  
                    FileList.Items.AddRange(myOpenFileDialog.FileNames);  
                }  
            }  

            private void btn_Send_Click(object sender, EventArgs e)  
            {  
                UserModel from = new UserModel(txt_UserName.Text);  
                from.password = txt_Pass.Text;  

                UserModel to = new UserModel(txt_rec.Text);  

                MailModel mail = new MailModel(txt_sub.Text, txt_content.Text, from);  

                List<Attachment> filelist = new List<Attachment>();  
                //添加附件  
                if (FileList.Items.Count > 0)  
                {  
                    for (int i = 0; i < FileList.Items.Count; i++)  
                    {  
                        Attachment attachFile = new Attachment(FileList.Items[i].ToString());  
                        filelist.Add(attachFile);  
                    }  
                }  

                mail.AddAttach(filelist);//添加附件  
                MailHelper helper = new MailHelper(mail, to);  
                //啟動一個線程發送郵件  
                Thread mythread = new Thread(new ThreadStart(helper.send));  
                mythread.Start();  
            }  
        }  
    }  

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!