程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 郵件發送,可根據需求修改為群發~

C# 郵件發送,可根據需求修改為群發~

編輯:C#入門知識

 

我直接上圖 上代碼吧。

 

\

 

代碼:

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; 

using System.Net.Mail; 

using System.Net.Mime; 

using System.IO; 

 

namespace SendMailExample 

    /// <summary> 

    /// 作者:Andrew 

    /// Blog: http://blog.csdn.net/Andrew_wx 

    /// </summary>

    public partial class FormSendMail : Form 

    { 

        public FormSendMail() 

        { 

            InitializeComponent(); 

        } 

 

        private void FormSendMail_Load(object sender, EventArgs e) 

        { 

            txtSmtpServer.Text = "smtp.qq.com"; 

            txtSend.Text = "[email protected]"; 

            txtDisplayName.Text = "Andrew(王旭)"; 

            txtPassword.Text = "";//密碼 

            txtReceive.Text = "[email protected]"; 

            txtTitle.Text = "發信測試"; 

            txtBody.Text = "This is a test(測試)"; 

            rbtnNoSSL.Checked = true; 

        } 

 

        private void btnAddFiles_Click(object sender, EventArgs e) 

        { 

            OpenFileDialog odlg = new OpenFileDialog(); 

            odlg.CheckFileExists = true; 

            //只接收有效的文件名 

            odlg.ValidateNames = true; 

            //允許一次選擇多個文件作為附件 

            odlg.Multiselect = true; 

            if (odlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 

            { 

                lstFiles.Items.AddRange(odlg.FileNames); 

            } 

             

        } 

 

        private void btnSend_Click(object sender, EventArgs e) 

        { 

            this.Cursor = Cursors.WaitCursor; 

            MailMessage mail = new MailMessage(); 

            mail.From = new MailAddress( 

                txtSend.Text, txtDisplayName.Text, Encoding.UTF8); 

            mail.To.Add(txtReceive.Text); 

            mail.Subject = txtTitle.Text; 

            mail.SubjectEncoding = Encoding.Default; 

            mail.Body = txtBody.Text; 

            mail.BodyEncoding = Encoding.Default; 

            mail.IsBodyHtml = false; 

            mail.Priority = MailPriority.Normal; 

            //添加附件 

            Attachment attachment = null; 

            if (lstFiles.Items.Count > 0) 

            { 

                for (int i = 0; i < lstFiles.Items.Count; i++) 

                { 

                    string pathFileName = lstFiles.Items[i].ToString(); 

                    string extName = Path.GetExtension(pathFileName).ToLower(); 

                    //判斷附件類型 

                    if (extName == ".rar" || extName == ".zip") 

                    { 

                        attachment = new Attachment(pathFileName, MediaTypeNames.Application.Zip); 

                    } 

                    else 

                    { 

                        attachment = new Attachment(pathFileName, MediaTypeNames.Application.Octet); 

                    } 

                    ContentDisposition cd = attachment.ContentDisposition; 

                    cd.CreationDate = File.GetCreationTime(pathFileName); 

                    cd.ModificationDate = File.GetLastWriteTime(pathFileName); 

                    cd.ReadDate = File.GetLastAccessTime(pathFileName); 

                    mail.Attachments.Add(attachment); 

 

                } 

            } 

            SmtpClient client = new SmtpClient(); 

            client.Host = txtSmtpServer.Text; 

            client.Port = 25; 

            //是否使用安全套接字層加密連接 

            client.EnableSsl = rbtnUseSSL.Checked; 

            //不使用默認憑證,注意此句必須放在client.Credentials 的上面 

            client.UseDefaultCredentials = false; 

            client.Credentials = new NetworkCredential(txtSend.Text, txtPassword.Text); 

            //郵件通過網絡直接發送到服務器 

            client.DeliveryMethod = SmtpDeliveryMethod.Network; 

            try 

            { 

                client.Send(mail); 

                MessageBox.Show("發送成功"); 

            } 

            catch (SmtpException ex) 

            { 

                MessageBox.Show("發送失敗:" + ex.Message); 

            } 

            catch (Exception ex) 

            { 

                MessageBox.Show("發送失敗:" + ex.Message); 

            } 

            finally 

            { 

                mail.Dispose(); 

                client = null; 

                this.Cursor = Cursors.Default; 

            } 

        } 

    } 

}

復制代碼

 

以上是完整代碼。

項目包下載地址: http://www.BkJia.com/uploadfile/2011/1210/20111210024614328.rar

 

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved