Tuesday, 3 January 2017

Send Email with Attachment x++ for port 465 or 587

This code is also useful for below error.

Method 'send' in COM object of class 'CDO.Message' returned error code 0x8004020E (<unknown>) which means: <unknown>.

 For SSL enabled mail servers. Like: gmail, or office365 smtp.gmail.com with port 465 or 587 you can use below code

public void email(filename ReportPath)
{
    System.Net.Mail.MailMessage             mailMessage;
    System.Net.Mail.SmtpClient              myMail;
    System.Net.Mail.MailAddressCollection   mailcoll;
    System.Net.Mail.MailAddress             mailFrom;
    System.Net.Mail.MailAddress             mailTo;
    System.Net.Mail.Attachment              attachment;
    System.Net.Mail.AttachmentCollection    attachementCollection;

    str                                     receiverMailAddress;
    str                                     mailBody;
    str                                     smtpServer;
    str                                     mailSubject;
    int                                     SMTPPort;


    #File
    str                 mail;
    userinfo            userInfo;
    str pwd;
    SysEmailParameters parameters = SysEmailParameters::find();
    ;
    new InteropPermission(InteropKind::ClrInterop).assert();

    receiverMailAddress = HcmWorker.email();

    if(receiverMailAddress != "") // check if email id exists for employee then send a mail
    {

            mailSubject         = "Pay slip for "+ mthName(Monthname) +"-"+int2str(YearName);
            mailFrom            = new  System.Net.Mail.MailAddress(parameters.SMTPUserName ,"DAX Info");
            mailTo              = new  System.Net.Mail.MailAddress(receiverMailAddress);
            mailcoll            = new  System.Net.Mail.MailAddressCollection();
            mailBody            = "Hello "+HcmWorker.name()+", Kindly Find the attachment salary slip"; // Body Should be in HTML Format;


            try
            {
            smtpServer          = SysEmaiLParameters::find(false).SMTPRelayServerName;// using the SMTP server ip //setup in email Parameters
            mailMessage         = new System.Net.Mail.MailMessage(mailFrom,mailTo);
            mailmessage.set_Subject(mailSubject);
            mailmessage.set_Body(mailBody);

            attachementCollection = mailMessage.get_Attachments();
            attachment = new System.Net.Mail.Attachment(ReportPath);
            attachementCollection.Add(attachment);


            SMTPPort            = SysEmaiLParameters::find(false).SMTPPortNumber;
            myMail              = new System.Net.Mail.SmtpClient(smtpServer, SMTPPort);

            myMail.set_EnableSsl(true); // For SSL enabled mail servers. Ex: gmail, smtp.gmail.com, port 465 or 587

            pwd = SysEmaiLParameters::password();

            mymail.set_Credentials(New System.Net.NetworkCredential(parameters.SMTPUserName, pwd));

            mymail.Send(mailmessage);
            }
            catch(Exception::CLRError)
            {
               throw Exception::CLRError;
            }

            mailMessage.Dispose();
            CodeAccessPermission::revertAssert();
    }
}

No comments:

Post a Comment