通过一个简单的控制台应用程序示例,演示如何通过C#发送邮件到QQ邮箱。这个代码使用了.NET的SmtpClient类来发送邮件。
步骤
1. 确保已经获取到QQ邮箱的授权码,作为SMTP的密码使用。(找授权码的具体流程,在文章末尾)
2. 使用以下代码在控制台应用程序中发送邮件。
using System;
using System.Net;
using System.Net.Mail;
namespace SendEmailExample
{
class Program
{
static void Main(string[] args)
{
try
{
// 邮件相关信息
string smtpServer = "smtp.qq.com"; // QQ邮箱的SMTP服务器地址
int port = 587; // QQ邮箱的SMTP服务器端口(使用SSL)
string senderEmail = "your-email@qq.com"; // 发送方邮箱
string senderPassword = "your-auth-code"; // 授权码(不是QQ邮箱密码)
string recipientEmail = "recipient-email@example.com"; // 收件方邮箱
// 创建邮件对象
MailMessage mailMe