C#邮件收发

    public class Mail
    {
        private string mailkey;
        private string mailSecret;
        public string mailFrom;

        public Mail()
        {
            this.mailkey = BaseHelper.GetValue("MailKey");
            this.mailSecret = BaseHelper.GetValue("MailSecret");
            this.mailFrom = BaseHelper.GetValue("MailFrom");
        }

        public Mail(string key,string secret,string username)
        {
            this.mailkey = key;
            this.mailSecret = secret;
            this.mailFrom = username;
        }

        /// <summary>
        /// 群发邮件
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="toUsers">接收方</param>
        /// <returns></returns>
        public ResultDTO Send(string title,string content,string[] toUsers)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = title;
                mail.From = new MailAddress(this.mailkey,this.mailFrom);
                foreach (var user in toUsers)
                {
                    mail.To.Add(new MailAddress(user));
                }
                mail.Body = content;

                mail.BodyEncoding = Encoding.UTF8;
                mail.IsBodyHtml = true;
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.163.com";
                client.Port = 25;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(this.mailkey,this.mailSecret);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(mail);
                return WebApi.Success("邮件发送成功!");
            }
            catch(Exception e)
            {
                return WebApi.Error(e.Message);
            }
        }

        /// <summary>
        /// 单发邮件
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="toUser"></param>
        /// <returns></returns>
        public ResultDTO Send(string title, string content, string toUser)
        {
            return Send(title, content, new[] {toUser});
        }
    }

  

转载于:https://www.cnblogs.com/xuhang/p/5204957.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值