.NET 邮件发送 SMTP邮件发送

SMTP(Simple Mail Transfer Protocol)是用于电子邮件传输的规则集,可以从邮件客户端向接收电子邮件服务器发送、中继或转发邮件。发件人可使用SMTP 服务器来执行发送电子邮件的过程。SMTP服务器则是按照这些规则中转电子邮件的服务器。

IMAP可以理解为收邮件。

🐧使用QQ邮箱发邮件

首先需要设置开启邮箱的SMTP服务

登录(https://mail.qq.com/)电脑网页版邮箱进入【设置】->【帐户】->【POP3/IMAP/SMTP服务】, 开启或关闭相应服务最后保存更改即可。

QQ邮箱 POP3 和 SMTP 服务器地址设置如下:

邮箱 POP3服务器(端口995) SMTP服务器(端口465或587)
qq.com pop.qq.com smtp.qq.com

SMTP服务器需要身份验证。

以下是示例代码:

cs 复制代码
using ConsoleApp1Test;
//xxx
string server = "smtp.qq.com";
string username = "my test email";
string password = "xxx;
string from = "from@qq.com";
string to =   "to@qq.com";
string subject = "Test Email";
string content = "This is a test email sent asynchronously.";
bool isHtml = false; // 是否为 HTML 格式

try
{
    bool success = await MailHelper. SendMailAsync(server, username, password, from, to, null, subject, content, isHtml);
    if (success)
    {
        Console.WriteLine("邮件发送成功!");
    }
    else
    {
        Console.WriteLine("邮件发送失败!");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"邮件发送出错:{ex.Message}");
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1Test
{
    internal class MailHelper
    {

    

      public static async Task<bool> SendMailAsync(string server, string username, string password, string from, string to, string cc, string subject, string content, bool isHtml)
    {
        try
        {
            using (var smtp = new SmtpClient(server))
            {
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(username, password);
                smtp.EnableSsl = true; // 启用加密
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                using (var mail = new MailMessage())
                {
                    mail.From = new MailAddress(from);
                    mail.To.Add(to);
                    mail.SubjectEncoding = Encoding.UTF8;
                    mail.Subject = subject;
                    mail.IsBodyHtml = isHtml;
                    mail.BodyEncoding = Encoding.UTF8;
                    mail.Body = content;

                    await smtp.SendMailAsync(mail); // 异步发送邮件
                }

                return true;
            }
        }
        catch (Exception err)
        {
            // 发送失败时的异常处理
            // 可以在此处记录日志
            return false;
        }
    }

}
}

🐷使用网易邮箱发送邮件

163网易免费邮

设置 > POP3/SMTP/IMAP

使用网易邮箱发送邮件上述示例类似,只需替换相应的服务器地址、用户名、密码、发件人、收件人、主题、内容等信息即可。

cs 复制代码
string server = "smtp.163.com";
string username = "f@163.com";
string password = "xxx";
string from = "f@163.com";
string to = "t@qq.com";
string subject = "Test163Email m";
string content = "This is a test email ";
bool isHtml = false; // 是否为 HTML 格式

运行:

🐬使用谷歌邮箱发送邮件

谷歌Gmail邮箱登陆地址:https://mail.google.com

谷歌imap开通 smtp也自动开通

https://myaccount.google.com/

|-----------------|--------------------------------------------------------------------------|
| 接收邮件 (IMAP) 服务器 | imap.gmail.com要求 SSL:是端口:993 |
| 发送邮件 (SMTP) 服务器 | smtp.gmail.com要求 SSL:是要求 TLS:是(如适用)使用身份验证:是SSL 端口:465TLS/STARTTLS 端口:587 |

使用谷歌邮箱修改对应的服务器地址、用户名、密码、发件人、收件人、主题、内容等信息即可。

cs 复制代码
string server = "smtp.gmail.com";
string username = "f@gmail.com";
string password = "xx";
string from = "f@gmail.com";
string to = "t@qq.com";
string subject = "TestSMTPEmail m";
string content = "This is a test email sent using Gmail SMTP.m";
bool isHtml = false; // 是否为 HTML 格式

运行:

📮有些免费邮箱对发信量有限制,可使用企业邮,多账号增加发信量。

END

相关推荐
硅基喵3 小时前
.NET 进阶之路:异步、并发与内存管理的系统性认知
.net
俊俊谢3 小时前
LabVIEW如何排查和修复dll缺失问题
驱动开发·.net·labview·dll
武藤一雄8 小时前
C# 设计模式大全(第一弹|7种)
microsoft·设计模式·微软·c#·.net·.netcore
麦壳饼11 小时前
JekyllNet .Net 版本的Jekyll , 你博客 文档的静态生成利器 。
.net
步步为营DotNet12 小时前
.NET 11 中 ASP.NET Core 10 在分布式系统中的安全通信与性能调优
安全·asp.net·.net
唐青枫13 小时前
C#.NET Consul + Steeltoe 深入解析:服务注册发现、健康检查与微服务接入
c#·.net
我是唐青枫16 小时前
C#.NET ConcurrentQueue<T> 深入解析:无锁队列原理、FIFO 语义与使用边界
c#·.net
时光追逐者16 小时前
一个基于 .NET Core + Vue3 构建的开源全栈平台 Admin 系统
开源·c#·.net·.netcore·admin系统
追逐时光者1 天前
一个基于 .NET Core + Vue3 构建的开源全栈平台 Admin 系统
后端·.net
light blue bird1 天前
多页签Razor组支轴业务整顿组件
数据库·.net·ai大数据·多功能图表报表·web mvc + razor