C#实现发送邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;
using System.IO;  

namespace TestForm
{
    class Email
    {
        string smtpHost = string.Empty;
        string Sendmailaddress = string.Empty;
        string Sendpassword = string.Empty;
        string SendDisplayname = string.Empty;
        string Recevivemailaddress = string.Empty;
        string ReceviveDisplayname = string.Empty;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="host">主机号</param>
        /// <param name="sendmailaddress">发送人邮箱地址</param>
        /// <param name="sendpassword">发送邮箱密码</param>
        /// <param name="sendDisplayname">发送人显示名称</param>
        /// <param name="recevivemailaddress">接收人邮箱地址</param>
        /// <param name="receviveDisplayname">接收人显示名称</param>
        public Email(string host, string sendmailaddress, string sendpassword, string sendDisplayname, string recevivemailaddress, string receviveDisplayname) 
        {
            smtpHost = host;
            Sendmailaddress = sendmailaddress;
            Sendpassword = sendpassword;
            SendDisplayname = sendDisplayname;
            Recevivemailaddress = recevivemailaddress;
            ReceviveDisplayname = receviveDisplayname;
        }

        /// <summary>
        /// 发送邮件功能
        /// </summary>
        /// <param name="mailsubject">邮件标题</param>
        /// <param name="mailbody">邮件主要内容</param>
        /// <param name="isadddocument">是否添加附件</param>
        /// <param name="documentpath">添加附件的文件路径列表</param>
        /// <returns></returns>
        public bool Sendmail(string mailsubject, string mailbody, bool isadddocument, List<string> documentpath)
        {
            bool sendstatus = false;
            try 
            {
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtpHost);  //确定smtp服务器地址。实例化一个Smtp客户端
                MailAddress from = new MailAddress(Sendmailaddress, SendDisplayname, Encoding.UTF8);//构造一个发件人地址对象
                MailAddress to = new MailAddress(Recevivemailaddress, ReceviveDisplayname, Encoding.UTF8);//构造一个收件人地址对象                
                MailMessage message = new MailMessage(from, to);//构造一个Email的Message对象

                message.Subject = mailsubject;
                message.SubjectEncoding = Encoding.UTF8;
                message.Body = mailbody;
                message.BodyEncoding = Encoding.UTF8;

                //设置邮件的信息
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = false;

                //如果服务器支持安全连接,则将安全连接设为true。
                //如果是gmail则一定要将其设为true
                if (smtpHost == "smpt.gmail.com")
                    client.EnableSsl = true;
                else
                    client.EnableSsl = false;

                if (isadddocument == true)
                {
                    AddDocument(message, documentpath);
                }
                client.UseDefaultCredentials = false;
                //用户登陆信息
                NetworkCredential myCredentials = new NetworkCredential(Sendmailaddress, Sendpassword);
                client.Credentials = myCredentials;
                //发送邮件
                client.Send(message);
                sendstatus = true; 
            }
            catch { }
            return sendstatus;
        }

        /// <summary>
        /// 添加附件功能
        /// </summary>
        /// <param name="message">Mailmessage对象</param>
        /// <param name="Documentpath">附件路径列表</param>
        private void AddDocument(MailMessage message, List<string> Documentpath)
        {
            foreach(string filepath in Documentpath)
            {
                try
                {
                    if (File.Exists(filepath)) //判断文件是否存在
                    {
                        Attachment attach = new Attachment(filepath);    //构造一个附件对象
                        ContentDisposition disposition = attach.ContentDisposition;   //得到文件的信息
                        disposition.CreationDate = System.IO.File.GetCreationTime(filepath);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(filepath);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(filepath);
                        message.Attachments.Add(attach);   //向邮件添加附件
                    }
                }
                catch { }      
            }             
        }
    }
}

 

posted @ 2016-05-23 16:52    阅读(584)  评论(0编辑  收藏  举报