.NET 邮件发送 SMTP邮件发送

发布于:2024-04-20 ⋅ 阅读:(25) ⋅ 点赞:(0)

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服务器需要身份验证。

以下是示例代码:

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}");
}
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 

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

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


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

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