Thursday, October 11, 2012

Sending PEC (Certified E-mail) with AX

A PEC (Certified E-mail) is not very different from a usual e-mail.

You just have to use SSL to secure the connection while sending the mail.

Usually I use the class System.Net.Mail to send mails with Ax, and this one has a useful property named EnableSsl that could work. But with a PEC, no, this isn't working.

That's because, to send a PEC you need an Implicit SSL, while System.Net.Mail only supports “Explicit SSL”.

 Reference: HERE

 And, as stated in the MSDN:

The EnableSsl property specifies whether SSL is used to access the specified SMTP mail server.
The default value for this property can also be set in a machine or application configuration file. Any changes made to the EnableSsl property override the configuration file settings.
The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.
An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

So, if you want to send a mail with Implicit SSL, you have to use a deprecated class from the old 2.0 .NET Framework, that is System.Web.Mail

Using this class is not difficult, and you can find a working sample down here:


 static void SendPECMail(Args _args)  
 {  
   System.Web.Mail.MailMessage  newMail;  
   System.Collections.IDictionary fields;  
   System.Collections.IList    attachementCollection;  
   System.Web.Mail.MailAttachment attachment;  
   try  
   {  
     new InteropPermission(InteropKind::ClrInterop).assert();  
     newMail = new System.Web.Mail.MailMessage();  
     fields = newMail.get_Fields();  
     fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtps.pec.aruba.it");  
     fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");  
     fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");  
     fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");  
     fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "lmattiuzzo@pec.it");  
     fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");  
     fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");  
     newMail.set_From("lmattiuzzo@pec.it");  
     newMail.set_To("santa@klaus.com");  
     newMail.set_Subject("subject");  
     newMail.set_BodyFormat(System.Web.Mail.MailFormat::Html);  
     newMail.set_Body("body of your message");  
     newMail.set_Priority(System.Web.Mail.MailPriority::High);  
     attachementCollection = newMail.get_Attachments();  
     attachment = new System.Web.Mail.MailAttachment(@"c:\file.pdf");  
     attachementCollection.Add(attachment);  
     System.Web.Mail.SmtpMail::set_SmtpServer("smtps.pec.aruba.it:465");  
     System.Web.Mail.SmtpMail::Send(newMail);  
     CodeAccessPermission::revertAssert();  
   }  
   catch( Exception::CLRError )  
   {  
     throw error(AifUtil::DEVgetClrErrorMessage());  
   }  
 }  

3 comments:

Anonymous said...

I have to say, while looking through hundreds of blogs daily, the theme of this blog is different (for all the proper reasons). If you do not mind me asking, what's the name of this theme or would it be a especially designed affair? It's significantly better compared to the themes I use for some of my blogs.

Anonymous said...

wow, this one is good, gonna use for another sport but i think it'll be just as good, BIG thanks :)

Ludovico Mattiuzzo said...

Oh, that's just a default theme that I found on blogger, nothing that I worked on!