Microsoft Dynamics Ax developer's blog

Thursday, June 21, 2007

Sending attachments using the "mailto:" protocol

It works with Outlook at least. I think this example job is enough

static void Test_MailTo(Args _args)
{
str recepient='mbelugin@gmail.com';
str subject = 'Test';
str attachment = @'c:\AUTOEXEC.BAT';
str url = strFmt(
'mailto:%1?Subject=%2&attachments=""%3""',

recepient,
subject,
attachment
);
;
WinApi::shellExecute(url);
}

12 comments:

Muhammad Shujaat Siddiqi said...

This does not seem to work saying some switch not correct. When i removed attachment it started working.
Can u help?

Max Belugin said...

What version of outlook do you use?

Unknown said...

The question is whether this is the recommended way to send mail or not ? Could you talk a bit about that ? (I mean compared to SysMail and SysINetMail)

Max Belugin said...

It depends on what do you want.

SysMailer sends E-Mail without wny interaction with user.

Mailto protocol opens new message window and allows to edit the message

Unknown said...

But why use the shell API and not the COM Class Wrappers (SysOutlook* classes) to send mail using much greater customization ? That sort of defeats the purpose of office and axapta integration no ?

Max Belugin said...

Can you post an equivalent example using SysOutlook

Unknown said...

static void Job4(Args _args)
{
COM sysOutlookApplication;
SysOutlook_NameSpace sysOutlookNameSpace;
SysOutlookMapiFolder sysOutlookMapiFolder;
SysOutlook_Folders sysOutlookFolders;
SysOutlookAttachments sysOutlookAttachments;
SysOutlookAttachment sysOutlookAttachment;
SysOutlook_Items collection;
COM item;
COMVariant type;
COMVariant _source;
COMVariant _type;
COMVariant _position;
COMVariant _displayname;
;

#sysOutLookComDef

sysOutlookApplication = new COM("Outlook.Application");
sysOutlookNameSpace = sysOutlookApplication.getNameSpace("MAPI");

sysOutlookNameSpace.logon();
sysOutlookFolders = sysOutlookNameSpace.Folders();
sysOutlookMapiFolder = sysOutlookNameSpace.getDefaultFolder(#OlDefaultFolders_olFolderOutbox);

collection = sysOutlookMapiFolder.Items();
type = new COMVariant();
type.int(#OlItemType_olMailItem);
item = collection.add(type);
sysOutlookAttachments = item.attachments();
//attach sample file
_source = COMVariant::createFromStr(@"C:\sigmatel.log");
_type = COMVariant::createNoValue();
_position = COMVariant::createNoValue();
_displayname = COMVariant::createNoValue();
sysOutlookAttachments.add(_source,_type,_position,_displayname);
item.to("raza.abbas@gmail.com");
item.subject("test message");
item.body("test body");
item.display();
}

you could so item.send() to send the mail without seeing the message window too. Besides this, there's another class called the SysINetMail which uses Outlook too.

Muhammad Shujaat Siddiqi said...

I am using the same version of Microsoft Outlook that comes with Office 2007. I think it is 12.0...

Max Belugin said...

I am using O2003 so it can be a version depended difference

Unknown said...

Adding attachements from the mailto protocol was considered a security issue that has been fixed in outlook 2007. So no more attachements in the mailto link possible ...

Alex said...

Once my friend opened MS Outlook and showed that all mails were damaged.But fortunately I advised him next utility-converting ost to pst files.Program solved his problems in a minute and free of charge.Besides tool showed how read Microsoft Outlook .ost files and extract all necessary data from it.

Nhel said...

I copied codes of Razza,but I encountered error.

Variable SysOutlook_NameSpace has not been declared.

Do I have to do something with my AX environment.Im using AX 2009 kernel version 5.0.1000.52