MailBee.NET Objects发送电子邮件(SMTP)教程五:发送带有嵌入图片的网页和HTML邮件...

MailBee.NET Objects介绍和试用点击查看>>>

 

为了发送带有嵌入对象的电子邮件,开发人员应该使用SMTP object。首先,开发人员应该按照【MailBee.NET Objects发送电子邮件(SMTP)教程一】中所述指定SMTP对象的所有必要属性。

如果HTML格式的邮件没有任何嵌入对象,开发人员可以使用SMTP.BodyHtmlText属性或SMTP.Message.LoadBodyText方法来设置邮件正文。

SMTP.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"

 

也可以使用SMTP.Message.LoadBodyText方法。在这种情况下,开发人员可以从指定的URI中加载消息,如下所示:

C#:
oMailer.Message.LoadBodyText(@"http://www.domain.com/index.htm", MessageBodyType.Html);  
VB.NET:
oMailer.Message.LoadBodyText("http://www.domain.com/index.htm", MessageBodyType.Html)

 

或者

C#:
oMailer.Message.LoadBodyText(@"C:\Temp\saved_web_page.htm", MessageBodyType.Html);     
VB.NET:
oMailer.Message.LoadBodyText("C:\Temp\saved_web_page.htm", MessageBodyType.Html)


否则,开发人员应该调用SMTP.Message.LoadBodyText方法来编写一个包含嵌入对象(图像,音频或视频)的消息。以下示例将邮件正文加载为默认编码以及指定URI中的所有相关文件:

C#:
oMailer.Message.LoadBodyText(@"http://www.domain.com/index.htm ", 
                MessageBodyType.Html,
                Encoding.Default, ImportBodyOptions.ImportRelatedFiles|
                ImportBodyOptions.ImportRelatedFilesFromUris);       
VB.NET:
oMailer.Message.LoadBodyText("http://www.domain.com/index.htm ", _
                MessageBodyType.Html, _
                Encoding.Default, ImportBodyOptions.ImportRelatedFiles Or _
                ImportBodyOptions.ImportRelatedFilesFromUris)

 

请注意:在使用SMTP.Message.LoadBodyText方法时,所有相关文件将被添加到邮件中。

 

代码示例:

该示例从拥有嵌入对象的现有网页中创建新邮件并发送。在使用MailBee.NET Objects之前,请确保它已解锁。

C#:

using System;
using System.Text;
using MailBee;
using MailBee.SmtpMail;
using MailBee.Mime;

namespace EmailApp
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            Smtp oMailer = new Smtp();

            oMailer.To.AddFromString("Bill Smith ");

            oMailer.From.AsString = "John Doe (Company Info)";

            oMailer.Subject = "Test web page";

            oMailer.Message.LoadBodyText(@"http://www.domain.com/index.htm", MessageBodyType.Html, Encoding.Default, 

ImportBodyOptions.ImportRelatedFiles | ImportBodyOptions.ImportRelatedFilesFromUris);

            try
            {
                oMailer.Send();
                Console.WriteLine("The message has been successfully sent.");
            }
            catch (MailBeeSmtpMessageSizeOutOfRangeException e)
            {
                Console.WriteLine("The message is too large (more than " + e.MaxAllowedMessageSize + " bytes).");
            }
        }
    }
}

VB.NET:

Imports System
Imports System.Text
Imports MailBee
Imports MailBee.SmtpMail
Imports MailBee.Mime
 
Namespace EmailApp
    Class Class1
        _ 
        Shared  Sub Main(ByVal args() As String)
            Dim oMailer As Smtp =  New Smtp() 
 
            oMailer.To.AddFromString("Bill Smith ")
 
            oMailer.From.AsString = "John Doe (Company Info)"
 
            oMailer.Subject = "Test web page"
 
            oMailer.Message.LoadBodyText("http://www.domain.com/index.htm", MessageBodyType.Html, Encoding.Default, 

ImportBodyOptions.ImportRelatedFiles | ImportBodyOptions.ImportRelatedFilesFromUris)
 
            Try
                oMailer.Send()
                Console.WriteLine("The message has been successfully sent.")
            Catch e As MailBeeSmtpMessageSizeOutOfRangeException
                Console.WriteLine("The message is too large (more than " + e.MaxAllowedMessageSize + " bytes).")
            End Try
        End Sub
    End Class
End Namespace

 

以上就是本次教程的全部内容,接下来会有更多相关教程,敬请关注!您也可以在评论者留下你的经验和建议。

转载于:https://my.oschina.net/u/3279502/blog/900499

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值