c# 发送邮件笔记(一)

代码比较简单 

using System;using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
using System.Net.Mime;
namespace 邮件发送程序
{
    class SMTPHelper
    {

        private string user_name = "";
        private string pwd = "";
        private SmtpClient smtp_client = null;
        private MailAddress address_my = null;

        public SMTPHelper(string mail_address, string display_name, string pwd, bool ssl = false)
        {
            address_my = new MailAddress(mail_address, display_name, Encoding.UTF8);
            //保存用户帐号和密码
            this.user_name = address_my.User;
            this.pwd = pwd;

            smtp_client = new SmtpClient(address_my.Host, ssl ? 110 : 25);
            smtp_client.Timeout = 10 * 1000; //设置10秒超时
            smtp_client.UseDefaultCredentials = false;
            this.smtp_client.Host = "smtp." + address_my.Host;

        }

        public void setAuthenticateInfo(string name, string pwd)
        {

            NetworkCredential credential = new NetworkCredential(name, pwd);
            this.smtp_client.Credentials = credential; //填写验证            

        }

        public MailMessage createMailMessage(string to, string subject, string body)
        {

            string[] to_list = to.Split(';');//多个邮箱使用;隔开

            MailMessage mail_message = new MailMessage();
            mail_message.Sender = this.address_my;
            mail_message.From = this.address_my;
            mail_message.Subject = subject;
            mail_message.SubjectEncoding = Encoding.UTF8;
            mail_message.Body = body;
            mail_message.BodyEncoding = Encoding.UTF8; //UTF8编码
            mail_message.IsBodyHtml = false; //不是HTML
            foreach (string s in to_list)
            {
                mail_message.To.Add(new MailAddress(s));

            }
            return mail_message;
        }
        /// <summary>
        /// 创建带附件的邮件
        /// </summary>
        /// <param name="address_to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public MailMessage createMailMessageWithAttacth(string to, string subject, string body, string fileNames)
        {
            MailMessage mail_message = this.createMailMessage(to, subject, body);
            string[] files = fileNames.Split(';');
            foreach (string file in files)
            {
                if (File.Exists(file))
                {
                    Attachment attchment = new Attachment(file);

                    //设置mime对象
                    ContentDisposition disposition = attchment.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(file); //附件创建日期
                    disposition.ModificationDate = File.GetLastWriteTime(file);//最后更改日期
                    disposition.ReadDate = File.GetLastAccessTime(file);//最后读取时间
                    mail_message.Attachments.Add(attchment);
                }

            }
            return mail_message;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="to">如果多个帐户用分隔符</param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public bool doSend(string to, string subject, string body, string fileNames)
        {
            smtp_client.DeliveryMethod = SmtpDeliveryMethod.Network; //指定发送模式,由网络直接发送,一般都用这个

            setAuthenticateInfo(this.user_name, this.pwd); //设置验证信息
            //发送
            try
            {
                smtp_client.Send(createMailMessageWithAttacth(to, subject, body, fileNames));
                return true;
            }
            catch (SmtpException err)
            {
                return false;
            }

        }


    }
}

调用

if (new SMTPHelper(this.textBox1.Text, this.textBox1.Text, this.textBox2.Text, false).doSend(this.textBox4.Text, this.textBox3.Text, this.richTextBox1.Text, fileNames))
{
MessageBox.Show("发送成功");
}
else
{
MessageBox.Show("发送失败");
}

 

posted @ 2013-04-15 12:27  TryHard  阅读(330)  评论(0编辑  收藏  举报