Hi,
this is the code I am using to send email with attachment.
I am able to send email without attachment with the below code by commenting the bold lines , but while using the bold lines it gives an exception stating the "clr object could not be created" on line 2
public void sendMail()
{
str sender = "abc"mail id of the sender
str recipient = "xyz"; //mulitple recipients can be specified
str cc = "pqr"; // mulitple cc id's can be specified
str subject = "seat master excel file";
str body = "Please find the attachment";
str fileName = @"C:\\Users\\admin\\Desktop\\seat_master.xlsx"; //Location of the attachment file
List toList;
List ccList;
ListEnumerator enumList;
Set permissionSet;
System.Exception exception;
str mailServer;
int mailServerPort;
System.Net.Mail.SmtpClient mailClient;
System.Net.Mail.MailMessage mailMessage;
System.Net.Mail.MailAddress mailFrom;
System.Net.Mail.MailAddress mailTo;
System.Net.Mail.MailAddressCollection mailToCollection;
System.Net.Mail.MailAddressCollection mailCCCollection;
System.Net.Mail.AttachmentCollection mailAttachementCollection;
System.Net.Mail.Attachment mailAttachment;
FileIOPermission permission;
;
try
{
toList = strSplit(recipient, ';');
ccList = strSplit(cc, ';');
permission = new FileIOPermission(fileName,'r');
permission.assert();
mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
mailClient = new System.Net.Mail.SmtpClient(mailServer,mailServerPort);
enumList = toList.getEnumerator();
enumList.moveNext();
mailFrom = new System.Net.Mail.MailAddress(sender);
mailTo = new System.Net.Mail.MailAddress(strLTrim(strRTrim(enumList.current())));
mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);
mailToCollection = mailMessage.get_To();
while (enumList.moveNext())
{
mailToCollection.Add(strLTrim(strRTrim(enumList.current())));
}
enumList = ccList.getEnumerator();
mailCCCollection = mailMessage.get_CC();
while (enumList.moveNext())
{
mailCCCollection.Add(strLTrim(strRTrim(enumList.current())));
}
mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
mailMessage.set_Subject(subject);
mailMessage.set_Body(body);
mailAttachementCollection = mailMessage.get_Attachments(); //1
mailAttachment = new System.Net.Mail.Attachment(fileName); //2
mailAttachment.set_Name("seat_master"); //3
mailAttachementCollection.Add(mailAttachment); //4
mailClient.Send(mailMessage);
mailMessage.Dispose();
CodeAccessPermission::revertAssert();
info("Email sent successfully");
}
catch (Exception::CLRError)
{
exception = ClrInterop::getLastException();
while (exception)
{
info(exception.get_Message());
exception = exception.get_InnerException();
}
CodeAccessPermission::revertAssert();
}
}
*This post is locked for comments