C#发送邮件(附件)类

本博客主要利用C#中System.Net.Mail中的MailMessage 类进行邮件的发送。话不多说直接上代码:

首先需要添加引用,不清楚的可以去https://msdn.microsoft.com 上去查看详细介绍

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net.Mime;

类主体如下:

/// 
/// 发送内邮(收件人、抄送人、附件若有多个,用分号[;]隔开)
/// 
/// 收件人
/// 抄送人
/// 邮件主题
/// 邮件内容
/// 附件路径
public static bool SendMail(string toMail, string ccMail, string subject, string content, string filepath)
{
     try
            {
                var emailAcount = ConfigurationManager.AppSettings["EmailAcount"];
                var emailPassword = ConfigurationManager.AppSettings["EmailPassword"];
                var emailServer = ConfigurationManager.AppSettings["SMTPServer"];
                var emailServerPort = ConfigurationManager.AppSettings["SMTPServerPort"];
                MailMessage message = new MailMessage();
                //设置发件人,163邮箱为例,需要在Web.config中进行配置
                MailAddress fromAddr = new MailAddress(string.Format("{0}@163.com", emailAcount));
                message.From = fromAddr;

                //遍历收件人邮箱地址     
                if (toMail.Trim().Length != 0)
                {
                    string[] receivers = toMail.Trim().Split(';');
                    for (int i = 0; i < receivers.Length; i++)
                    {
                        if (receivers[i].Length > 0)
                        {
                            message.To.Add(receivers[i]);//为该邮件添加联系人  
                        }
                    }
                }
                //遍历抄送人邮箱地址
                if (ccMail.Trim().Length != 0)
                {
                    string[] ccreceivers = ccMail.Trim().Split(';');
                    for (int j = 0; j < ccreceivers.Length; j++)
                    {
                        if (ccreceivers[j].Length > 0)
                        {
                            message.CC.Add(ccreceivers[j]);//为该邮件添加抄送人  
                        }
                    }
                }
                //设置邮件标题
                message.Subject = subject;
                //设置邮件内容
                message.Body = content;

                //添加附件
                if(filepath.Trim().Length != 0)
                {
                    string[] attachFiles = filepath.Trim().Split(';');
                    for(int k = 0; k < attachFiles.Length; k++)
                    {
                        if (attachFiles[k].Length > 0)
                        {
                            Attachment data = new Attachment(attachFiles[k], MediaTypeNames.Application.Octet);
                            message.Attachments.Add(data);
                        }
                    }
                }

                //设置邮件SMTP服务器
                //SmtpClient client = new SmtpClient("smtp.163.com", 25);
                SmtpClient client = new SmtpClient(emailServer, Convert.ToInt32(emailServerPort));
                //设置发送人的邮箱账号和密码
                client.Credentials = new NetworkCredential(emailAcount, emailPassword);
                //启用ssl,也就是安全发送
                client.EnableSsl = true;
                ServicePointManager.ServerCertificateValidationCallback =
    delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
                //发送邮件
                client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                log.Error("SendMail()", ex);
                return false;
            }
}



你可能感兴趣的:(C#)