Class

EmailMessage


Description

Used to hold the contents of an email message and its enclosures, if any.

Methods

Name

Parameters

Returns

Shared

AddBCCRecipient

Recipient As String

AddCCRecipient

Recipient As String

AddRecipient

Recipient As String

BCCAddress

String

CCAddress

String

FromAddress

String

Assigns nameEmail As String

RawSource

String

Source

String

Subject

String

Assigns subject As String

ToAddress

String

Property descriptions


EmailMessage.Attachments

Attachments As EmailAttachment

The array of EmailAttachments that are attached to this email message.

Var mail As EmailMessage
Var file As EmailAttachment
.
.
' add attachments
file = New EmailAttachment
file.ContentEncoding = "UTF8"
file.LoadFromFile(New FolderItem("MyAttachment.txt"))
mail.Attachments.Add(file)

EmailMessage.BodyEnriched

BodyEnriched As String

The body text of the email message in enriched format (RTF).

Some clients such as Mail use this format when sending styled emails. You should also include BodyPlainText for clients that cannot display BodyEnriched content.


EmailMessage.BodyHTML

BodyHTML As String

The body text of the email as HTML.

You should also include BodyPlainText for clients that cannot display HTML content.

' populate the email message
mail = New EmailMessage
mail.FromAddress = "mary@company.com"
mail.Subject = "Status Report"
mail.BodyHTML = HtmlArea.Text ' text area

EmailMessage.BodyPlainText

BodyPlainText As String

The body text of the email as plain text.

Clients that do not support HTML or enriched format will display the message as plain text using the contents of this property. You should always include plain text when sending the email message.

' populate the email message
mail = New EmailMessage
mail.FromAddress = "mary@company.com"
mail.Subject = "Status Report"
mail.BodyPlainText = MessageField.Text

EmailMessage.Headers

Headers As EmailHeaders

The headers for this email message.

Var mail As New EmailMessage
mail.Headers.AddHeader("X-Mailer","Example SMTP Demo")

Method descriptions


EmailMessage.AddBCCRecipient

AddBCCRecipient(Recipient As String)

Adds a BCC recipient to the email.

Var mail As New EmailMessage
mail.AddBCCRecipient("boss@company.com")
mail.AddBCCRecipient("bigboss@company.com")

EmailMessage.AddCCRecipient

AddCCRecipient(Recipient As String)

Adds a CC recipient to the email.

Var mail As New EmailMessage
mail.AddCCRecipient("mary@company.com")
mail.AddCCRecipient("mark@company.com")

EmailMessage.AddRecipient

AddRecipient(Recipient As String)

Adds a recipient to the email.

Var mail As New EmailMessage
mail.AddRecipient("mary@company.com")
mail.AddRecipient("mark@company.com")

EmailMessage.BCCAddress

BCCAddress As String

Returns a comma-separated list of Strings. Gets the BCC addresses for this email.

To add BCC addresses use the AddBCCRecipient method.

Var bccAddresses = mail.BCCAddress

EmailMessage.CCAddress

CCAddress As String

Returns a comma-separated list of Strings. Gets the CC addresses for this email.

To add CCAddresses, use the AddCCRecipient method.

Var ccAddresses = mail.CCAddress

EmailMessage.FromAddress

FromAddress As String

Returns the email address from which the email was or will be sent.

FromAddress(Assigns nameEmail As String)

Sets the email address from which the email was or will be sent.

mail.FromAddress = "mary@company.com"

EmailMessage.RawSource

RawSource As String

A String containing the raw source of the email message.


EmailMessage.Source

Source As String

A String containing the source of the email message including headers.

Var mail As New EmailMessage
mail.Source(MessageArea.Text)

EmailMessage.Subject

Subject As String

Returns the subject of the email message.

Subject(Assigns subject As String)

Sets the subject of the email message.

Var mail As New EmailMessage
mail.Subject = "TPS Report"

EmailMessage.ToAddress

ToAddress As String

Gets the addresses for this email and returns a comma-separated list of Strings.

' add recipients
Var s As String
s = ToAddressField.Text.ReplaceAll(",", EndOfLine.CR)
s = s.ReplaceAll(EndOfLineCRLF, EndOfLine.CR)
Var recipients() As String
recipients = s.ToArray(EndOfLine.CR)
For Each recipient As String In recipients
  mail.AddRecipient(recipient.Trim)
Next

Sample code

The following example sets the values of the From Address, Subject, Body, and Headers from the values of the Text properties of TextFields in a window.

' set up the socket--Socket1 is an SMTPSocket
Socket1.Address = ServerField.Text
Socket1.Port = 25
If AuthenticateBox.Text = True Then
  Socket1.UserName = UserNameField.Text
  Socket1.Password = PasswordField.Text
Else
  Socket1.UserName = ""
End If

Var mail As New EmailMessage
mail.FromAddress = FromAddressField.Text
mail.Subject = SubjectField.Text
mail.BodyPlainText = BodyField.Text
mail.BodyHTML = HtmlField.Text
mail.Headers.AddHeader("X-Mailer","Example SMTP Demo")

' add recipients
Var CR As String = EndOfLine.CR
Var LF As String = EndOfLine.LF
Var s As String
s = ToAddressField.Text.ReplaceAll(",", CR)
s = s.ReplaceAll(EndOfLine.CRLF, CR)
Var recipients() As String
recipients = s.ToArray(CR)
For Each recipient As String In recipients
  mail.AddRecipient(recipient.Trim)
Next

' add cc recipients
s = CCAddress.Text.ReplaceAll(",", CR)
s = s.ReplaceAll(EndOfLine.CRLF, CR)
recipients.RemoveAllRows
recipients = s.ToArray(CR)
For Each recipient As String In recipients
  mail.AddCCRecipient(recipient.Trim)
Next

Var file As EmailAttachment

' add attachments
If Not fileField.Text.IsEmpty Then
  file = New EmailAttachment
  file.LoadFromFile(New FolderItem(fileField.Text, FolderItem.PathModes.Native))
  mail.Attachments.Add(file)
End If

' send the email
Socket1.Messages.Add(mail)
Socket1.SendMail

The Pressed event of a Button connects to a POP3 server and gets the email for the specified account. It reads the POP3Server, username, and password from TextFields on the form. Socket1 is a POP3SecureSocket object.

If Me.Caption = "Connect" Then
  Socket1.Address = ServerField.Text
  Socket1.Port = 110

  Socket1.UserName = UserNameField.Text
  Socket1.Password = PasswordField.Text

  Socket1.Connect
  Me.Caption = "Disconnect"
Else
  Socket1.DisconnectFromServer
  Me.Caption = "Connect"
End If

The MessageReceived event of the POP3SecureSocket places the text of the message in a TextField.

Var s As String

' display the message
s = Email.BodyHTML
If s.IsEmpty Then
  s = Email.BodyPlainText
End If

BodyField.Text = s.ReplaceAll(EndOfLine.CRLF, EndOfLine.CR)

Compatibility

All project types on all supported operating systems.