在单行代码中快速发送电子邮件的最简单的方法是使用SMTP对象的QuickSend方法。在这种情况下,开发人员甚至不需要创建一个新的SMTP对象实例。只需要指定其基本属性(如From、To、Subject、Body等):C#:
MailBee.SmtpMail.Smtp.QuickSend("from@me.com", "to@you.com", "Subject", "Message Body");VB.NET:
MailBee.SmtpMail.Smtp.QuickSend("from@me.com", "to@you.com", "Subject", "Message Body")
开发人员还可以使用QuickSend方法发送带有附件的电子邮件,如下所示:C#:
MailBee.SmtpMail.Smtp.QuickSend("From Me (Company Info)",
"To you ",
"Subject", "Plain text body",
"HTML-formatted body",
null, @"C:\My Documents\report.doc");VB.NET:
MailBee.SmtpMail.Smtp.QuickSend("From Me (Company Info)", _
"To you ", _
"Subject", "Plain text body", _
"«html»HTML-formatted body«/html»", _
Nothing, "C:\My Documents\report.doc")
开发人员可以直接使用一个SMTP对象的实例,该实例提供了广泛的方法和属性来调整消息发送的过程。C#应用程序中的SMTP对象的新实例可以创建如下:C#:
Smtp oMailer = new Smtp();VB.NET:
Dim oMailer As New Smtp()
如果SMTP服务器不需要任何身份验证,那么指定的主机名或相同的IP地址就可以连接到此SMTP服务器,如下所示:C#:
oMailer.SmtpServers.Add("smtp.domain.com");VB.NET:
oMailer.SmtpServers.Add("smtp.domain.com")
或者C#:
oMailer.SmtpServers.Add("127.0.0.1");VB.NET:
oMailer.SmtpServers.Add("127.0.0.1")
另一方面,如果SMTP服务器需要身份验证,开发人员应该确定该服务器上的帐户名称和相应的密码:C#:
oMailer.SmtpServers.Add("smtp.domain.com","login","password");VB.NET:
oMailer.SmtpServers.Add("smtp.domain.com","login","password")
或者C#:
oMailer.SmtpServers.Add("127.0.0.1","login","password");VB.NET:
oMailer.SmtpServers.Add("127.0.0.1","login","password")
然后,开发人员应确定邮件发件人的电子邮件地址,如下所示:C#:
oMailer.From.AsString = "Dan Brown (Company Info)";VB.NET:
oMailer.From.AsString = "Dan Brown (Company Info)"
或者C#:
oMailer.From.AsString = "Dan Brown ";VB.NET:
oMailer.From.AsString = "Dan Brown "
或者C#:
oMailer.From.AsString = "dan@domain.com";VB.NET:
oMailer.From.AsString = "dan@domain.com"
添加To、Cc、Bcc或Reply-To,开发人员应使用SMTP对象的相应属性,如下所示:C#:
oMailer.To.AsString = "Bill Smith (Remarks), Kathy@mail.com ";
oMailer.Cc.AsString = "Joe Black , Joseph ";
oMailer.Bcc.AsString = "t.jay@domain.com, s.connor@domain.com";
oMailer.ReplyTo.AsString = "john@domain.com, Barbara Jones ";VB.NET:
oMailer.To.AsString = "Bill Smith (Remarks), Kathy@mail.com "
oMailer.Cc.AsString = "Joe Black , Joseph "
oMailer.Bcc.AsString = "t.jay@domain.com, s.connor@domain.com"
oMailer.ReplyTo.AsString = "john@domain.com, Barbara Jones "
要指定邮件主题,开发人员应使用SMTP对象的Subject属性,如下所示:C#:
oMailer.Subject = "Test message";VB.NET:
oMailer.Subject = "Test message"
此外,邮件可能没有主题。
开发人员还应该确定信息的正文。如果是纯文本,开发人员应该使用BodyPlainText属性:C#:
oMailer.BodyPlainText = "This is a test e-mail message.";VB.NET:
oMailer.BodyPlainText = "This is a test e-mail message."
如果开发人员要使用HTML格式的正文,那么应该设置BodyHtmlText属性:C#:
oMailer.BodyHtmlText = @"
Test HTML message.
This is a test HTML mes-sage.
www.afterlogic.com";VB.NET:
oMailer.BodyHtmlText = "
Test HTML message.
" & vbCrLf & _
"" & vbCrLf & _
"
" & vbCrLf & _
"This is a test HTML mes-sage." & vbCrLf & _
"" & vbCrLf & _
"" & vbCrLf & _
"www.afterlogic.com"
开发人员也可以将这两种类型同时使用。在这种情况下,邮件客户端的设置会影响显示哪个正文。
要将任何文件附加到电子邮件中,开发人员应使用AddAttachment方法,该方法只需要开发人员指定此文件的完整路径。要添加多个附件,开发人员应重复调用AddAttachment方法,如下所示:C#:
oMailer.AddAttachment(@"C:\annual_reoprt.xls");
oMailer.AddAttachment(@"C:\deposits.doc");VB.NET:
oMailer.AddAttachment("C:\annual_reoprt.xls")
oMailer.AddAttachment("C:\deposits.doc")
要发送信息,开发人员应调用SMTP对象的Send方法。由于此方法可能会引发异常,开发人员可以按如下方式处理这些异常:C#:
try
{
oMailer.Send();
Console.WriteLine("The message has been successfully sent.");
}
catch (MailBeeSmtpRefusedRecipientException e)
{
Console.WriteLine("The following recipient was refused by SMTP server: " + e.RefusedRecipientEmail);
}VB.NET:
Try
oMailer.Send()
Console.WriteLine("The message has been successfully sent.")
Catch e As MailBeeSmtpRefusedRecipientException
Console.WriteLine("The following recipient was refused by SMTP server: " + e.RefusedRecipientEmail)
End Try
示例代码:
摘要:以下示例创建一个包含纯文本和HTML格式主体的邮件,并添加.xls文档附加。然后将邮件发送给两个指定的收件人。
在使用MailBee.NET Objects之前,请确保它已解锁。C#:
using System;
using MailBee;
using MailBee.SmtpMail;
namespace EmailApp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Smtp oMailer = new Smtp();
oMailer.From.AsString = "John Doe (Company Info)";
oMailer.To.AsString = "Bill Smith , Kathy Ritchie (Company Info)";
oMailer.Subject = "Test e-mail";
oMailer.BodyPlainText = "This is a test e-mail message.";
oMailer.BodyHtmlText = @"
Test HTML message.
This is a test HTML mes-sage.
www.afterlogic.com";
oMailer.AddAttachment(@"C:\annual_reoprt.xls");
oMailer.SmtpServers.Add("127.0.0.1", "login", "password");
oMailer.SmtpServers[0].AllowRefusedRecipients = false;
try
{
oMailer.Send();
Console.WriteLine("The message has been successfully sent.");
}
catch (MailBeeSmtpRefusedRecipientException e)
{
Console.WriteLine("The following recipient was refused by SMTP server: "+
e.RefusedRecipientEmail);
}
}
}
}VB.NET:
Imports System
Imports MailBee
Imports MailBee.SmtpMail
Namespace EmailApp
Class Class1
_
Shared Sub Main(ByVal args() As String)
Dim oMailer As New Smtp()
oMailer.From.AsString = "John Doe (Company Info)"
oMailer.To.AsString = "Bill Smith , Kathy Ritchie (Company Info)"
oMailer.Subject = "Test e-mail"
oMailer.BodyPlainText = "This is a test e-mail message."
oMailer.BodyHtmlText = "
Test HTML message.
" & vbCrLf & _
"" & vbCrLf & _
"
" & vbCrLf & _
"This is a test HTML mes-sage." & vbCrLf & _
"" & vbCrLf & _
"" & vbCrLf & _
"www.afterlogic.com"
oMailer.AddAttachment("C:\annual_reoprt.xls")
oMailer.SmtpServers.Add("127.0.0.1", "login", "password")
oMailer.SmtpServers(0).AllowRefusedRecipients = False
Try
oMailer.Send()
Console.WriteLine("The message has been successfully sent.")
Catch e As MailBeeSmtpRefusedRecipientException
Console.WriteLine("The following recipient was refused by SMTP server: "+
e.RefusedRecipientEmail)
End Try
End Sub
End Class
End Namespace