? QA Design Gurus: Supported Protocols for Sending and Receiving from Email Server

Aug 1, 2015

Supported Protocols for Sending and Receiving from Email Server

We come across situations which protocols supports sending and receiving Emails from Email Server.
There are 3 separate protocols that are all related to email in general sense. One is strictly for sending Emails and two are for receiving emails from Email Server.
SMTP: SIMPLE MAIL TRANSFER PROTOCAL
POP:    POST OFFICE PROTOCOL
IMAP:INTERNET MESSAGE ACCESS PROTOCAL

SMTP Allows you to send out a message to Internet server from there route to its final destination.

POP and IMAP protocols are used to grab messages from Email Server. There are some pros and cons between these two email server.

POP is excellent if all you want to do is pull down mail from the server to a single client. This could be a single desktop computer (perhaps using Outlook or Thunderbird or any other desktop e-mail client).  If you don’t access e-mail from anywhere else, POP3 is fantastic because it’s quick, very simple to set up and it works with everything.

IMAP is excellent if all you want to do is pull down mail from the server to two or more applications. If you have a blackberry or an iPhone or several computers or all of the above and you want to be able to read the same messages from each e-mail client, then IMAP is the system for you.

Difference between POP and IMAP

POP
IMAP
To Single Client
To Multiple clients
once a message is marked as read on one client, that message gets marked as read with all of the clients.  This eliminates the need to “clean up” several inboxes every time new messages come in.
 With POP3, you would have to read the message in both locations to take the “read” flag off the e-mail.


Below are the examples you can find for SMTP ,POP3 and IMAP Clients in C#

SMTP:
using System.Net.Mail;
SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress(FromAddress);
            smtpClient.Host = Host;
            smtpClient.Port = Port;
            message.From = fromAddress;
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.Body = content;
           

            smtpClient.Send(message);


IMAP:

MailServer oServer = new MailServer("Imap Server”,
                            "User", "Password", sslConnection(true),ServerAuthType.AuthLogin,ServerProtocol.Imap4);
                EAGetMail.MailClient oClient = new EAGetMail.MailClient("TryIt");
               try
                {
                    oClient.Connect(oServer);
                    oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly;
                    //oClient.GetMailInfosParam.DateRange.SINCE = System.DateTime.Now;
                    MailInfo[] infos = oClient.GetMailInfos();
                    Console.WriteLine("Email count is" + infos.Length);

                    for (int i = 0; i < infos.Length; i++)
                    {
                        MailInfo info = infos[i];
                        Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
                            info.Index, info.Size, info.UIDL);

                        // Receive email from POP3 server
                        Mail oMail = oClient.GetMail(info);
                        
                        Console.WriteLine("From: {0}", oMail.From.ToString());
                        Console.WriteLine("Subject: {0}\r\n", oMail.Subject);
                        Attachment[] at = oMail.Attachments;
                        Attachment at1 = at[0];
                        at1.SaveAs("temp.csv", true);
                        // Generate an email file name based on date time.
                        System.DateTime d = System.DateTime.Now;
                        System.Globalization.CultureInfo cur = new
                            System.Globalization.CultureInfo("en-US");
                        string sdate = d.ToString("yyyyMMddHHmmss", cur);
                        string fileName = String.Format("{0}\\{1}{2}{3}.eml",
                            mailbox, sdate, d.Millisecond.ToString("d3"), i);

                        // Save email to local disk
                        oMail.SaveAs(fileName, true);

                        // Mark email as deleted from POP3 server.
                        //oClient.Delete(info);
                    }

                    // Quit and pure emails marked as deleted from POP3 server.
                    oClient.Quit();
                }
                catch (Exception ep)
                {
                    Console.WriteLine(ep.Message);
                }

            }


POP:

MailServer oServer = new MailServer("Pop3 Server”,
                            "User", "Password", sslConnection(true),ServerAuthType.AuthLogin,ServerProtocol.Pop3);
                EAGetMail.MailClient oClient = new EAGetMail.MailClient("TryIt");
               try
                {
                    oClient.Connect(oServer);
                    oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly;
                    //oClient.GetMailInfosParam.DateRange.SINCE = System.DateTime.Now;
                    MailInfo[] infos = oClient.GetMailInfos();
                    Console.WriteLine("Email count is" + infos.Length);

                    for (int i = 0; i < infos.Length; i++)
                    {
                        MailInfo info = infos[i];
                        Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
                            info.Index, info.Size, info.UIDL);

                        // Receive email from POP3 server
                        Mail oMail = oClient.GetMail(info);
                        
                        Console.WriteLine("From: {0}", oMail.From.ToString());
                        Console.WriteLine("Subject: {0}\r\n", oMail.Subject);
                        Attachment[] at = oMail.Attachments;
                        Attachment at1 = at[0];
                        at1.SaveAs("temp.csv", true);
                        // Generate an email file name based on date time.
                        System.DateTime d = System.DateTime.Now;
                        System.Globalization.CultureInfo cur = new
                            System.Globalization.CultureInfo("en-US");
                        string sdate = d.ToString("yyyyMMddHHmmss", cur);
                        string fileName = String.Format("{0}\\{1}{2}{3}.eml",
                            mailbox, sdate, d.Millisecond.ToString("d3"), i);

                        // Save email to local disk
                        oMail.SaveAs(fileName, true);

                        // Mark email as deleted from POP3 server.
                        //oClient.Delete(info);
                    }

                    // Quit and pure emails marked as deleted from POP3 server.
                    oClient.Quit();
                }
                catch (Exception ep)
                {
                    Console.WriteLine(ep.Message);
                }

            }




No comments:

Post a Comment