编程语言
首页 > 编程语言> > c# 发送邮件

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();
            }
        }
    }

 

标签:Web,string,c#,cdo,发送,static,mail,com,邮件
来源: https://www.cnblogs.com/aitong/p/16198546.html