Search your query

Friday, May 26, 2017

X++ code to attach the text file from local system directory and send an email.

class EmailTextFile
{      
   
    /// <summary>
    /// Attach the text file from local system directory and send an email using X++.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)
    {    
        EmailTextFile emailTextFile = new EmailTextFile();
       
        emailTextFile .sendMail();
    }
 
    private void sendMail()
    {
        SysMailerMessageBuilder     sysMailBuilder = new SysMailerMessageBuilder();
        SysMailerSMTP               smtp           = new SysMailerSMTP();
        System.IO.StreamReader      reader         = new System.IO.StreamReader(@"C:\test.txt");
        System.Byte[]               byte           = reader.CurrentEncoding.GetBytes(reader.ReadToEnd());
        System.IO.Stream            stream         = new System.IO.MemoryStream(byte);

        sysMailBuilder.setSubject("Regarding - sending mail");
        sysMailBuilder.setFrom("XXX@yyy.com");
        sysMailBuilder.addTo("yyy@zzz.com");
        sysMailBuilder.addAttachment(stream,'text file');
        smtp.sendNonInteractive(sysMailBuilder.getMessage());          
     
    }

}

No comments:

Post a Comment