在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);
}
这两种方法都可以用于发送邮件,具体选择哪种方法取决于个人的需求和偏好。