Class
EmailAttachment
Description
Used to hold attachments to an email.
Properties
Name |
Type |
Read-Only |
Shared |
---|---|---|---|
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
File As FolderItem |
|||
File As FolderItem |
Property descriptions
EmailAttachment.ContentEncoding
ContentEncoding As String
Content encoding of the attachment.
The following example specifies the UTF8 encoding.
Var file As EmailAttachment
file = New EmailAttachment
file.ContentEncoding = "UTF8"
EmailAttachment.Data
Data As String
When receiving an attachment, the contents of the attachment.
file = New EmailAttachment
file.ContentEncoding = "UTF8"
file.Data = TextArea1.Text
EmailAttachment.MIMEType
MIMEType As 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.
file = New EmailAttachment
file.MIMEType = "image/jpeg"
EmailAttachment.Name
Name As String
The name of the attached file.
If Not fileField.Text.IsEmpty Then
file = New EmailAttachment
file.Name = "Attached file"
End If
Method descriptions
EmailAttachment.LoadFromFile
LoadFromFile(File As 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.
' 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
EmailAttachment.ParseAttachment
ParseAttachment(type As String, header As String, fileData as String)
Updates the EmailAttachment using the data passed. Specifically the type is the MIMEType, the header impacts the ContentEncoding and the fileData is assigned to the Data property.
EmailAttachment.SaveToFile
SaveToFile(File As FolderItem) As Boolean
Saves the attachment to File. Returns a Boolean, True if successful.
' 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 named fileField.
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:
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
All project types on all supported operating systems.
See also
Object parent class; EmailMessage, EmailHeaders, POP3SecureSocket, POP3SecureSocket, SMTPSecureSocket, SMTPSecureSocket, SocketCore, TCPSocket classes.