使用的是.NET Framework 4.0,并且需要使用SMTP协议发送电子邮件

using System;
using System.Net;
using System.Net.Mail;

class Program
{
    static void Main(string[] args)
    {
        string smtpHost = "<your-smtp-host>";
        int smtpPort = 587;
        string smtpUsername = "<your-smtp-username>";
        string smtpPassword = "<your-smtp-password>";
        string fromEmail = "<from-email-address>";
        string toEmail = "<to-email-address>";
        string subject = "Test Email";
        string body = "This is a test email.";

        using (SmtpClient client = new SmtpClient(smtpHost, smtpPort))
        {
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(smtpUsername, smtpPassword);

            MailMessage message = new MailMessage(fromEmail, toEmail, subject, body);

            try
            {
                client.Send(message);
                Console.WriteLine("Email sent successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to send email: " + ex.Message);
            }
        }
    }
}

 

posted @ 2024-04-12 15:46  shiningrise  阅读(12)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css