Cache has a set of classes that allow users to write, send, and receive
email - %Net.SMTP, %Net.MailMessage, and %Net.POP3. These classes can
be called from within MV Basic code, so MV routines can also include
this functionality. For those familiar with Cache's integration of MV
Basic and objects, everything below should be straightforward. If you
happen to need a refresher, please reference a couple of our older tips
here:
http://groups.google.com/group/InterSystems-MV/browse_thread/thread/d30dfed5a531b3ee
http://groups.google.com/group/InterSystems-MV/browse_thread/thread/f5d158cc3ead0c22
To send an email, we will need to construct our mail message object and
reference the %Net.SMTP class:
SMTPSRV = "%Net.SMTP"->%New()
SMTPSRV->smtpserver = "smtp.example.com"
SMTPSRV->port = 25
SMTPAUTH = "%Net.Authenticator"->%New()
SMTPAUTH->UserName = "user...@example.com"
SMTPAUTH->Password = "password"
SMTPSRV->authenticator = SMTPAUTH
MSG = "%Net.MailMessage"->%New()
MSG->From = "sen...@example.com"
MSG->To->Insert("rece...@example.com")
MSG->Subject = "Send ham, not spam."
STAT = MSG->TextData->Write("Hello world!")
* add an attachment
STREAM = "%Library.FileCharacterStream"->%New()
STREAM->Filename = "C:\AnAttachment.txt"
MSG->AttachStream(STREAM, "AnAttachment.txt", 0)
STAT = SMTPSRV->Send(MSG)
Similarly, to receive we will reference the %Net.POP3 class and use it
to pull down data which will be stored in mail message objects. Below,
I connect to our POP3 server, download all messages (without deleting
them from the server), and then dumping everything to screen:
POP3 = "%Net.POP3"->%New()
POP3->AttachDir = "C:\Temp\Maildir\"
STAT = POP3->Connect("pop3.example.com", "username", "password")
POP3->GetMailboxStatus(NUMMSG, NUMBYTES)
FOR I = 1 TO NUMMSG
PRINT "RETRIEVING MESSAGE ":I
STAT = POP3->Fetch(I, MSG, 0)
PRINT MSG->From
PRINT MSG->Date
PRINT MSG->Subject
STREAM = MSG->TextData
STREAM->Rewind()
LEN = 32763
LOOP WHILE STREAM->AtEnd = 0
PRINT STREAM->Read(LEN)
REPEAT
NEXT I
Another feature worth noting is that Cache allows for SMTP and POP3
traffic to be encrypted with very little additional coding involved.
Cache has a central location where SSL configuration information is
stored. While the specifics of working with SSL configurations are
outside the scope of this tip, what is important for us today is that
any SSL configuration defined must be given a name, and that name can be
supplied to the instanced object of %Net.SMTP or %Net.POP3 to make Cache
use that configuration information behind the scenes whenever it calls
out over the network. You would supply this name by defining the
SSLConfiguration property, like so:
SMTPSRV->SSLConfiguration = "ConfigNameGoesHere"
POP3->SSLConfiguration = "ConfigNameGoesHere"
For more information about SSL configurations, please see our documentation:
http://docs.intersystems.com/cache20121/csp/docbook/DocBook.UI.Page.cls?KEY=GCAS_ssltls#GCAS_ssltls_createedit
Also worth noting briefly is that Cache's classes for handling email do
allow for the attachment and retrieval of files, and the sending of
multi-part messages. These are done with additional method calls in the
same classes, some of which (for sending emails) are included in the
example above. For those who are simply looking to get started, the
above will help. If you want to know more about what is included in our
%Net.SMTP and %Net.POP3 classes, please see our class reference API:
http://docs.intersystems.com/cache20121/csp/documatic/%25CSP.Documatic.cls
One thing I learned at Spectrum is that there are folks working with
Cache' and MV who were unaware of this forum. I would like to see more
people responding here as I do not like to see a tip with no
responses. They are soooo appreciated!
We have already sent e-mail from our software but we have not attached
anything. We also have not yet addressed any requirements to read
e-mail. If we do, it is great to get a headstart from a tip like this
one.
We might want to do texting too. I have not yet looked into that, so a
tip on sending and receiving texts might be nice too. Thanks! --dawn
> --
> You received this message because you are subscribed to the Google Groups
> "InterSystems: MV Community" group.
> To post to this group, send email to Cac...@googlegroups.com
> To unsubscribe from this group, send email to
> CacheMV-u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/CacheMV?hl=en
--
Dawn M. Wolthuis
Take and give some delight today