C# 控制台发送邮件示例原创
4人赞赏了该文章
154次浏览
编辑于2024年03月16日 08:35:29
//VS版本2022
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
// 创建一个新的邮件对象
MailMessage mail = new MailMessage();
// 设置发件人
mail.From = new MailAddress("123456@qq.com", "发件名称:我的qq我做主");
// 设置收件人
mail.To.Add("123456@qq.com");
// 设置邮件主题
mail.Subject = "test邮件主题";
// 设置邮件正文
mail.Body = "test,你好1234567890!!";
// 设置邮件优先级
mail.Priority = MailPriority.High;
// 设置邮件是否需要HTML格式
mail.IsBodyHtml = true;
// 设置附件
// Attachment attachment = new Attachment("path-to-attachment-file");
// mail.Attachments.Add(attachment);
// 创建一个SMTP客户端
SmtpClient client = new SmtpClient();
// 设置SMTP服务器的地址和端口
client.Host = "smtp.qq.com";
client.Port = 587; // 通常为25、465或587,取决于你的邮件服务提供商
// 设置SMTP服务器的认证方式
client.EnableSsl = true; // 如果SMTP服务器需要SSL加密,则设置为true
client.Credentials = new NetworkCredential("123456@qq.com", "我的密码");
// 发送邮件
try
{
client.Send(mail);
Console.WriteLine("邮件发送成功!");
}
catch (Exception ex)
{
Console.WriteLine("邮件发送失败:" + ex.Message);
}
}
}
}
ConsoleApp1.zip(16.48KB)
推荐阅读