EmailMessage

From Xojo Documentation

Revision as of 17:29, 12 November 2019 by PLefebvre (talk | contribs) (Examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Class (inherits from Object)

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

Properties
Attachments BodyPlainText Source
BCCAddress fa-lock-32.png CCAddress fa-lock-32.png Subject
BodyEnriched FromAddress ToAddress
BodyHTML Headers


Methods
AddBCCRecipient AddCCRecipient AddRecipient

Examples

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.Value
Socket1.Port = 25
If AuthenticateBox.Value = True Then
Socket1.UserName = UserNameField.Value
Socket1.Password = PasswordField.Value
Else
Socket1.UserName = ""
End If

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

// add recipients
Var s As String
s = ToAddressField.Value.ReplaceAll(",", Chr(13))
s = s.ReplaceAll(Chr(13) + Chr(10), Chr(13))
Var recipients() As String
recipients = s.Split(Chr(13))
For Each recipient As String In recipients
mail.AddRecipient(Trim(recipient))
Next

// add cc recipients
s = CCAddress.Value.ReplaceAll(",", Chr(13))
s = s.ReplaceAll(Chr(13) + Chr(10), Chr(13))
recipients.RemoveAllRows
recipients = s.Split(Chr(13))
For Each recipient As String In recipients
mail.AddCCRecipient(Trim(recipient))
Next

Var file As EmailAttachment

// add attachments
If fileField.Text <> "" Then
file = New EmailAttachment
file.LoadFromFile(GetFolderItem(fileField.Value))
mail.Attachments.Add(file)
End If

// send the email
Socket1.Messages.Append(mail)
Socket1.SendMail

The Action event of a PushButton 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 POP3Socket object.

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

Socket1.UserName = UserNameField.Value
Socket1.Password = PasswordField.Value

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

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

Var s As String

// display the message
s = Email.BodyHTML
If s = "" Then
s = Email.BodyPlainText
End If

BodyField.Value = s.ReplaceAll(Chr(13) + Chr(10), Chr(13))

See also the examples for the POP3Socket class.

See Also

EmailAttachment, EmailHeaders, HTTPSecureSocket, HTTPSocket, POP3SecureSocket, POP3Socket, SMTPSecureSocket, SMTPSocket, SocketCore, SSLSocket, TCPSocket classes.