-
Create an instance of %Net.MailMessageOpens in a new tab.
Tip:
You can specify a character set as the argument to %New(); if you do so, that sets the Charset property for the message. For information on how this affects the message, see Automatic Encoding and Character Translation.
-
Set the To, From, and Subject properties of your instance.
-
To — The list of email addresses to which this message will be sent. This property is a standard InterSystems IRIS list class; to work with it, you use the standard list methods: Insert(), GetAt(), RemoveAt(), Count(), and Clear().
-
From — The email address this message is sent from.
-
Subject — The subject of the message, if this is required by the SMTP server you are using.
-
Optionally set Date, Cc, Bcc, and other properties. For details, see Specifying Basic Email Headers.
-
If the message is not plain text, set the following properties to indicate the kind of message you are creating:
-
If this is an HTML message, set the IsHTML property to 1.
-
If this is a binary message, set the IsBinary property to 1.
-
To specify the character set of the message and its headers, set the Charset property as needed. (For details on how this affects the message, see Automatic Encoding and Character Translation.)
Important:
It is important to specify the character set before you add the contents of the message.
-
Add the contents of the message:
-
For plain text or HTML, use the TextData property, which is an instance of %FileCharacterStreamOpens in a new tab. You do not need to specify the TranslateTable property of this stream; that occurred automatically when you specified the character set of the mail message.
-
For binary data, use the BinaryData property, which is an instance of %FileBinaryStreamOpens in a new tab.
Note that %FileCharacterStreamOpens in a new tab and %FileBinaryStreamOpens in a new tab are deprecated but are still supported for use in this way. It is not recommended to substitute different stream classes for this use case.
Tip:
When you specify the Filename property of the stream, be sure to use a directory to which the users will have write access.
To work with these properties, use the standard stream methods: Write(), WriteLine(), Read(), ReadLine(), Rewind(), MoveToEnd(), and Clear(). You can also use the Size property of the stream, which gives you the size of the message contents.
Note:
You should be aware of the requirements of the SMTP server that you are using. For example, some SMTP servers require that you include a Subject header. Similarly, some SMTP servers do not permit arbitrary From headers.
Similarly, some SMTP servers recognize the Priority header and others recognize X-Priority instead.
Example 1: CreateTextMessage()
The following method creates a simple message and specifies the addresses for it:
ClassMethod CreateTextMessage() As %Net.MailMessage
{
Set msg = ##class(%Net.MailMessage).%New()
set msg.From = "test@test.com"
Do msg.To.Insert("xxx@xxx.com")
Do msg.Cc.Insert("yyy@yyy.com")
Do msg.Bcc.Insert("zzz@zzz.com")
Set msg.Subject="subject line here"
Set msg.IsBinary=0
Set msg.IsHTML=0
Do msg.TextData.Write("This is the message.")
Quit msg
}