当前位置:编程学习 > C#/ASP.NET >>

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 = "heuandmei@qq.com"; 

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

            txtPassword.Text = "";//密码 

            txtReceive.Text = "heuandmei@qq.com"; 

            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

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,