C# 发送邮件

前言

本文简单的通过C#的SmtpClient实现发送邮件的功能。

实现

using System.Net.Mail;
using System.Net;

namespace Test
{
   
    public class SendMail
    {
   
        private SmtpClient _emailClient;
        
        public class UserInfo {
   
            public string Email {
    get; set; } // 用户的邮箱
            public string DisplayName {
    get; set; } // 用户的显示名称
        }
        
        /// <summary>
        /// 发送邮件(邮件主题,邮件内容,接收人,抄送人,密送人,附件)
        /// </summary>
        public string SendMail(
            string subject,
            string body,
            ICollection<UserInfo> tos,
            ICollection<UserInfo>? ccs = null,
            ICollection<UserInfo>? bccs = null,
            ICollection<Attachment>? attachments = null)
        {
   

            // 设置smtp配置信息(以QQ邮箱为例)
            string host = "smtp.qq.com"; // 邮件服务器
            int port = 587; // 端口号
            bool enableSsl = true; // 是否开启ssl
            string username = "yourqqemail"; // 你的邮箱
            string password = "yourpassword"; // 你的邮箱授权码
            string senderAddress = "yourqqemail";// 你的邮箱
            string senderDisplayName = "test"; // 发件人那里展示的名字

            // 实例化smtp服务器
            _emailClient = new SmtpClient
            {
   
                // 配置好服务器地址,端口和ssl,使用账密验证
                Host = host,
                Port = port,
                EnableSsl = enableSsl,
                Credentials = new NetworkCredential {
    UserName = username, Password = password }
            };
            MailMessage message = new()
            {
   
                // 设置邮件发送人
                From = new MailAddress(senderAddress, senderDisplayName) 
            };

            // 接收人
            foreach (UserInfo user in tos)
            {
   
                message.To.Add(new MailAddress(user.Email, user.DisplayName));
            }
            // 抄送人
            if (ccs != null)
            {
   
                foreach (UserInfo user in ccs)
                {
   
                    message.CC.Add(new MailAddress(user.Email, user.DisplayName));
                }
            }
            // 密送人
            if (bccs != null)
            {
   
                foreach (UserInfo user in bccs)
                {
   
                    message.Bcc.Add(new MailAddress(user.Email, user.DisplayName));
                }
            }
            // 附件
            if (attachments != null)
            {
   
                foreach (Attachment attachment in attachments)
                {
   
                    message.Attachments.Add(attachment);
                }
            }
            // 主题/内容/是否是html/默认优先级
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;
            message.Priority = MailPriority.Normal;

            try
            {
   
                _emailClient.Send(message);
                return "邮件发送成功";
            }
            catch (Exception ex)
            {
   
                return ex.Message;
            }
        }
    }
}

相关推荐

  1. C# 发送邮件

    2024-02-02 20:16:01       34 阅读
  2. Golang- 邮件服务,发送邮件

    2024-02-02 20:16:01       21 阅读
  3. golang发送邮件

    2024-02-02 20:16:01       24 阅读
  4. 使用golang发送邮件

    2024-02-02 20:16:01       49 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-02 20:16:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-02 20:16:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-02 20:16:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-02 20:16:01       20 阅读

热门阅读

  1. 《Vite 基础知识》基于 Vite 的组件库框架搭建

    2024-02-02 20:16:01       37 阅读
  2. c语言 ceil() 函数

    2024-02-02 20:16:01       29 阅读
  3. 跨域访问支持(Spring Boot、Nginx、浏览器)

    2024-02-02 20:16:01       33 阅读
  4. 手写实现call、apply和bind函数

    2024-02-02 20:16:01       30 阅读
  5. Unity之延迟函数

    2024-02-02 20:16:01       35 阅读
  6. SpringBoot+JdbcTempalte+SQLServer

    2024-02-02 20:16:01       32 阅读