在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服务器可能需要额外的配置才能成功发送邮件,如授权码或不同的端口号等。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c#中contains的用法是什么