新思想

C# 简单邮件发送(smtp)客户端 源代码

C# 简单邮件发送(smtp)客户端 源代码

smtp

// http://sourceforge.net/p/netimplicitssl/wiki/C%23%20-%20Send%20Email%20over%20SSL%20on%20465%20port/
// https://www.emailarchitect.net/easendmail/ex/c/2.aspx
// http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx
// http://stackoverflow.com/questions/1011245/how-can-i-send-emails-through-ssl-smtp-with-the-net-framework

using System;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.IO;

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

        private void btSend_Click(object sender, EventArgs e)
        {
            MailAddress from = new MailAddress(tbMyMail.Text);
            MailAddress to = new MailAddress(tbSendTo.Text);
            // MailMessage用于表示将被发送的电子邮件
            MailMessage message = new MailMessage(from, to);
            message.Subject = tbSubject.Text;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.Body = tbBody.Text;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            
            try
            {
                //添加附件
                if (comboBox1.Items.Count > 0)
                    for (int i = 0; i < comboBox1.Items.Count; i++)
                    {
                        Attachment attachFile = new Attachment(comboBox1.Items[i].ToString());
                        message.Attachments.Add(attachFile);
                    }

                {
                    //MailMessage mail = new MailMessage();
                    //mail.To.Add("anyreceiver@anydomain.com");
                    //mail.From = new MailAddress("anyaddress@anydomain.com");
                    //mail.Subject = "Test email of All-In-One Code Framework - CSSMTPSendEmail";
                    //mail.Body = "Welcome to <a href='http://cfx.codeplex.com'>All-In-One Code Framework</a>!";
                    //mail.IsBodyHtml = true;

                    //// 附件
                    //Console.WriteLine("添加附件");
                    //string attachedFile = "<attached file path>";
                    //mail.Attachments.Add(new Attachment(attachedFile));

                    //// Embedded image in the message body
                    //Console.WriteLine("Embed image");
                    //mail.Body += "<br/><img alt=\"\" src=\"cid:image1\">";

                    //string imgFile = "<image file path>";
                    //AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
                    //    mail.Body, null, "text/html");
                    //LinkedResource imgLink = new LinkedResource(imgFile, "image/jpg");
                    //imgLink.ContentId = "image1";
                    //imgLink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                    //htmlView.LinkedResources.Add(imgLink);
                    //mail.AlternateViews.Add(htmlView);
                }

                // SmtpClient 用于发送邮件
                //SmtpClient mailClient = new SmtpClient("smtp." + from.Host, Convert.ToInt32(tbSmtpPort.Text));
                SmtpClient mailClient = new SmtpClient(tbSmtpSvr.Text, Convert.ToInt32(tbSmtpPort.Text));
                //mailClient.Port = Convert.ToInt32(tbSmtpPort.Text);

                //不使用默认凭证,注意此句必须放在client.Credentials的上面
                mailClient.UseDefaultCredentials = false;
                mailClient.Credentials = new NetworkCredential(tbMyMail.Text, tbMyPassword.Text);
                if (cbTLS.Checked)
                    mailClient.EnableSsl = true;
                else
                    mailClient.EnableSsl = false;
                // 邮件通过网络发送到服务器
                mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                // 发送到服务器
                mailClient.Send(message);
                MessageBox.Show("邮件已发送. ");
            }
            catch (FormatException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (SmtpException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbMyMail_Leave(object sender, EventArgs e)
        {
            string subst = tbMyMail.Text.Substring(tbMyMail.Text.LastIndexOf("@") + 1);
            tbPopSvr.Text = "pop." + subst;
            tbSmtpSvr.Text = "smtp." + subst;
            tbBody.Text += "\r\n说明:\r\n非加密时,SMTP 默认使用25端口,POP 默认110端口。\r\n加密时,smtp隐式Implicit SSL使用465端口,显式Explicit SSL使用587端口,.net 默认不支持Implicit SSL。\r\n\r\n"
                            + "常见邮箱的设定:\r\n"
                            + "\t\tsmtp(SSL)\t\tsmtp(TLS)\tpop(SSL)\t\t\t\timap(SSL)\r\n"
                            + "gmail:\t\tsmtp.gmail.com : 465\t587\t\tpop.gmail.com : 995\t\t\timap.gmail.com : 993\r\n"
                            + "msn / live:\tsmtp.live.com : \t587 / 25\tpop3.live.com : 995\r\n"
                            + "outlook:\tsmtp-mail.outlook.com :\t587 / 25\tpop-mail.outlook.com : 995(需开启)\timap-mail.outlook.com : 993\r\n"
                            + "qq:\t\tsmtp.qq.com : 465\t587\t\tpop.qq.com : 995\r\n"
                            + "163:\t\tsmtp.163.com : 465/994\t25\t\tpop.163.com: 995/ 110(noSSL)\t\timap.163.com: 993/ 143(noSSL)\r\n"
                            + "sina:\t\tsmtp.sina.com\t\t\t\tpop.sina.com\t\t\t\timap.sina.com(需开启)\r\n";
        }

        private void btAddAttachment_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)
                comboBox1.Items.AddRange(myOpenFileDialog.FileNames);
            comboBox1.Text = Path.GetFileName(comboBox1.Items[0].ToString());
        }
    }
}

源程序下载

执行程序下载

posted on 2016-01-10 12:35  新思想  阅读(959)  评论(0)    收藏  举报

导航