MailBee. NET Objects Tutorials

Sending a web page and HTML message with embedded pictures

In order to send an e-mail message with embedded objects, the developer should use the SMTP object. First of all, the developer should specify all the necessary properties of the SMTP object as described here.

In case an HTML-formatted message without any embedded objects is composed, the developer can use either SMTP.BodyHtmlText property or SMTP.Message.LoadBodyText method to set the message body.

The use SMTP.BodyHtmlText property allows the developer to assign the body of a message directly as follows:

C#

oMailer.BodyHtmlText = @"<DIV>Test HTML message.</DIV><BR><BR>
        <FONT face=""Arial Black"" color=#0000ff size=5>
        <P align=left><STRONG><U>
        This is a test HTML mes-sage.
        </U></STRONG></P></FONT><p>
        <hr size=1>
        <a href=""http://www.afterlogic.com"">www.afterlogic.com</a>";

VB.NET

oMailer.BodyHtmlText = "<DIV>Test HTML message.</DIV><BR><BR>" & vbCrLf & _
        "<FONT face= ""Arial Black"" color = #0000ff size = 5>" & vbCrLf & _
        "<P align=left><STRONG><U>" & vbCrLf & _
        "This is a test HTML mes-sage." & vbCrLf & _
        "</U></STRONG></P></FONT><p>" & vbCrLf & _
        "<hr size= 1>" & vbCrLf & _
        "<a href= ""http://www.afterlogic.com"">www.afterlogic.com</a>"

Another way is to use the SMTP.Message.LoadBodyText method. In this case, the developer is able to load a message from the specified URI as follows:

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)

or

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)

Otherwise, the developer should call the SMTP.Message.LoadBodyText method to compose a message with embedded objects (images, sounds or movies). The following sample loads the message body in default encoding along with all related files from the specified 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)

Please note: when calling the SMTP.Message.LoadBodyText method, all related files will be attached to the message.

Should you experience any problems with the following sample, please see the corresponding topic of the Troubleshooting section.

Code Sample:

Summary: The following example creates a new message from the existing web page with the embedded objects and sends this message. Before using MailBee.NET Objects, please make sure it is unlocked (see "Sales, Licensing, and Support" and "Using MailBee.NET Objects in Your Projects" sections).

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 <b.smith@domain.com>");

            oMailer.From.AsString = "John Doe <j.doe@domain.com> (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
        <STAThread> _ 
        Shared  Sub Main(ByVal args() As String)
            Dim oMailer As Smtp =  New Smtp() 
 
            oMailer.To.AddFromString("Bill Smith <b.smith@domain.com>")
 
            oMailer.From.AsString = "John Doe <j.doe@domain.com> (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