PDA

View Full Version : Using Outlook to send emails



CGibson
2015-02-12, 11:47
Hello,

There have been several discussions on how to send email from Lianja, such as
http://www.lianja.com/community/showthread.php?1438-Sending-email-through-gmail-SSL

Also mentioned was a built in email function called sendmail.
http://www.lianja.com/doc/index.php/SENDMAIL()

Some issues that I noticed with sendmail include:
- knowing the smtp server address, username and password;
- determining if you need to use SMTP or ESTMP protocols;
- there is no record of a sent message;
- there is no notification for undelivered message

If you prefer to worry less about the details, you can use Microsoft Outlook to send/track emails.
Here is some basic code that seemed to work for me (using Windows 7).
This code should work regardless if Outlook is open/active or not.



emailAttach = getfile()
LOCAL loOutlook AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace
LOCAL loNewMsg AS Outlook.MailItem

#DEFINE olMailItem 0

loOutlook = CREATEOBJECT('Outlook.Application')
loNameSpace = loOutlook.GetNameSpace("MAPI")
loNameSpace.SendAndReceive(.T.)
loNewMsg = loOutlook.CreateItem(olMailItem)
loNewMsg.Attachments.Add(emailAttach)
loNewMsg.Subject = "This is the subject"
loNewMsg.Body = "Some information here."

loNewMsg.To = "abc@test.com;cba@test.com"
loNewMsg.CC = "info@test.com"
loNewMsg.Recipients.ResolveAll()
loNewMsg.Send()


Enjoy.

Cory

phabio
2015-02-12, 12:16
Thank you Cory to share this code.

Fabio

alanbourke
2015-02-13, 06:38
Possible downsides of MAPI:

Using MAPI like that won't use any stationery/signature that outgoing emails normally use.
Outlook security on a customer PC could well pop up a 'A program is trying to send a message on your behalf' warning that you have to click to close for each send(), which is fun when you're doing a batch of 500.
It'll only work on Windows machines with Outlook installed.

For those reasons I would always use SMTP even if it is more difficult to set up initially. But it's a use-case thing of course.