.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

相关推荐
light blue bird1 天前
主子端台二分法任务汇总组件
前端·数据库·.net·桌面端winform
rockey6271 天前
基于AScript的python3脚本语言发布啦!
python·c#·.net·script·python3·eval·expression·function·动态脚本
TeamDev1 天前
如何在 DotNetBrowser 中使用本地 AI 模型
前端·后端·.net
唐青枫1 天前
内存为什么越来越高?C#.NET GC 详解:分代回收、LOH、终结器与性能优化实战
c#·.net
日落飞雪1 天前
重塑 .NET 国际化工作流:时光恒Net多语言生成系统,让出海更简单
.net
日落飞雪1 天前
从 .NET 11 到 ARM64:时光恒Net 系统 v1.06 进化全纪实,定义自动化翻译新高度!
.net·wpf开发·net国际化·net本地化·net多语言
rockey6272 天前
AScript之eval函数详解
c#·.net·script·eval·expression·动态脚本
周杰伦fans3 天前
AutoCAD .NET 二次开发:深入理解 EntityJig 的工作原理与正确实现
开发语言·.net
William_cl3 天前
【C#/.NET 进阶】ASP.NET 架构与最佳实践:DI 依赖注入(IoC 核心)从入门到避坑
c#·asp.net·.net
武藤一雄3 天前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf