c# 发送邮件

    public class MailHelper
    {
        private static string fromAddr = "from@xx.com";//发送邮箱
        private static string fromPwd = "abcd";//发送邮箱密码
        private static string smtpAddr = "smtp.exmail.qq.com";//smtp地址,样例使用的是腾讯企业邮箱地址
        private static int smtpPort = 465;//smtp端口号
        private static string usessl = "true";//是否使用ssl加密

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="toAddr">接收邮箱</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        public static void SendEmailByWebMail(string toAddr, string subject, string body)
        {
            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            try
            {
                mail.To = toAddr;
                mail.From = fromAddr;
                mail.Subject = "邮件测试";
                mail.BodyFormat = System.Web.Mail.MailFormat.Text;
                mail.Body = "测试正文";
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", fromAddr);
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", fromPwd);
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", smtpPort);
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", usessl);
                System.Web.Mail.SmtpMail.SmtpServer = smtpAddr;
                System.Web.Mail.SmtpMail.Send(mail);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
    }

 

posted @ 2022-04-27 13:24  echo三毛  阅读(38)  评论(0编辑  收藏  举报