C# 使用 QQ 邮箱发送邮件

一、概述

软件提醒功能最常用的就是邮箱了,如果使用QQ邮箱的话再搭配 QQ邮箱公众号又可以以最简单的方式实现微信提醒功能。不过在使用QQ邮箱的过程中遇到过一个大坑,按照官方说明使用的 465 端口发邮件经常出错,查了很久发现 465端口已被弃用,要替换成 587 端口~~

后文整理了一个 C# 语言发邮件的类,QQ邮箱亲测正常,其他邮箱未知。

二、程序实例

发邮件类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SystemMonitor
{
    public static class SendEmail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromEmail">发邮件地址</param>
        /// <param name="fromEmailSmtp">发邮件SMTP服务器地址</param>
        /// <param name="fromEmailSmtpPassword">发邮件SMTP服务器密码</param>
        /// <param name="receiveEmails">收件人</param>
        /// <param name="ccEmails">抄送人,没有抄送人员使用 null</param>
        /// <param name="subject">标题</param>
        /// <param name="body">内容</param>
        /// <param name="message">方法返回信息</param>
        /// <returns></returns>
        public static bool SendMail(string fromEmail,string fromEmailSmtp, string fromEmailSmtpPassword, List<string> receiveEmails, List<string> ccEmails, string subject, string body, out string message)
        {
            bool result = false;

            #region 检查配置文件是否设置

            if (String.IsNullOrEmpty(fromEmail))
            {
                message = "发件箱不能为空!设置信息:" + fromEmail;
                return result;
            }

            if (String.IsNullOrEmpty(fromEmailSmtp))
            {
                message = "发件箱SMTP服务器地址不能为空!设置信息:" + fromEmailSmtp;
                return result;
            }

            if (String.IsNullOrEmpty(fromEmailSmtpPassword))
            {
                message = "发件箱SMTP服务器密码或授权码不能为空!设置信息:" + fromEmailSmtpPassword;
                return result;
            }

            if (receiveEmails.Count <= 0)
            {
                message = "收件箱不能为空!";
                return result;
            }

            #endregion

            try
            {
                //实例化一个发送邮件类。
                MailMessage mailMessage = new MailMessage();
                //发件人邮箱地址,方法重载不同,可以根据需求自行选择。
                mailMessage.From = new MailAddress(fromEmail);
                //收件人邮箱地址。
                foreach (var email in receiveEmails)
                {
                    mailMessage.To.Add(new MailAddress(email));
                }

                if (ccEmails != null)//没有抄送人员
                {
                    //抄送人邮箱地址
                    foreach (var email in ccEmails)
                    {
                        mailMessage.CC.Add(new MailAddress(email));
                    }
                }

                //邮件标题。
                mailMessage.Subject = subject;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                //邮件内容。
                mailMessage.Body = body;
                mailMessage.BodyEncoding = Encoding.UTF8;
                //是否是html格式
                mailMessage.IsBodyHtml = true;
                

                //实例化一个SmtpClient类。
                using (SmtpClient client = new SmtpClient())
                {
                    //在这里我使用的是qq邮箱,所以是smtp.qq.com,如果你使用的是126邮箱,那么就是smtp.126.com。
                    client.Host = fromEmailSmtp;
                    //使用安全加密连接。
                    client.EnableSsl = true;
                    client.Port = 587;//456 端口已经被弃用了,参考:https://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection-net-io-connect
                    //不和请求一块发送。
                    client.UseDefaultCredentials = false;
                    //验证发件人身份(发件人的邮箱,邮箱里的生成授权码);
                    client.Credentials = new NetworkCredential(fromEmail, fromEmailSmtpPassword);
                    //发送
                    client.Send(mailMessage);
                }
            }
            catch (Exception ex)
            {
                message = "发送邮件出现异常!\n输入参数:" + String.Join(",", receiveEmails) + " | " + subject + "\n详细异常信息:" + ex.ToString();
                return result;
            }

            result = true;
            message = DateTime.Now.ToString() + " 发送邮件成功!";
            return result;
        }
    }
}

调用方式:<br />**

SendEmail.SendMail("[email protected]", "smtp.qq.com", "授权码",new List<string>() {"[email protected]", "[email protected]"}, null,"异常说明!" , "", out message);

三、获取QQ邮箱授权码

在“设置”→ “账户”中开启 SMTP 服务:

76130-08e943e6705e1314.png
76130-2b0bb5a4fbe51a01.png

发送短信获取授权码:

76130-c39b383ac45a1779.png
76130-c24d14ba4cfb98cf.png

四、参考文章

【1】SmtpException: Unable to read data from the transport connection: net_io_connectionclosed


本文为原创文章,转载请注明出处!欢迎关注任前程博客 https://renqiancheng.com,第一时间看后续精彩文章。

猜你喜欢

转载自blog.csdn.net/weixin_33963594/article/details/86801240