Sending email from Outlook with Xojo

Hi everyone,

Just I want to share a code that I’m using to send emails from Outlook using Xojo. I have “adapted” the code to work with OLEObject. Just keep in mind that the local machine must have installed Outlook application with an account configured and Outlook must be open.

I have created a small app with Xojo/RS and I have tested the code with Windows 7 and 8 using Outlook 2007 version. I have not had any problems. I hope it’s useful for who need it. The code is :

=====================>

dim objApp as OLEObject
dim objMail as OLEObject
dim sFile as String

sFile=“yourpathfile\yourpicture.jpg”

objApp=new OLEObject(“Outlook.Application”)
objMail=OutApp.CreateItem(0)
objMail.Value(“To”)=“someone@gmail.com
objMail.Value(“Subject”)=“Hello Xojo”
objMail.Value(“Body”)=“Hi Xojo Community”
objMail.Attachments.Add sFile
objMail.Send

MsgBox “Done ! Great !”

//error handler
Exception err

if err IsA RuntimeException then
MsgBox “The error message”
end if

Hi,

Please, a small correction, change objMail=OutApp.CreateItem(0) by objMail=objApp.CreateItem(0)

Thanks for sharing Elvis!

a better cross-platform approach could be in using ShowURL

ShowURL(“mailto:email@demo.de?Subject=something&Body=Something&attachment=filenameandpath”)

…quite same result but cross-platform and quite across all email apps if someone uses MS Mail or Mozilla Mail or something else…

[quote=127718:@Tomas Jakobs]a better cross-platform approach could be in using ShowURL

ShowURL(“mailto:email@demo.de?Subject=something&Body=Something&attachment=filenameandpath”)

…quite same result but cross-platform and quite across all email apps if someone uses MS Mail or Mozilla Mail or something else…[/quote]

Unfortunately, Windows 8 and 8.1 comes with no email program and mailto simply does nothing.

After testing for such system, one can assume they have a hotmail account, since this is required as default to log into Windows 8/8.1.

I have looked for a way to set mailto: for hotmail, and found a way at
http://stackoverflow.com/questions/10660529/mailto-link-not-working-in-hotmail

You can pull directly the hotmail mail compose page with this type of line :
email@domain.com&subject=My%20Subject%20Line&body=Email%20Body’ rel=‘nofollow external’ target=’_blank’ class=‘link-external’>http://www.hotmail.msn.com/secure/start?action=compose&to=email@domain.com&subject=My%20Subject%20Line&body=Email%20Body

You could use that to showURL instead of mailto:.

email@domain.com&subject=My%20Subject%20Line&body=Email%20Body' rel='nofollow external' target='_blank' class='link-external'>http://www.hotmail.msn.com/secure/start?action=compose&to=email@domain.com&subject=My%20Subject%20Line&body=Email%20Body

While the OP method with an OLE to Outlook is pertinent under Windows 7 which bundles Outlook Express, I believe a much better solution to send email from Xojo under Windows 8/8.1 is simply to use SMTPSocket, if the user knows the settings to apply.

Really? I do not own Win8.x but as far as I know there is still an Email App left in Win8:
http://apps.microsoft.com/windows/de-de/app/mail-calendar-and-people/64a79953-cf0b-44f9-b5c4-ee5df3a15c63

If you use Services like Hotmail, Gmail etc. without any Email App, this won’t work.

A few notes on this - I’ve done a fair bit of work with Outlook OLE stuff and sending emails…

  • If you want multiple recipients, separate them with a semicolon (i.e. tom@example.com; hamish@example.com)
  • If you want multiple attachments, just repeat the attachment add line.
  • Different versions of Outlook are VERY picky about things, so handle exceptions as if your life depended on it.
  • There is no way to programmatically add an existing signature to a mail item you’re creating like this.
  • If you want to display the message in Outlook instead of sending it, use
objMail.Invoke( "Display" )

but be sure to wrap that in a try block which catches an OLEException, because Outlook 2007 doesn’t like invoking display.

  • Using a mailto: link is a quick and dirty way of doing it, but you’re limited to 2083 characters, so if you’ve got a long email, or an HTML email, or if you want to send an attachment, you have to do it this way.
  • Using SMTPSockets requires the user to know what their SMTP server address is. Through bitter experience we’ve found that most people haven’t a clue what that is!

H

[quote=127749:@Tomas Jakobs]Really? I do not own Win8.x but as far as I know there is still an Email App left in Win8:
http://apps.microsoft.com/windows/de-de/app/mail-calendar-and-people/64a79953-cf0b-44f9-b5c4-ee5df3a15c63

If you use Services like Hotmail, Gmail etc. without any Email App, this won’t work.[/quote]

Thank you for posting this. I was not aware of that particular app. But contrary to Outlook Express in Windows 7, it is not bundled as default in Windows 8. One has to go to the Windows Store and download it from there.

That lack of email client in Windows 8/8.1 is a thorny challenge when it comes to add email capabilities to a Xojo app. While mailto: works flawlessly on every other computer, it simply does nothing there. I find that quite preposterous from Microsoft, since email is now part of everyday life. But who am I to argue the desktop leader ?

Let us say I want to create an app that send email through mailto: which can perform adequately under Windows 8.

After checking that the app is running in Windows 8, I need to check if an email client is installed. It should be possible to dig somewhere in the registry to find out if an app has been assigned to mailto:.

If no email client is present, it becomes a mess. You can use SMTP socket directly from within the app, which would seem the simplest solution, but how do you manage the somewhat complex email settings ? Will the user know what to do ? Alternatively, you can provide your own SMTP server, but run a terrible risk that a user uses that for spamming and your domain gets blacklisted.

Then there are the major web based mail services. I just tried the method I posted above for hotmail. It works fine, expect the search in the URL bar can interfere. I have recently switched search to duckduckgo, and guess what ? The tip I posted no longer works. Sorry.

Do web mail services provide some sort of URL arguments API that allows some sort of mailto ?

The Hotmail method I posted above is wrong. That is the reason why it did not work. Here is the good one :

http://www.hotmail.msn.com/secure/start?action=compose&to=email@domain.com&subject=My%20Subject%20Line&body=Email%20Body

So at least Hotmail provides the functionality. Now what about GMail, Yahoo, AOL, and others ? A mess, I tell you …

As far as I know you just need to check if mailto: URI scheme is registred. It’s quite the same with callto:, ftp: and http:

http://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx

As I said before, the limit on URLs is 2083 characters within many browsers, which means that mailto isn’t a practical way of doing it. I’ve not seen any mailto APIs in web mail services, and I’ve looked before; going via real APIs using CURL is probably a better way to go.
H

Hamish, don’t mix Web and Desktop. While websites and webservers do have this Limit, the handling of Desktop URI Strings depends on the App itself. Until now I do not know any Outlook or Mail Program which failed on URI sized > 2048 chars. This does not mean that there is no limitation. Maybe somebody else can find out.

Hi there -
Well, that’s interesting. I’ve just tried it with a 10,000 character URL in Mac Mail, and it’s worked just fine…

:wink: that’s why I am using ShowURL in my software for years. I do not care about email apps anymore as far as somebody has installed it properly on his system. The remaining reasons for not to use ShowURL are only mass mailings or automated mailings in background daemons or apps.

years ago (I’ve refactored using smtpsocket now), i used autoIt to do this.
you can create scripts (and there are pre-written ones available) to automate lots of windows stuff

you can compile to an exe as well, so you don’t need the client to have auto it and then distribute it with your app.

don’t know if it helps or not, but it might be worth a look

Hi,

Thank’s to all for your comments and points of view. Just to clarify that the code is used for a desktop App developed to a customer ,where Outlook is mandatory because is installed and licensed for all PC’s of the company which work with W7 and Office 2007.

In other cases and when the customer has a corporate mail server, a different option is provided. Depend of the customer’s environment.