MailBee.NET Objects是一款为创建、发送、接收以及处理电子邮件而设计的健壮、功能丰富的.NET控件。几行代码便可为应用程序添加E-Mail支持,简单高效。具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加到他们的应用程序中。
本文主要介绍了SMTP服务器退回邮件的代码示例。
所有退回的电子邮件都应该到达一个邮箱地址。这里将其命名为我们的退件通知栏 - bounce@domain.com。显然,你需要确保退回的邮件能有它们的方式到达退件通知栏。
让我们看看这些邮件是如何由SMTP服务器发送的。当一封邮件提交给SMTP服务器时,它将被标记返回路径。返回路径是服务器用于与邮件的原始发送方通信的路径,因此返回路径通常是发件人的电子邮件地址(“From”地址)。
返回路径记录在邮件中。“From”用于显示发件人地址。返回路径和字段“From”中的地址不必一样。因此,您可以发送一个使用字段“From”的电子邮件显示发件人(jdoe@domain.com),但返回路径将是bounce box地址(bounce@domain.com)。有了这个技巧,所有的反弹消息都将返回到一个特殊的邮箱。
那么如何使用MailBee Objects呢?smtp.Send(senderEmail,recipient)方法向指定的收件人电子邮件地址发送电子邮件。第一个参数是发件人的电子邮件地址。如果是空引用,则电子邮件地址取自“From”属性。第二个参数包含收件人电子邮件地址的分隔列表。当第二个参数为空时,收件人列表可以由To、Cc和Bcc列表组合。正如你所看到的,第一个参数是我们组织一个退件栏所需要的。如果将退件栏地址设置为第一个参数,那么所有退回邮件将被发送到特殊地址。
注意:某些SMTP服务器不允许从“From”标头中指定的地址发送电子邮件。
代码示例:
此示例使用SMTP协议发送电子邮件。为了确保所有收件人都收到该邮件,TestSend将被调用。带有传送错误的邮件将来自指定的电子邮件地址。
C#
// Create SMTP object
Smtp mailer = new Smtp();
// Set the message fields.
mailer.From.AsString = "jdoe@domain.com";
mailer.To.AsString = "bill@domain2.com";
mailer.Subject = "Hi";
mailer.BodyPlainText = "This is test message";
// Starts logging SMTP activities into a file.
mailer.Log.Enabled = true;
mailer.Log.Filename = @"C:\log.txt";
mailer.Log.Clear();
// Specify the server to use. If your server does not require authentication,
// just omit both last parameters.
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret");
// Attempt to connect.
mailer.Connect();
// Display the host name of the server the connection was established with.
Console.WriteLine("Connected to " +mailer.SmtpServers[mailer.GetCurrentSmtpServerIndex()].Name);
// Make sure all the recipients are ok.
if (mailer.TestSend(SendFailureThreshold.AllRecipientsFailed) != TestSendResult.OK)
{
Console.WriteLine("No recipients can receive the message.");
}// Show refused recipients if any
else if (mailer.GetRefusedRecipients().Count > 0)
{
Console.WriteLine("The following recipients failed: " + mailer.GetRefusedRecipients().ToString());
}
else
{
Console.WriteLine("All recipients are ok. Will send the message now.");
// Send e-mail. If it cannot be delivered, bounce will
// arrive to bounce@domain3.com, not to joe@domain1.com
mailer.Send("bounce@domain.com", (string)null);
Console.WriteLine("Sent to: " + mailer.GetAcceptedRecipients().ToString());
}
// Disconnect from the server
mailer.Disconnect();
VB.NET
' Create SMTP object
Dim mailer As New Smtp
' Set the message fields.
mailer.From.AsString = "jdoe@domain.com"
mailer.To.AsString = "bill@domain2.com"
mailer.Subject = "Hi"
mailer.BodyPlainText = "This is test message"
' Starts logging SMTP activities into a file.
mailer.Log.Enabled = True
mailer.Log.Filename = "C:\log.txt"
mailer.Log.Clear()
' Specify the server to use. If your server does not require authentication,
' just remove last 2 parameters.
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret")
' Attempt to connect.
mailer.Connect()
' Display the host name of the server the connection was established with.
Console.WriteLine("Connected to " + mailer.SmtpServers(mailer.GetCurrentSmtpServerIndex()).Name)
' Make sure all the recipients are ok.
If mailer.TestSend(SendFailureThreshold.AllRecipientsFailed) <> TestSendResult.OK Then
Console.WriteLine("No recipients can receive the message.")
Else
' Show refused recipients if any
If mailer.GetRefusedRecipients().Count > 0 Then
Console.WriteLine("The following recipients failed: " & mailer.GetRefusedRecipients().ToString())
Else
Console.WriteLine("All recipients are ok. Will send the message now.")
' Send e-mail. If it cannot be delivered, bounce will
' arrive to bounce@domain3.com, not to joe@domain1.com
mailer.Send("bounce@domain.com", CType(Nothing, String))
Console.WriteLine("Sent to: " + mailer.GetAcceptedRecipients().ToString())
End If
End If
' Disconnect from the server
mailer.Disconnect()
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。