使用的是.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);
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章