Unable to send attachment in sending mail in Ax 2009 !

Hi

I am able to send mail if i run the job but if i add attachment code it goes to visual studio debugger some win32 exception and AX closes.

Is it possible to add attachment in AX 2009 ?

static void TestMail(Args _args)

{

CustTable custTable;

System.Text.StringBuilder htmlTable;

SysMailer mailer;

str filePathName;

;

// filePathName = @‘C:\Test.txt’;

// mailer.attachments().add(filePathName);

htmlTable = new System.Text.StringBuilder();

htmlTable.Append(@“Company Customers”);

htmlTable.Append(@"
");

htmlTable.Append("");

while select custTable where custtable.CustGroup == ‘FLAT1’

{

htmlTable.Append("

");

htmlTable.Append("

");

htmlTable.Append("

");

htmlTable.Append("

");

}

htmlTable.Append("

");

htmlTable.Append(custTable.AccountNum);

htmlTable.Append("

");

htmlTable.Append(custTable.NameAlias);

htmlTable.Append("

");

mailer = new SysMailer();

//building mail details

mailer.htmlBody(htmlTable.ToString());

mailer.subject(“Automated Mail - Customers”);

mailer.fromAddress(“AAA@abc.com”);

mailer.tos().appendAddress(" BBB@abc.com");

// Setting SMTP details

mailer.SMTPRelayServer(“10.11.12.123”,25,“myname”,“mypassword”,false);

// Sending Mail

mailer.sendMail();

}

It definitely is possible to attach files to e-mails in AX2009. What’s the exception you get?

Hi Martin,

Visual Studio Just-In time Debugger comes saying “An unhandled exception win32 occurred in ax32.exe”.

I can successfully send e-mails with attachment using your code (after I added code for attachments to the right place, of course).

Try to catch the exception and get COMError from the underlying COM object by calling invoking its error() method. Also verify that the file is accessible (it exists, it’s on the right tier and you have permissions to read it).

You can also use the following code for sending mail.

SysEmailTable::sendmail() method and pass the parameters accordingly.

Hi Martin,

Thanks again for your valuable quick reply but could you please elaborate a little bit more.

I don’t know how to catch COM exception.

I can give you some direction, but you may need to adjust it according your situation.

You said that the problem is when adding an attachment - that’s done by a COM call in SysMailerAttachments.add():

_email.addAttachment(_url);

I would try to catch errors and check whether anything is returned from the error() method:

COMError comError;

try
{
    _email.addAttachment(_url);
}
catch (Exception::Error)
{
    comError = _email.error();
    if (comError)
    {
        throw error(comError.description());
    }
}

Changed the SysMailer class’s new method and did compile forward the class and my problem got solved.

void new(COM c = null)
{
COM cdoConfig;
InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
;

permission.assert();
c = new COM(‘CDO.Message’);
_COM = c;

//BP Deviation Documented
if (_COM.configuration() == null)
{
//BP Deviation Documented
cdoConfig = new COM(‘CDO.Configuration’);
_configuration = SysMailerConfiguration::create(cdoConfig);
//BP Deviation Documented
this.configuration(_configuration);
}
else
{
//BP Deviation Documented
_configuration = SysMailerConfiguration::create(_COM.configuration());
}

//BP Deviation Documented
_fields = _configuration.fields();
_fields.add(#sendUsing,#cdoSendUsingPort);
_fields.resync();
}