Issue in sending a mail

Hi all,

I have written a code, to send a mail from axapta. But i am facing an issue that ax application is closing automatically when i execute my code. code snippet as follows,


void sendSplittedMail(Email _emailId, NoYes _ediOrNonEDI, str _mailBody)
{
int retryCount = 3;
;
_mailBody += "@CHK862" + "@CHK864" + "@CHK865"; // <tr align = center><td>&nbsp;</td><td>&nbsp;</td>, <td>&nbsp;</td><td>&nbsp;</td>, <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
_mailBody += "@CHK862" + strfmt("@CHK863",cartonsTotal,grossWeightTotal) + "@CHK865"; // <tr align = center><td>&nbsp;</td><td>&nbsp;</td>, <td>%1</td><td>%2</td>, <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
_mailBody += "@CHK862" + "@CHK864" + "@CHK865" + "@CHK866"; // <tr align = center><td>&nbsp;</td><td>&nbsp;</td>, <td>&nbsp;</td><td>&nbsp;</td>, <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>, </table>
_mailBody += this.addRemarks(); // Add remarks to mail
_mailBody += "@CHK867"; // <br><br><div>Best regards,</div>
_mailBody += "@CHK868"; // <div>Regional Operations Dept.</div>
_mailBody += "@CHK869"; // </div>

mail.htmlBody(_mailBody);

try
{
**mail.sendMail(); // calls sendMail method in Sysmailer class**

}
catch (Exception::Error)
{
retryCount--;

if(retryCount > 0)
{
retry;
}
else
{
throw(Exception::Error);
}
}
if(!isBatchProcess && NoYes::No == _ediOrNonEDI)
{
info(strfmt("@CHK784",_emailId)); // Email: %1
}
else
{
setPrefix(strfmt("@CHK784",_emailId)); // Email: %1
}

}

**sendMail** Method in **"Sysmailer"** class

// Send mail
public void sendMail()
{ InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
;

permission.assert();

//BP Deviation Documented
**_COM.send();**
}

When this “_COM.send” method is called, the AX application is getting closed.

Can any one help me!

Thanks in advance!!!

Regards,

Deepak M.

Well you didn’t copy enough code. Did you initialize your sysMailer object? (mail = new SysMailer():wink:

Did you do mail.SMTPRelayServer and initalize what mail server it should connect to? Did you add who the mailer should send the message to? I believe you still need to assert the ComInterop permission, even though it’s in the sendMail method, but I’m not sure on that one.

Hi alex,

Thanks for your reply,

I have done these initialization for sysMailerclass, ComInterop permission, and all mail addresses are given properly, but also the AX is getting closed.

Code snippet as follows,

[code]

void reset()
{
InteropPermission permission= new InteropPermission(InteropKind::ComInterop);
;
permission.assert();
mail = new SysMailer();
codeAccessPermission::revertAssert();
mail.SMTPRelayServer(sysEmailParameters.SMTPRelayServerName,
sysEmailParameters.SMTPPortNumber,
sysEmailParameters.SMTPUserName,
sysEmailParameters.SMTPPassword,
sysEmailParameters.NTLM);

pdfInvoice = new Set(Types::String);
excelInvoice = new Set(Types::String);
pdfPackingSlip = new Set(Types::String);
excelPackingSlip = new Set(Types::String);

cartonsTotal = 0;
grossWeightTotal = 0;
mailAttachmentSize = 0;
}

public Boolean sendMail(Email _emailId, Container _shipments)
{
int counter;
int noOfPDFAttachmentsForInvoice, noOfExcelAttachmentsForInvoice, noOfPDFAttachmentsForPackingSlip, noOfExcelAttachmentsForPackingSlip;
Str mailBody;
Str fileName;
int recordCounter,dataCounter;
CNLIN_FreightForwarderInfo freightForwarderInfoTable;
CNLIN_ShipmentIds shipmentIds;
Boolean isExcelAttached, isPDFAttached, sendMail, isFileAttached;
int attachmentSize, i;
Container limitedAttachment, splitedAttachment;

void sendMailImmediately()
{
if(mailAttachmentSize > (maxAttachmentSize * 1000000))
{
mailBody += this.mailBody(recordCounter, _shipments, counter);
this.sendSplittedMail(_emailId, conpeek(conpeek(_shipments,1),3), mailBody);
this.reset();
mail.fromAddress(parm.CNLIN_HKEmailId);
mail.ccs().appendAddress(parm.CNLIN_HKEmailId);
mail.tos().appendAddress(_emailId);
}
}
;

if(!isBatchProcess)
{
sysEmailParameters = SysEmailParameters::find(false);
maxAttachmentSize = sysEmailParameters.MaxEmailAttachmentSize;

//maxAttachmentSize in megabytes
if (maxAttachmentSize < 1)
maxAttachmentSize = #maxAttachmentSizeDefault;
}

this.reset();

parm = SalesParameters::find();
if(!parm.CNLIN_HKEmailId)
{
throw error("@CHK673"); // HK Email Id cannot be blank
}
else
{
mail.fromAddress(parm.CNLIN_HKEmailId);
mail.ccs().appendAddress(parm.CNLIN_HKEmailId);
}
mail.tos().appendAddress(_emailId);

for(counter = 1; counter <= conlen(_shipments); counter++)
{
sendMail = isBatchProcess? conpeek(conpeek(_shipments, counter), 21): conpeek(conpeek(_shipments, counter), 17);
if(_emailId == conpeek(conpeek(_shipments, counter), 1)
&& NoYes::Yes == conpeek(conpeek(_shipments, counter), 6)
&& sendMail)
{
recordCounter++;

isPDFAttached = isBatchProcess? conpeek(conpeek(_shipments, counter), 18): conpeek(conpeek(_shipments, counter), 2);
isExcelAttached = isBatchProcess? conpeek(conpeek(_shipments, counter), 19): conpeek(conpeek(_shipments, counter), 15);
salesId = conpeek(conpeek(_shipments, counter), 26);
if(NoYes::Yes == isPDFAttached)
{
// Attaching Invoice PDF file
fileName = this.generatePDForExcelInvoice(conpeek(conpeek(_shipments, counter), 4), #pdf, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 22))); // %1
if(fileName)
{
sendMailImmediately();
mail.attachments().add(fileName);
noOfPDFAttachmentsForInvoice++;
}

// Attaching Packing Slip PDF file salesTable.SalesId
fileName = this.generatePDForExcelPackingSlip(conpeek(conpeek(_shipments, counter), 4), #pdf, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 23))); // %1
if(fileName)
{
sendMailImmediately();
mail.attachments().add(fileName);
noOfPDFAttachmentsForPackingSlip++;
}
}
if(NoYes::Yes == isExcelAttached)
{
// Attaching Invoice Excel file
fileName = this.generatePDForExcelInvoice(conpeek(conpeek(_shipments, counter), 4), #excel, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 24))); // %1
if(fileName)
{
sendMailImmediately();
mail.attachments().add(fileName);
noOfExcelAttachmentsForInvoice++;
}

// Attaching Packing Slip Excel file
fileName = this.generatePDForExcelPackingSlip(conpeek(conpeek(_shipments, counter), 4), #excel, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 25))); // %1
if(fileName)
{
sendMailImmediately();
mail.attachments().add(fileName);
noOfExcelAttachmentsForPackingSlip++;
}
}

mailBody += this.mailBody(recordCounter+1, _shipments, counter);
}
}

if(recordCounter > 0)
{
this.sendSplittedMail(_emailId, conpeek(conpeek(_shipments,1),3), mailBody);
info(strfmt("@CHK912", noOfPDFAttachmentsForInvoice)); // No of Invoice PDF attached: %1
info(strfmt("@CHK913", noOfExcelAttachmentsForInvoice)); // No of Invoice Excel attached: %1
info(strfmt("@CHK914", noOfPDFAttachmentsForPackingSlip)); // No of Packing Slip PDF attached: %1
info(strfmt("@CHK915", noOfExcelAttachmentsForPackingSlip)); // No of Packing Slip Excel attached: %1
return true;
}
else
{
return false;
}
}

[\code]

Hi alex,

[Sorry I have not pasted this code in previous post]

[Thanks for your reply, but even i have some more clarification please see my comments]

In my code I have done the initialization for Sysmailer Class, InteropPermission is provided and all mail addresses has given properly. But also AX application is getting closed automatically.

code snippet as follows,


void reset()
{
InteropPermission permission= new InteropPermission(InteropKind::ComInterop);
;
**permission.assert();**
**mail = new SysMailer();**
**codeAccessPermission::revertAssert();**
**mail.SMTPRelayServer(sysEmailParameters.SMTPRelayServerName,**
**sysEmailParameters.SMTPPortNumber,**
**sysEmailParameters.SMTPUserName,**
**sysEmailParameters.SMTPPassword,**
**sysEmailParameters.NTLM);**

pdfInvoice = new Set(Types::String);
excelInvoice = new Set(Types::String);
pdfPackingSlip = new Set(Types::String);
excelPackingSlip = new Set(Types::String);

cartonsTotal = 0;
grossWeightTotal = 0;
mailAttachmentSize = 0;
}

public Boolean sendMail(Email _emailId, Container _shipments)
{
int counter;
int noOfPDFAttachmentsForInvoice, noOfExcelAttachmentsForInvoice, noOfPDFAttachmentsForPackingSlip, noOfExcelAttachmentsForPackingSlip;
Str mailBody;
Str fileName;
int recordCounter,dataCounter;
CNLIN_FreightForwarderInfo freightForwarderInfoTable;
CNLIN_ShipmentIds shipmentIds;
Boolean isExcelAttached, isPDFAttached, sendMail, isFileAttached;
int attachmentSize, i;
Container limitedAttachment, splitedAttachment;

void sendMailImmediately()
{
if(mailAttachmentSize > (maxAttachmentSize * 1000000))
{
mailBody += this.mailBody(recordCounter, _shipments, counter);
this.sendSplittedMail(_emailId, conpeek(conpeek(_shipments,1),3), mailBody);
this.reset();
mail.fromAddress(parm.CNLIN_HKEmailId);
mail.ccs().appendAddress(parm.CNLIN_HKEmailId);
mail.tos().appendAddress(_emailId);
}
}
;

if(!isBatchProcess)
{
sysEmailParameters = SysEmailParameters::find(false);
maxAttachmentSize = sysEmailParameters.MaxEmailAttachmentSize;

//maxAttachmentSize in megabytes
if (maxAttachmentSize < 1)
maxAttachmentSize = #maxAttachmentSizeDefault;
}

this.reset();

parm = SalesParameters::find();
if(!parm.CNLIN_HKEmailId)
{
throw error("@CHK673"); // HK Email Id cannot be blank
}
else
{
**mail.fromAddress(parm.CNLIN_HKEmailId);**
**mail.ccs().appendAddress(parm.CNLIN_HKEmailId);**
**}**
**mail.tos().appendAddress(_emailId);**

for(counter = 1; counter <= conlen(_shipments); counter++)
{
sendMail = isBatchProcess? conpeek(conpeek(_shipments, counter), 21): conpeek(conpeek(_shipments, counter), 17);
if(_emailId == conpeek(conpeek(_shipments, counter), 1)
&& NoYes::Yes == conpeek(conpeek(_shipments, counter), 6)
&& sendMail)
{
recordCounter++;

isPDFAttached = isBatchProcess? conpeek(conpeek(_shipments, counter), 18): conpeek(conpeek(_shipments, counter), 2);
isExcelAttached = isBatchProcess? conpeek(conpeek(_shipments, counter), 19): conpeek(conpeek(_shipments, counter), 15);
salesId = conpeek(conpeek(_shipments, counter), 26);
if(NoYes::Yes == isPDFAttached)
{
// Attaching Invoice PDF file
fileName = this.generatePDForExcelInvoice(conpeek(conpeek(_shipments, counter), 4), #pdf, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 22))); // %1\
if(fileName)
{
sendMailImmediately();
if(attachedInvoiceId)
pdfInvoice.add(attachedInvoiceId);
mail.attachments().add(fileName);
noOfPDFAttachmentsForInvoice++;
}

// Attaching Packing Slip PDF file salesTable.SalesId
fileName = this.generatePDForExcelPackingSlip(conpeek(conpeek(_shipments, counter), 4), #pdf, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 23))); // %1\
if(fileName)
{
sendMailImmediately();
if(attachedPackingSlipId)
pdfPackingSlip.add(attachedPackingSlipId);
mail.attachments().add(fileName);
noOfPDFAttachmentsForPackingSlip++;
}
}
if(NoYes::Yes == isExcelAttached)
{
// Attaching Invoice Excel file
fileName = this.generatePDForExcelInvoice(conpeek(conpeek(_shipments, counter), 4), #excel, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 24))); // %1\
if(fileName)
{
sendMailImmediately();
if(attachedInvoiceId)
excelInvoice.add(attachedInvoiceId);
mail.attachments().add(fileName);
noOfExcelAttachmentsForInvoice++;
}

// Attaching Packing Slip Excel file
fileName = this.generatePDForExcelPackingSlip(conpeek(conpeek(_shipments, counter), 4), #excel, strfmt("@CHK924",conpeek(conpeek(_shipments, counter), 25))); // %1\
if(fileName)
{
sendMailImmediately();
if(attachedPackingSlipId)
excelPackingSlip.add(attachedPackingSlipId);
mail.attachments().add(fileName);
noOfExcelAttachmentsForPackingSlip++;
}
}
mailBody = "";
mailBody += this.mailBody(recordCounter, _shipments, counter);
}
}

if(recordCounter > 0)
{
this.sendSplittedMail(_emailId, conpeek(conpeek(_shipments,1),3), mailBody);
info(strfmt("@CHK912", noOfPDFAttachmentsForInvoice)); // No of Invoice PDF attached: %1
info(strfmt("@CHK913", noOfExcelAttachmentsForInvoice)); // No of Invoice Excel attached: %1
info(strfmt("@CHK914", noOfPDFAttachmentsForPackingSlip)); // No of Packing Slip PDF attached: %1
info(strfmt("@CHK915", noOfExcelAttachmentsForPackingSlip)); // No of Packing Slip Excel attached: %1
return true;
}
else
{
return false;
}
}

Regards,

Deepak M.