C# 发送邮件
本文最后更新于 176 天前,其中的信息可能已经有所发展或是发生改变。

前言

本文简单的通过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;
            }
        }
    }
}
本文链接:https://likepoems.com/articles/cshap-send-mail/
转载说明:本站文章若无特别说明,皆为原创,转载请注明来源:likepoems,谢谢!^^

评论

  1. Windows Firefox
    2 月前
    2024-5-22 17:17:36

    这篇文章写得深入浅出,让我这个小白也看懂了!

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇