Net Core教程

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

 

这篇关于c# 发送邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!