发送邮件

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//添加的命名空间引用
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Mail;

namespace SendEmail
{
    public class Send
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromEmail">发送者邮箱地址</param>
        /// <param name="fromPsaaWord">发送者邮箱密码</param>
        /// <param name="toEmail">收件人邮箱地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件主要内容</param>
        /// <param name="attachmentsPath">邮件附件(无附件时为null)</param>
        /// <returns>发送成功返回true,失败false</returns>
        public bool SendEmail(string fromEmail, string fromPsaaWord, string toEmail, string subject, string body, string[] attachmentsPath)
        {
            bool Success = true;
            System.Text.StringBuilder errorMsg = new System.Text.StringBuilder();
            if (!Regex.IsMatch(fromEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
            {
                errorMsg.Append("参数fromEmail格式不正确!");
                Success = false;
            }
            if (!Regex.IsMatch(toEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
            {
                errorMsg.Append("参数toEmail格式不正确!");
                Success = false;
            }
            if (subject.Trim() == "")
            {
                errorMsg.Append("不支持无主题的邮件!");
                Success = false;
            }
            if (body.Trim() == "")
            {
                errorMsg.Append("不支持无内容的邮件!");
                Success = false;
            }
            try
            {
                SendReady(fromEmail, fromPsaaWord, toEmail, subject, body, attachmentsPath);
            }
            catch (Exception err)
            {
                Success = false;
                throw new Exception(errorMsg.ToString() + err);
            }
            return Success;
        }

        public void SendReady(string fromEmail, string fromPsaaWord, string toEmail, string subject, string body, string[] attachmentsPath)
        {
            //验证合法邮箱的表达式,正确时返回true
            if (!Regex.IsMatch(fromEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
            {
                //邮箱不合法
            }
            //发件人地址
            MailAddress from = new MailAddress(fromEmail);
            //收件人地址
            MailAddress to = new MailAddress(toEmail);
            //邮件主题、内容
            MailMessage message = new MailMessage(from, to);
            //主题
            message.Subject = subject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            //内容
            message.Body = body;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            //在有附件的情况下添加附件
            try
            {
                if (attachmentsPath != null && attachmentsPath.Length > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in attachmentsPath)
                    {
                        attachFile = new Attachment(path);
                        message.Attachments.Add(attachFile);
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("在添加附件时有错误:" + err);
            }
            try
            {
                //大部分邮件服务器均加smtp.前缀
                SmtpClient client = new SmtpClient("smtp." + from.Host);
                SendMail(client, from, fromPsaaWord, to, message);
            }
            catch (SmtpException err)
            {
                //如果错误原因是没有找到服务器,则尝试不加smtp.前缀的服务器
                if (err.StatusCode == SmtpStatusCode.GeneralFailure)
                {
                    try
                    {
                        //有些邮件服务器不加smtp.前缀
                        SmtpClient client = new SmtpClient(from.Host);
                        SendMail(client, from, fromPsaaWord, to, message);
                    }
                    catch (SmtpException)
                    { }
                }
                else
                { }
            }
        }
        //根据指定的参数发送邮件
        public void SendMail(SmtpClient client, MailAddress from, string password, MailAddress to, MailMessage message)
        {
            //不使用默认凭证,注意此句必须放在client.Credentials的上面
            client.UseDefaultCredentials = false;
            //指定用户名、密码
            client.Credentials = new NetworkCredential(from.Address, password);
            //邮件通过网络发送到服务器
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                client.Send(message);
            }
            catch
            {
                throw new Exception("系统繁忙,发送失败!");
            }
            finally
            {
                //及时释放占用的资源
                message.Dispose();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值