在ASP.NET中,有多種方法可以發送電子郵件:
using System.Net.Mail;
// 創建SmtpClient實例
SmtpClient client = new SmtpClient();
client.Host = "smtp.example.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("your-email@example.com", "your-password");
// 創建MailMessage實例
MailMessage message = new MailMessage();
message.From = new MailAddress("your-email@example.com");
message.To.Add("recipient@example.com");
message.Subject = "Subject";
message.Body = "Email body";
// 發送電子郵件
client.Send(message);
using System.Web.Mail;
// 創建MailMessage實例
MailMessage message = new MailMessage();
message.From = "your-email@example.com";
message.To = "recipient@example.com";
message.Subject = "Subject";
message.Body = "Email body";
// 發送電子郵件
SmtpMail.SmtpServer = "smtp.example.com";
SmtpMail.Send(message);
// 使用MailKit發送電子郵件
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
// 創建MimeMessage實例
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress("Your Name", "your-email@example.com"));
message.To.Add(new MailboxAddress("Recipient", "recipient@example.com"));
message.Subject = "Subject";
message.Body = new TextPart("plain") { Text = "Email body" };
// 發送電子郵件
using (SmtpClient client = new SmtpClient())
{
client.Connect("smtp.example.com", 587, SecureSocketOptions.StartTls);
client.Authenticate("your-email@example.com", "your-password");
client.Send(message);
client.Disconnect(true);
}
請注意,上面的代碼示例中的SMTP服務器詳細信息和身份驗證憑據應根據您自己的情況進行修改。