在C#中使用SMTP發送郵件可以通過以下步驟:
using System.Net;
using System.Net.Mail;
SmtpClient client = new SmtpClient("smtp.example.com", 587);
client.Credentials = new NetworkCredential("your_email@example.com", "your_password");
client.EnableSsl = true;
MailMessage message = new MailMessage();
message.From = new MailAddress("your_email@example.com");
message.To.Add("recipient@example.com");
message.Subject = "Subject";
message.Body = "Body";
client.Send(message);
完整的示例代碼如下:
using System;
using System.Net;
using System.Net.Mail;
class Program
{
static void Main()
{
SmtpClient client = new SmtpClient("smtp.example.com", 587);
client.Credentials = new NetworkCredential("your_email@example.com", "your_password");
client.EnableSsl = true;
MailMessage message = new MailMessage();
message.From = new MailAddress("your_email@example.com");
message.To.Add("recipient@example.com");
message.Subject = "Subject";
message.Body = "Body";
client.Send(message);
Console.WriteLine("Email sent successfully.");
}
}
請注意,您需要替換示例中的SMTP服務器、郵箱地址、密碼、收件人地址、主題和正文內容為實際的信息。另外,有些SMTP服務器可能需要額外的配置才能成功發送郵件,如授權碼或不同的端口號等。