EmailMessage
From Xojo Documentation
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site! |
Class (inherits from Object)
Used to hold the contents of an email message and its enclosures, if any.
Properties | |||||||||||
|
Methods | |||
|
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.Text
Socket1.Port = 25
If AuthenticateBox.Value = 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 = Encodings.UTF8.Chr(13)
Var LF As String = Encodings.UTF8.Chr(10)
Var s As String
s = ToAddressField.Text.ReplaceAll(",", CR)
s = s.ReplaceAll(CR + LF, CR)
Var recipients() As String
recipients = s.Split(CR)
For Each recipient As String In recipients
mail.AddRecipient(recipient.Trim)
Next
// add cc recipients
s = CCAddress.Text.ReplaceAll(",", CR)
s = s.ReplaceAll(CR + LF, CR)
recipients.RemoveAllRows
recipients = s.Split(CR)
For Each recipient As String In recipients
mail.AddCCRecipient(recipient.Trim)
Next
Var file As EmailAttachment
// add attachments
If fileField.Text <> "" 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
Socket1.Address = ServerField.Text
Socket1.Port = 25
If AuthenticateBox.Value = 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 = Encodings.UTF8.Chr(13)
Var LF As String = Encodings.UTF8.Chr(10)
Var s As String
s = ToAddressField.Text.ReplaceAll(",", CR)
s = s.ReplaceAll(CR + LF, CR)
Var recipients() As String
recipients = s.Split(CR)
For Each recipient As String In recipients
mail.AddRecipient(recipient.Trim)
Next
// add cc recipients
s = CCAddress.Text.ReplaceAll(",", CR)
s = s.ReplaceAll(CR + LF, CR)
recipients.RemoveAllRows
recipients = s.Split(CR)
For Each recipient As String In recipients
mail.AddCCRecipient(recipient.Trim)
Next
Var file As EmailAttachment
// add attachments
If fileField.Text <> "" 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 POP3Socket 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
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 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.Text = s.ReplaceAll(Chr(13) + Chr(10), Chr(13))
// display the message
s = Email.BodyHTML
If s = "" Then
s = Email.BodyPlainText
End If
BodyField.Text = 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.