在ASP.NET中,發送郵件有兩種常見的方法:
using System.Net;
using System.Net.Mail;
public void SendEmail(string to, string subject, string body)
{
// 創建SMTP客戶端
SmtpClient smtpClient = new SmtpClient("smtp.example.com", 25);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("sender@example.com", "password");
// 創建郵件
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender@example.com");
mailMessage.To.Add(new MailAddress(to));
mailMessage.Subject = subject;
mailMessage.Body = body;
// 發送郵件
smtpClient.Send(mailMessage);
}
using System.Web.Mail;
public void SendEmail(string to, string subject, string body)
{
// 創建郵件
MailMessage mailMessage = new MailMessage();
mailMessage.From = "sender@example.com";
mailMessage.To = to;
mailMessage.Subject = subject;
mailMessage.Body = body;
// 發送郵件
SmtpMail.SmtpServer = "smtp.example.com";
SmtpMail.Send(mailMessage);
}
這兩種方法都可以用于發送郵件,具體選擇哪種方法取決于個人的需求和偏好。