Code to send email with attachment using dynamics MSD axapta x++


Code to send email with attachment using dynamics axapta x++

void SendEMail()
{
    System.Net.Mail.MailMessage              Message;
    System.Net.Mail.Attachment              attachment;
    System.Net.Mail.AttachmentCollection    attachementCollection;
    System.Net.Mail.SmtpClient              smtpClient;
    System.Net.Mail.MailAddress             emailfrom;
    System.Net.Mail.MailAddress             emailto;
    str                                     messageBody ;
    str                                     Subject;
    str                                     SMTPServer;
    str                                     Filenm;
    FileIOPermission                        permission1;
    ;
    emailfrom = new System.Net.Mail.MailAddress("From@xyz.com","");
    mailAddressTo = new System.Net.Mail.MailAddress("To@abc.com","");
    messageBody = "This is test mail";
    Subject = "Subject";
    SMTPServer = SysEmailParameters::find(false).SMTPRelayServerName;
    mailMessage = new System.Net.Mail.MailMessage(emailfrom, mailAddressTo);
    mailmessage.set_Subject(Subject);
    mailmessage.set_Body(messageBody);
    attachementCollection = mailMessage.get_Attachments();
    Filenm = "C:\\sample.bmp";
    permission1= new FileIOPermission(Filenm,'w');
    permission1.assert();
    attachment = new System.Net.Mail.Attachment(Filenm);
    attachementCollection.Add(attachment);
    smtpClient = new System.Net.Mail.SmtpClient(SMTPServer);
    smtpClient.Send(mailmessage);
    CodeAccessPermission::revertAssert();
}