Spire.email 支持发送纯文本文件、HTML邮件以及带附件的邮件等,本文将详细介绍如何用SMTP发送HTML邮件。
C#
//实例化一个MailMessgae对象,指定发件人邮箱地址及收件人邮箱地址
MailAddress addressFrom = new MailAddress("Nina.Tang @e-iceblue.com", "Nina Tang");
MailAddress addressTo = new MailAddress("Simon.Yang @e-iceblue.com");
MailMessage message = new MailMessage(addressFrom, addressTo);
//设置邮件的创建日期,主题以及html文本
message.Date = DateTime.Now;
message.Subject = "年终晚会出席确认";
string htmlString = @"
<html>
<body>
<p>您好!</p>
<p>非常感谢您邀请我参加2月2日(周五)晚8点在贵公司总部举办的年终晚会。我很乐意参加,期待在晚会上见到您和您的同事们。</p>
<p>预祝公司年会圆满成功。<br>Nina<br>2018年1月12日</p>
</body>
</html>"
message.BodyHtml = htmlString;
//实例化一个SmtpClient对象,设置邮件服务器、端口、发件人邮箱及密码
SmtpClient client = new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
//发送邮件
client.SendOne(message);
VB.NET
'实例化一个MailMessgae对象,指定发件人邮箱地址及收件人邮箱地址
Dim addressFrom As New MailAddress("Nina.Tang @e-iceblue.com", "Nina Tang")
Dim addressTo As New MailAddress("Simon.Yang @e-iceblue.com")
Dim message As New MailMessage(addressFrom, addressTo)
'设置邮件的创建日期,主题以及html文本
message.[Date] = DateTime.Now
message.Subject = "年终晚会出席确认"
Dim htmlString As String = "
<html>
<body>
<p>您好!</p>
<p>非常感谢您邀请我参加2月2日(周五)晚8点在贵公司总部举办的年终晚会。我很乐意参加,期待在晚会上见到您和您的同事们。</p>
<p>预祝公司年会圆满成功。<br>Nina <br>2018年1月12日</p>
</body>
</html>"
message.BodyHtml = htmlString
'实例化一个SmtpClient对象,设置邮件服务器、端口、发件人邮箱及密码
Dim client As New SmtpClient()
client.Host = "smtp.outlook.com"
client.Port = 587
client.Username = addressFrom.Address
client.Password = "password"
client.ConnectionProtocols = ConnectionProtocols.Ssl
'发送邮件
client.SendOne(message)
结果展示: