SMTP - .NET发送邮件

通过SmtpClient,实现.NET框架下的SMTP邮件协议编程

SMTP - .NET发送邮件

1 example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System.Net.Mail;

namespace SMTP
{
/// <summary>
/// A utility provides SMTP mail functions
/// </summary>
public static class SMTPUtility
{
/// <summary>
/// send an email in the name of 'System Notification'(demo.noreply@cn.demo.com) via SMTP
/// </summary>
/// <param name="mailto"></param>
/// <param name="mailBody"></param>
/// <param name="mailSubject"></param>
public static void SendMail(string mailto, string mailBody, string mailSubject)
{
MailMessage MyMail = new MailMessage();
MyMail.From = new MailAddress("demo.noreply@cn.demo.com", "System Notification");
MyMail.To.Add(mailto);
MyMail.Subject = mailSubject;
MyMail.IsBodyHtml = true;
MyMail.Body = mailBody;

SmtpClient smtp = new SmtpClient();
smtp.Host = "hostname or IP address";
smtp.Send(MyMail);
}
}
}

  • 注:MailMessageSmtpClient通过using System.Net.Mail;导入namespace。