欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 邮件发送

程序员文章站 2022-04-19 19:28:50
/// /// 邮件发送 /// /// 邮件标题 /// 邮件内容,html格式 /// 收... ......
        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">邮件内容,html格式</param>
        /// <param name="tomaillist">收件人,支持多人</param>
        /// <returns></returns>
        public async task sendmailasync(string subject, string body, list<string> tomaillist)
        {
            try
            {
              
                smtpclient smtpclient = new smtpclient();
                smtpclient.deliverymethod = smtpdeliverymethod.network;//指定电子邮件发送方式
                smtpclient.host = "smtp.exmail.qq.com";//指定smtp服务器
                smtpclient.credentials = new networkcredential(_username, _pwd);//用户名和密码
                smtpclient.enablessl = true;
                mailaddress fromaddress = new mailaddress(_username);//用户名和密码
                mailmessage mailmessage = new mailmessage();
                mailmessage.from = fromaddress;
                tomaillist.foreach(e =>
                {
                    mailmessage.to.add(e);
                });
                mailmessage.subject = subject;//主题
                mailmessage.body = body;//内容
                mailmessage.bodyencoding = encoding.default;//正文编码
                mailmessage.isbodyhtml = true;//设置为html格式
                mailmessage.priority = mailpriority.normal;//优先级
                await smtpclient.sendmailasync(mailmessage);
            }
            catch (exception ex)
            {
               throw ex;
            }
        }
  public static string addemailbody(string username,string upwd )
        {
            stringbuilder sbbody = new stringbuilder();
            sbbody.append($"<div style=\"text-align:left\">感谢您使用本服务,您的登录账号已经开通:<br/>" );
            sbbody.append($"您可使用账号:{username},初始密码:{upwd},登录url:http://***.com<br/>");
            sbbody.append($"用户名、密码均为重要信息,请妥善保管,不得泄露给他人,若出现信息泄露等情况,请及时联系我们。<br/></div>");
            sbbody.append($"<div style=\"text-align:right\">{datetime.now.tostring("yyyy年mm月dd日")}</div>");

            return sbbody.tostring();
        }