Class

# EmailMessage

<div class="rst-class">

forsearch

</div>

EMail

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

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

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                        | Type                                               | Read-Only | Shared |
|---------------------------------------------|----------------------------------------------------|-----------|--------|
| `Attachments<emailmessage.attachments>`     | `EmailAttachment</api/networking/emailattachment>` |           |        |
| `BodyEnriched<emailmessage.bodyenriched>`   | `String</api/data_types/string>`                   |           |        |
| `BodyHTML<emailmessage.bodyhtml>`           | `String</api/data_types/string>`                   |           |        |
| `BodyPlainText<emailmessage.bodyplaintext>` | `String</api/data_types/string>`                   |           |        |
| `Headers<emailmessage.headers>`             | `EmailHeaders</api/networking/emailheaders>`       |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                            | Parameters                                                                     | Returns                          | Shared |
|-------------------------------------------------|--------------------------------------------------------------------------------|----------------------------------|--------|
| `AddBCCRecipient<emailmessage.addbccrecipient>` | Recipient As `String</api/data_types/string>`                                  |                                  |        |
| `AddCCRecipient<emailmessage.addccrecipient>`   | Recipient As `String</api/data_types/string>`                                  |                                  |        |
| `AddRecipient<emailmessage.addrecipient>`       | Recipient As `String</api/data_types/string>`                                  |                                  |        |
| `BCCAddress<emailmessage.bccaddress>`           |                                                                                | `String</api/data_types/string>` |        |
| `CCAddress<emailmessage.ccaddress>`             |                                                                                | `String</api/data_types/string>` |        |
| `FromAddress<emailmessage.fromaddress>`         |                                                                                | `String</api/data_types/string>` |        |
|                                                 | `Assigns</api/language/assigns>` nameEmail As `String</api/data_types/string>` |                                  |        |
| `RawSource<emailmessage.rawsource>`             | `String</api/data_types/string>`                                               |                                  |        |
| `Source<emailmessage.source>`                   | `String</api/data_types/string>`                                               |                                  |        |
| `Subject<emailmessage.subject>`                 |                                                                                | `String</api/data_types/string>` |        |
|                                                 | `Assigns</api/language/assigns>` subject As `String</api/data_types/string>`   |                                  |        |
| `ToAddress<emailmessage.toaddress>`             |                                                                                | `String</api/data_types/string>` |        |

## Property descriptions

<div id="emailmessage.attachments">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.Attachments

**Attachments** As `EmailAttachment</api/networking/emailattachment>`

> The array of `EmailAttachments</api/networking/emailattachment>` that are attached to this email message.
>
> ``` xojo
> Var mail As EmailMessage
> Var file As EmailAttachment
> .
> .
> ' add attachments
> file = New EmailAttachment
> file.ContentEncoding = "UTF8"
> file.LoadFromFile(New FolderItem("MyAttachment.txt", FolderItem.PathModes.Native))
> mail.Attachments.Add(file)
> ```

<div id="emailmessage.bodyenriched">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.BodyEnriched

**BodyEnriched** As `String</api/data_types/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<emailmessage.bodyplaintext>` for clients that cannot display BodyEnriched content.

<div id="emailmessage.bodyhtml">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.BodyHTML

**BodyHTML** As `String</api/data_types/string>`

> The body text of the email as HTML.
>
> You should also include `BodyPlainText<emailmessage.bodyplaintext>` for clients that cannot display HTML content.
>
> ``` xojo
> ' populate the email message
> mail = New EmailMessage
> mail.FromAddress = "mary@company.com"
> mail.Subject = "Status Report"
> mail.BodyHTML = HtmlArea.Text ' text area
> ```

<div id="emailmessage.bodyplaintext">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.BodyPlainText

**BodyPlainText** As `String</api/data_types/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.
>
> ``` xojo
> ' populate the email message
> mail = New EmailMessage
> mail.FromAddress = "mary@company.com"
> mail.Subject = "Status Report"
> mail.BodyPlainText = MessageField.Text
> ```

<div id="emailmessage.headers">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.Headers

**Headers** As `EmailHeaders</api/networking/emailheaders>`

> The headers for this email message.
>
> ``` xojo
> Var mail As New EmailMessage
> mail.Headers.AddHeader("X-Mailer","Example SMTP Demo")
> ```

## Method descriptions

<div id="emailmessage.addbccrecipient">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.AddBCCRecipient

**AddBCCRecipient**(Recipient As `String</api/data_types/string>`)

> Adds a BCC recipient to the email.
>
> ``` xojo
> Var mail As New EmailMessage
> mail.AddBCCRecipient("boss@company.com")
> mail.AddBCCRecipient("bigboss@company.com")
> ```

<div id="emailmessage.addccrecipient">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.AddCCRecipient

**AddCCRecipient**(Recipient As `String</api/data_types/string>`)

> Adds a CC recipient to the email.
>
> ``` xojo
> Var mail As New EmailMessage
> mail.AddCCRecipient("mary@company.com")
> mail.AddCCRecipient("mark@company.com")
> ```

<div id="emailmessage.addrecipient">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.AddRecipient

**AddRecipient**(Recipient As `String</api/data_types/string>`)

> Adds a recipient to the email.
>
> ``` xojo
> Var mail As New EmailMessage
> mail.AddRecipient("mary@company.com")
> mail.AddRecipient("mark@company.com")
> ```

<div id="emailmessage.bccaddress">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.BCCAddress

**BCCAddress** As `String</api/data_types/string>`

> Returns a comma-separated list of `Strings</api/data_types/string>`. Gets the BCC addresses for this email.
>
> To add BCC addresses use the `AddBCCRecipient<emailmessage.addbccrecipient>` method.
>
> ``` xojo
> Var bccAddresses = mail.BCCAddress
> ```

<div id="emailmessage.ccaddress">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.CCAddress

**CCAddress** As `String</api/data_types/string>`

> Returns a comma-separated list of `Strings</api/data_types/string>`. Gets the CC addresses for this email.
>
> To add CCAddresses, use the `AddCCRecipient<emailmessage.addccrecipient>` method.
>
> ``` xojo
> Var ccAddresses = mail.CCAddress
> ```

<div id="emailmessage.fromaddress">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.FromAddress

**FromAddress** As `String</api/data_types/string>`

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

**FromAddress**(`Assigns</api/language/assigns>` nameEmail As `String</api/data_types/string>`)

> Sets the email address from which the email was or will be sent.
>
> ``` xojo
> mail.FromAddress = "mary@company.com"
> ```

<div id="emailmessage.rawsource">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.RawSource

**RawSource** As `String</api/data_types/string>`

> A `String</api/data_types/string>` containing the raw source of the email message.

<div id="emailmessage.source">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.Source

**Source** As `String</api/data_types/string>`

> A `String</api/data_types/string>` containing the source of the email message including headers.
>
> ``` xojo
> Var mail As New EmailMessage
> mail.Source(MessageArea.Text)
> ```

<div id="emailmessage.subject">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.Subject

**Subject** As `String</api/data_types/string>`

> Returns the subject of the email message.

**Subject**(`Assigns</api/language/assigns>` subject As `String</api/data_types/string>`)

> Sets the subject of the email message.
>
> ``` xojo
> Var mail As New EmailMessage
> mail.Subject = "TPS Report"
> ```

<div id="emailmessage.toaddress">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

EmailMessage.ToAddress

**ToAddress** As `String</api/data_types/string>`

> Gets the addresses for this email and returns a comma-separated list of `Strings</api/data_types/string>`.
>
> ``` xojo
> ' 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</api/user_interface/desktop/desktoptextfield>` in a window.

``` xojo
' 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</api/user_interface/desktop/desktopbutton>` connects to a POP3 server and gets the email for the specified account. It reads the POP3Server, username, and password from `TextFields</api/user_interface/desktop/desktoptextfield>` on the form. Socket1 is a `POP3SecureSocket</api/networking/pop3securesocket>` object.

``` xojo
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</api/networking/pop3securesocket>` places the text of the message in a `TextField</api/user_interface/desktop/desktoptextfield>`.

``` xojo
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

|                       |                       |
|-----------------------|-----------------------|
| **Project Types**     | Console, Desktop, Web |
| **Operating Systems** | All                   |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `EmailAttachment</api/networking/emailattachment>`, `EmailHeaders</api/networking/emailheaders>`, `URLConnection</api/networking/urlconnection>`, `URLConnection</api/networking/urlconnection>`, `POP3SecureSocket</api/networking/pop3securesocket>`, `POP3SecureSocket</api/networking/pop3securesocket>`, `SMTPSecureSocket</api/networking/smtpsecuresocket>`, `SMTPSecureSocket</api/networking/smtpsecuresocket>`, `SocketCore</api/networking/socketcore>`, `SSLSocket</api/networking/sslsocket>`, `TCPSocket</api/networking/tcpsocket>` classes.

</div>
