溫馨提示×

C#中如何實現SMTP郵件的自動回復功能

c#
小樊
134
2024-08-18 05:14:41
欄目: 編程語言

在C#中實現SMTP郵件的自動回復功能,可以通過使用SmtpClient類和MailMessage類來發送自動回復郵件。以下是一個簡單的示例代碼:

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

class Program
{
    static void Main()
    {
        string host = "smtp.example.com"; // SMTP服務器地址
        int port = 587; // SMTP服務器端口
        string userName = "your_email@example.com"; // 發件人郵箱
        string password = "your_password"; // 發件人郵箱密碼
        string from = "your_email@example.com"; // 發件人郵箱
        string to = "recipient@example.com"; // 收件人郵箱
        string subject = "自動回復"; // 郵件主題
        string body = "這是自動回復郵件"; // 郵件內容

        // 創建MailMessage對象
        MailMessage mail = new MailMessage(from, to, subject, body);

        // 創建SmtpClient對象
        SmtpClient client = new SmtpClient(host, port)
        {
            Credentials = new NetworkCredential(userName, password),
            EnableSsl = true
        };

        try
        {
            // 發送郵件
            client.Send(mail);
            Console.WriteLine("自動回復郵件發送成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine("發送郵件失?。?quot; + ex.Message);
        }
    }
}

以上代碼演示了如何使用C#發送一封簡單的自動回復郵件,可以根據實際需求添加更多功能,如設置郵件的附件、抄送、密送等。請注意,發送郵件需要提供正確的SMTP服務器地址、端口、發件人郵箱、密碼等信息。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女