Visualizzazione post con etichetta C#. Mostra tutti i post
Visualizzazione post con etichetta C#. Mostra tutti i post

venerdì 31 gennaio 2014

C# - How to send an email using the socket

The standard .NET library give you an easy and quick way to send an email in C#. But sometimes you need to set some values in the header and it's not possible to do that using System.Net.Mail.MailMessage. An example is the enveloper from and to set it, the only way, is to set the sender. What if do you need a different sender and envelope from? One way is to send the mail using the socket.

 using System;  
 using System.IO;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Net;  
 using System.Text;  
 using System.Net.Sockets;  
 using Admin.Models;  
 using System.Net.Mail;  
 namespace Admin.Services.Implementation  
 {  
   /// <summary>  
   ///  <para>Wraps an smtp request</para>  
   /// </summary>  
   public class SmtpService : ISmtpService  
   {  
     /// <summary>  
     /// Get / Set the name of the SMTP mail server  
     /// </summary>  
     private enum SMTPResponse : int  
     {  
       CONNECT_SUCCESS = 220,  
       GENERIC_SUCCESS = 250,  
       DATA_SUCCESS = 354,  
       QUIT_SUCCESS = 221  
     }  
     private const int timeOutLimit = 1000;  
     bool ISmtpService.Send(MailMessage message, string SmtpServer, string returnPath)  
     {  
       IPAddress localIPAddress = IPAddress.Parse(SmtpServer);  
       IPEndPoint endPt = new IPEndPoint(localIPAddress, 25);  
       using (Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp))  
       {  
         //set timeout  
         s.ReceiveTimeout = timeOutLimit;  
         s.SendTimeout = timeOutLimit;  
         // try to establish the connection  
         s.Connect(endPt);  
         if (!Check_Response(s, SMTPResponse.CONNECT_SUCCESS))  
         {  
           s.Close();  
           return false;  
         }  
         // say hello to the server to identify myself  
         SendData(s, string.Format("HELO {0}\r\n", Dns.GetHostName()));  
         if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))  
         {  
           s.Close();  
           return false;  
         }  
         // set the mail from in the envelope  
         SendData(s, string.Format("MAIL From: {0}\r\n", returnPath));  
         if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))  
         {  
           s.Close();  
           return false;  
         }  
         // set the email to in the envelope  
         if (message.To.Count > 0)  
         {  
           foreach (var to in message.To)  
           {  
             SendData(s, string.Format("RCPT TO: {0}\r\n", to.Address));  
             if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))  
             {  
               s.Close();  
               return false;  
             }  
           }  
         }  
         else  
           return false;  
         // add bcc to the envelope  
         if (message.Bcc.Count > 0)  
         {  
           foreach (var to in message.Bcc)  
           {  
             SendData(s, string.Format("RCPT TO: {0}\r\n", to.Address));  
             if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))  
             {  
               s.Close();  
               return false;  
             }  
           }  
         }  
         // create the header as string builder  
         StringBuilder Header = new StringBuilder();  
         Header.Append("MIME-Version: 1.0\r\n");  
         // email priority  
         Header.Append("Importance: " + message.Priority.ToString() + "\r\n");  
         // set the from  
         Header.Append("From: " + message.From.DisplayName + " <" + message.From.Address + ">\r\n");  
         // set the reply to  
         Header.Append("Reply-To: " + message.ReplyToList.FirstOrDefault() + "\r\n");  
         // set the email to  
         Header.Append("To: ");  
         int i = 0;  
         foreach (var To in message.To)  
         {  
           Header.Append(i > 0 ? "," : "");  
           Header.Append(To.Address);  
           i++;  
         }  
         Header.Append("\r\n");  
         // set the cc  
         if (message.CC.Count > 0)  
         {  
           Header.Append("Cc: ");  
           i = 0;  
           foreach (MailAddress To in message.CC)  
           {  
             Header.Append(i > 0 ? "," : "");  
             Header.Append(To.Address);  
             i++;  
           }  
           Header.Append("\r\n");  
         }  
         // set the email subject  
         Header.Append("Subject: " + message.Subject + "\r\n");  
         Header.Append("Date: ");  
         Header.Append(DateTime.Now.ToString("R"));  
         Header.Append("\r\n");  
         string MsgBody = message.Body;  
         if (!MsgBody.EndsWith("\r\n"))  
           MsgBody += "\r\n";  
         //generate a unique boundary for this email  
         var uniqueBoundary = EncodeTo64("uniqueboundary" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));  
         Header.Append("Content-Type: multipart/alternative; boundary=" + uniqueBoundary + "\r\n");  
         Header.Append("This is a multi-part message in MIME format.\r\n");  
         // adding message body  
         SendData(s, ("DATA\r\n"));  
         if (!Check_Response(s, SMTPResponse.DATA_SUCCESS))  
         {  
           s.Close();  
           return false;  
         }  
         if (message.Attachments.Count > 0)  
         {  
           StringBuilder sb = new StringBuilder();  
           foreach (Attachment attachment in message.Attachments)  
           {  
             byte[] binaryData;  
             if (attachment != null)  
             {  
               sb.Append("--" + uniqueBoundary + "\r\n");  
               sb.Append("Content-Type: application/octet-stream; file=" + attachment.Name + "\r\n");  
               sb.Append("Content-Transfer-Encoding: base64\r\n");  
               sb.Append("Content-Disposition: attachment; filename=" + attachment.Name + "\r\n");  
               sb.Append("\r\n");  
               var fs = attachment.ContentStream;  
               binaryData = new Byte[fs.Length];  
               long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);  
               fs.Close();  
               string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);  
               for (int j = 0; j < base64String.Length; )  
               {  
                 int nextchunk = 100;  
                 if (base64String.Length - (j + nextchunk) < 0)  
                   nextchunk = base64String.Length - j;  
                 sb.Append(base64String.Substring(j, nextchunk));  
                 sb.Append("\r\n");  
                 j += nextchunk;  
               }  
               sb.Append("\r\n");  
             }  
           }  
           Header.Append(sb.ToString());  
         }  
         Header.Append("--" + uniqueBoundary + "\r\n");  
         // is an html email?  
         if (message.IsBodyHtml)  
         {  
           Header.Append("Content-Type: text/html; charset=\"UTF-8\"\r\n");  
           Header.Append("Content-Transfer-Encoding: 7bit\n");  
         }  
         else  
           Header.Append("Content-Type: text/plain; charset=\"UTF-8\"\r\n");  
         Header.Append("\r\n");  
         Header.Append(MsgBody);  
         Header.Append("--" + uniqueBoundary + "--\r\n");  
         Header.Append(".\r\n");  
         // send the email  
         SendData(s, Header.ToString());  
         if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))  
         {  
           s.Close();  
           return false;  
         }  
         // close the connection  
         SendData(s, "QUIT\r\n");  
         Check_Response(s, SMTPResponse.QUIT_SUCCESS);  
         s.Close();  
       }  
       return true;  
     }  
     // send a message using the socket  
     private static void SendData(Socket s, string msg)  
     {  
       byte[] _msg = Encoding.UTF8.GetBytes(msg);  
       s.Send(_msg, 0, _msg.Length, SocketFlags.None);  
     }  
     // check if the socket response watch the one expected  
     private static bool Check_Response(Socket s, SMTPResponse response_expected)  
     {  
       string sResponse;  
       int response;  
       byte[] bytes = new byte[1024];  
       s.Receive(bytes);  
       sResponse = Encoding.ASCII.GetString(bytes);  
       response = Convert.ToInt32(sResponse.Substring(0, 3));  
       if (response != (int)response_expected)  
         return false;  
       return true;  
     }  
     private string EncodeTo64(string toEncode)  
     {  
       byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);  
       string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);  
       return returnValue;  
     }  
   }  
 }  

Every time we send some data through the socket using the method SendData, we need to check the response from the server. If the answer is positive we proceed sending the next bunch of data.


lunedì 25 novembre 2013

C# - How to set the Mail From Envelope

When you send an email using System.Net.Mail you can set the field FROM but you can not set the SMTP MAIL FROM envelope. It is possible to set that value just using the MailMessage.Sender property. Here it is the code:

using (MailMessage mailMsg = new MailMessage())
{
    mailMsg.Subject = "Hello There";
    mailMsg.Body = "This is a test.";
    mailMsg.To.Add(new MailAddress("xxxxx@gmail.com"));
    mailMsg.ReplyToList.Add("replyhere@gmail.com");
    mailMsg.From = new MailAddress("me@gmail.com", "Alessandro");
    mailMsg.Sender = new MailAddress("somereturnpath@example.co.uk");
    SmtpClient client = new SmtpClient
    {
        Host = "localhost",
        DeliveryMethod = SmtpDeliveryMethod.Network
    };
 
    client.Send(mailMsg);
}

It is important to set the DeliveryMethode to "Network" in order to let the system to set the MAIL FROM envelope from the sender.

And here is the proof:




giovedì 4 luglio 2013

Json.Net

JSON e' diventato uno standard molto diffuso nell'ambito delle web application e sta pian piano sostituendo XML come standard per lo scambio di dati in ambito web. Per chi come me lavora con C#, trovera' molto utile la libreria Json.Net. Questa libreria permette la serializzazione e deserializzazione di oggetti JSON in maniera veloce ed intuitiva.
Come mostrato nella figura 1, le performance sono sicuramente un punto di forza di questa libreria.



Diamo ora uno sgurado piu da vicino al codice. Supponiamo di ricevere la seguente stringa e di voler estrarre i dati contenuti al suo interno:

{
    "global": {
        "TITLE": "global TITLE",
        "FIRSTNAME": "global FIRSTNAME",
        "LASTNAME": "global LASTNAME",
        "USERNAME": "global USERNAME",
        "COMPANYNAME": "global COMPANYNAME"
    }
}

Ecco il codice C#:

using Newtonsoft.Json.Linq;
namespace Test
{
      public class SavePlaceholder : Entity
     {
             public virtual IList<string> Placeholders { get; set; }
             public virtual void savePlaceholders(string json)
            {
                 this.Placeholders = new List<string>();
                 JObject jObject = JObject.Parse(json);
                 foreach (var item in jObject)
                {
                      foreach (JProperty token in item.Value)
                      {
                           var placeholder = token.Name;
                          Placeholders.Add(placeholder);
                      }
                }
           }
       }
}

Il metodo parse della classe JObject ci permette di leggere la stringa in formato JSON e serializzarla in un oggetto. Scorrendo i diversi item che formano jObject, per ogni item andiamo ad estrarre le diverse proprieta' e successivamente salviamo per ogni coppia chiave-valore soltanto la chiave. In alternativa usando token.Value avremmo potuto estrarre il valore della proprieta'. Il risultato e' la seguente lista:

TITLE
FIRSTNAME
LASTNAME
USERNAME
COMPANYNAME

Per una documentazione dettagliata e maggiori esempi vi rimando al sito ufficiale.