在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服务器详细信息和身份验证凭据应根据您自己的情况进行修改。