using System;
using System.Net;
using System.Net.Mail;
class Program
{
static void Main()
{
string fromAddress = "sandy.gan@outlook.com";
string toAddress = "pwgan@126.com";
string subject = "Test Email";
string body = "<html><a href='http://10.1.1.104'>点击这里访问示例网站</a></html>";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient("smtp.office365.com", 587))
{
smtp.Credentials = new NetworkCredential("sandy.gan@outlook.com", "邮箱密码"); //使用应用专用密码或授权码: 如果你的邮箱开启了两步验证,你需要生成一个应用专用密码或者使用授权码来代替登录密码。在代码中,NetworkCredential 的密码应该是应用专用密码或者授权码。
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
try
{
smtp.Send(mail);
Console.WriteLine("Email sent successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
}
}
C#发送邮件代码
最新推荐文章于 2024-06-12 13:35:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)