发送邮件

using System;
using System.Data;
using System.Configuration;
using System.Net.Mail;
/** <summary>
/// sendMail 的摘要说明
/// </summary>
public class Mail
{
    /// <summary>
    /// 发送邮件 For Ex:MessageBox.Show(Mail.Send("zhaoyao@solutionkeys.com", "zhaoyao@solutionkeys.com", "test head", false, "body", "mail.solutionkeys.com", null, null,"c://sp.txt,c://sp.txt").ToString());
    /// </summary>
    /// <param name="from">发送人邮件地址</param>
    /// <param name="to">接收人邮件地址</param>
    /// <param name="subject">邮件主题</param>
    /// <param name="isBodyHtml">是否是Html</param>
    /// <param name="body">邮件体</param>
    /// <param name="smtpHost">SMTP服务器地址,例如:smtp.163.com</param>
    /// <param name="userName">用户名,不需要身份验证时使用null</param>
    /// <param name="password">密码,不需要身份验证时使用null</param>
    /// <param name="attachments">附件地址列表,以“,”分割</param>
    /// <returns>是否成功</returns>
    public static bool Send(string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password,string attachments)
    {
        string[] ts = to.Split(',');
        bool isSuccess = true;
        foreach (string t in ts)
        {
            try
            {
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress(from);
                mm.To.Add(new MailAddress(t.Trim()));
                mm.Subject = subject;
                mm.IsBodyHtml = isBodyHtml;
                mm.Body = body;
                #region 附件
                if (attachments != null)
                {
                    if (attachments.Contains("|"))
                    {
                        string[] arr = attachments.Split("|".ToCharArray());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if(System.IO.File.Exists(arr[i]))
                            {
                                Attachment a = new Attachment(arr[i]);
                                mm.Attachments.Add(a);
                            }
                        }
                    }
                    else
                    {
                        if (System.IO.File.Exists(attachments))
                        {
                            Attachment a = new Attachment(attachments);
                            mm.Attachments.Add(a);
                        }
                    }
                }
                #endregion
                SmtpClient sc = new SmtpClient(smtpHost);
                sc.Host = smtpHost;
                //smtpClient.EnableSsl = true;//如果服务器不支持ssl则报,服务器不支持安全连接 错误
                if (!(string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)))
                {
                    sc.UseDefaultCredentials = true;//winform中不受影响,asp.net中,false表示不发送身份严正信息
                    sc.Credentials = new System.Net.NetworkCredential(userName, password);
                }
                sc.Send(mm);
            }
            catch
            {
                isSuccess = false;
            }
        }
        return isSuccess;
    }


    /// <summary>
    /// 发送邮件 For Ex:MessageBox.Show(Mail.Send("zhaoyao@solutionkeys.com", "zhaoyao@solutionkeys.com", "test head", false, "body", "mail.solutionkeys.com", null, null,new string[] { "c://sp.txt", "c://sp.txt" }).ToString());
    /// </summary>
    /// <param name="from">发送人邮件地址</param>
    /// <param name="to">接收人邮件地址列表,以“,”分割</param>
    /// <param name="subject">邮件主题</param>
    /// <param name="isBodyHtml">是否是Html</param>
    /// <param name="body">邮件体</param>
    /// <param name="smtpHost">SMTP服务器地址,例如:smtp.163.com</param>
    /// <param name="userName">用户名,不需要身份验证时使用null</param>
    /// <param name="password">密码,不需要身份验证时使用null</param>
    /// <param name="attachments">附件地址列表</param>
    /// <returns>是否成功</returns>
    public static bool Send(string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password, string[] attachments)
    {
        string[] ts = to.Split(',');
        bool isSuccess = true;
        foreach (string t in ts)
        {
            try
            {
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress(from);
                mm.To.Add(new MailAddress(t.Trim()));
                mm.Subject = subject;
                mm.IsBodyHtml = isBodyHtml;
                mm.Body = body;
                #region 附件
                if (attachments != null)
                {
                    for (int i = 0; i < attachments.Length; i++)
                    {
                        if (System.IO.File.Exists(attachments[i]))
                        {
                            Attachment a = new Attachment(attachments[i]);
                            mm.Attachments.Add(a);
                        }
                    }
                }
                #endregion
                SmtpClient sc = new SmtpClient(smtpHost);
                sc.Host = smtpHost;
                //smtpClient.EnableSsl = true;//如果服务器不支持ssl则报,服务器不支持安全连接 错误
                if (!(string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)))
                {
                    sc.UseDefaultCredentials = true;//winform中不受影响,asp.net中,false表示不发送身份严正信息
                    sc.Credentials = new System.Net.NetworkCredential(userName, password);
                }
                sc.Send(mm);
            }
            catch
            {
                isSuccess = false;
            }
        }
        return isSuccess;
    }

    /// <summary>
    /// 发送邮件 For Ex:Mail.Send("zhaoyao@solutionkeys.com", new string[] { "zhaoyao@solutionkeys.com", "zyip@qq.com" }, "test attment and mutile receive", false, "body", "mail.solutionkeys.com", null, null, new string[] { "c://sp.txt", "c://sp.txt" })
    /// </summary>
    /// <param name="from">发送人邮件地址</param>
    /// <param name="to">接收人邮件地址列表</param>
    /// <param name="subject">邮件主题</param>
    /// <param name="isBodyHtml">是否是Html</param>
    /// <param name="body">邮件体</param>
    /// <param name="smtpHost">SMTP服务器地址,例如:smtp.163.com</param>
    /// <param name="userName">用户名,不需要身份验证时使用null</param>
    /// <param name="password">密码,不需要身份验证时使用null</param>
    /// <param name="attachments">附件地址列表</param>
    /// <returns>是否成功</returns>
    public static bool Send(string from, string[] to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password, string[] attachments)
    {
        bool isSuccess = true;
        try
        {
            MailMessage mm = new MailMessage();
            mm.From = new MailAddress(from);
            for (int i = 0; i < to.Length; i++)
            {
                mm.To.Add(new MailAddress(to[i]));
            }
            mm.Subject = subject;
            mm.IsBodyHtml = isBodyHtml;
            mm.Body = body;
            #region 附件
            if (attachments != null)
            {
                for (int i = 0; i < attachments.Length; i++)
                {
                    if (System.IO.File.Exists(attachments[i]))
                    {
                        Attachment a = new Attachment(attachments[i]);
                        mm.Attachments.Add(a);
                    }
                }
            }
            #endregion
            SmtpClient sc = new SmtpClient(smtpHost);
            sc.Host = smtpHost;
            //smtpClient.EnableSsl = true;//如果服务器不支持ssl则报,服务器不支持安全连接 错误
            if (!(string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)))
            {
                sc.UseDefaultCredentials = true;//winform中不受影响,asp.net中,false表示不发送身份严正信息
                sc.Credentials = new System.Net.NetworkCredential(userName, password);
            }
            sc.Send(mm);
        }
        catch
        {
            isSuccess = false;
        }

        return isSuccess;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值