Class

# EmailAttachment

<div class="rst-class">

forsearch

</div>

EMail

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

## Description

Used to hold attachments to an email.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                               | Type                             | Read-Only | Shared |
|----------------------------------------------------|----------------------------------|-----------|--------|
| `ContentEncoding<emailattachment.contentencoding>` | `String</api/data_types/string>` |           |        |
| `Data<emailattachment.data>`                       | `String</api/data_types/string>` |           |        |
| `MIMEType<emailattachment.mimetype>`               | `String</api/data_types/string>` |           |        |
| `Name<emailattachment.name>`                       | `String</api/data_types/string>` |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                               | Parameters                                                                                                                         | Returns                            | Shared |
|----------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------|------------------------------------|--------|
| `LoadFromFile<emailattachment.loadfromfile>`       | File As `FolderItem</api/files/folderitem>`                                                                                        |                                    |        |
| `ParseAttachment<emailattachment.parseattachment>` | type As `String</api/data_types/string>`, header As `String</api/data_types/string>`, fileData as `String</api/data_types/string>` |                                    |        |
| `SaveToFile<emailattachment.savetofile>`           | File As `FolderItem</api/files/folderitem>`                                                                                        | `Boolean</api/data_types/boolean>` |        |

## Property descriptions

<div id="emailattachment.contentencoding">

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

</div>

<div class="rst-class">

forsearch

</div>

EmailAttachment.ContentEncoding

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

> Content encoding of the attachment.
>
> The following example specifies the UTF8 encoding.
>
> ``` xojo
> Var file As EmailAttachment
> file = New EmailAttachment
> file.ContentEncoding = "UTF8"
> ```

<div id="emailattachment.data">

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

</div>

<div class="rst-class">

forsearch

</div>

EmailAttachment.Data

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

> When receiving an attachment, the contents of the attachment.
>
> ``` xojo
> file = New EmailAttachment
> file.ContentEncoding = "UTF8"
> file.Data = TextArea1.Text
> ```

<div id="emailattachment.mimetype">

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

</div>

<div class="rst-class">

forsearch

</div>

EmailAttachment.MIMEType

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

> The MIME type of the attachment. Here is a reference of MIME types: <http://www.ltsw.se/knbase/internet/mime.htp>.
>
> You should set the MIME type for most attachments to ensure that mail clients and services do not flag things as spam.
>
> ``` xojo
> file = New EmailAttachment
> file.MIMEType = "image/jpeg"
> ```

<div id="emailattachment.name">

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

</div>

<div class="rst-class">

forsearch

</div>

EmailAttachment.Name

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

> The name of the attached file.
>
> ``` xojo
> If Not fileField.Text.IsEmpty Then
>   file = New EmailAttachment
>   file.Name = "Attached file"
> End If
> ```

## Method descriptions

<div id="emailattachment.loadfromfile">

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

</div>

<div class="rst-class">

forsearch

</div>

EmailAttachment.LoadFromFile

**LoadFromFile**(File As `FolderItem</api/files/folderitem>`)

> Attaches the passed *File*.
>
> Set the MIMEType for the attachment after loading the file.
>
> This example specifies an attachment file by taking the contents of a `TextField</api/user_interface/desktop/desktoptextfield>`.
>
> ``` xojo
> ' add image attachments
> If Not fileField.Text.IsEmpty Then
>   file = New EmailAttachment
>   file.LoadFromFile(New FolderItem(fileField.Text, FolderItem.PathModes.Native))
>   file.MIMEType = "image/jpeg"
>   mail.Attachments.Add(file)
> End If
> ```

<div id="emailattachment.parseattachment">

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

</div>

<div class="rst-class">

forsearch

</div>

EmailAttachment.ParseAttachment

**ParseAttachment**(type As `String</api/data_types/string>`, header As `String</api/data_types/string>`, fileData as `String</api/data_types/string>`)

> Updates the <span class="title-ref">EmailAttachment</span> using the data passed. Specifically the *type* is the `MIMEType<emailattachment.mimetype>`, the *header* impacts the `ContentEncoding<emailattachment.contentencoding>` and the *fileData* is assigned to the `Data<emailattachment.data>` property.

<div id="emailattachment.savetofile">

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

</div>

<div class="rst-class">

forsearch

</div>

EmailAttachment.SaveToFile

**SaveToFile**(File As `FolderItem</api/files/folderitem>`) As `Boolean</api/data_types/boolean>`

> Saves the attachment to *File*. Returns a `Boolean</api/data_types/boolean>`, `True</api/language/true>` if successful.
>
> ``` xojo
> ' attachment is an EmailAttachment
> Var saveFile As FolderItem = SpecialFolder.Documents.Child(file.Name)
>
> If attachment.SaveToFile(saveFile) Then
>   MessageBox("The attachment was saved!")
> End If
> ```

## Sample code

The following example takes a path to a file and adds it as an email attachment. The path has been entered into the `TextField</api/user_interface/desktop/desktoptextfield>` named fileField.

``` xojo
Var mail As New EmailMessage
If Not fileField.Text.IsEmpty Then
  Var file As New EmailAttachment
  file.LoadFromFile(New FolderItem(fileField.Text, FolderItem.PathModes.Native))
  mail.Attachments.Add(file)
End If
```

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

This code accounts for some different file types:

``` xojo
Var mail As New EmailMessage
Var file As FolderItem = FolderItem.ShowOpenFileDialog("")
If file <> Nil And file.Exists Then
    Var attachment As New EmailAttachment
    attachment.LoadFromFile(file)
    attachment.Name = file.Name
    If file.Name.Right(4) = ".pdf" Then
      attachment.MIMEType = "application/pdf"
    ElseIf file.Name.Right(4) = ".png" Then
      attachment.MIMEType = "image/png"
    ElseIf file.Name.Right(4) = ".jpg" Or attachment.Name.Right(5) = ".jpeg" Then
      attachment.MIMEType = "image/jpeg"
      ' Else ' and so on with other extensions
    End If
    mail.Attachments.Add(attachment)
  End If
```

## Compatibility

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

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `EmailMessage</api/networking/emailmessage>`, `EmailHeaders</api/networking/emailheaders>`, `POP3SecureSocket</api/networking/pop3securesocket>`, `POP3SecureSocket</api/networking/pop3securesocket>`, `SMTPSecureSocket</api/networking/smtpsecuresocket>`, `SMTPSecureSocket</api/networking/smtpsecuresocket>`, `SocketCore</api/networking/socketcore>`, `TCPSocket</api/networking/tcpsocket>` classes.

</div>
