Sebelum memulai, ada baiknya kita nikmati secangkir kopi hitam dan sambil mengamati promo Honda terbaru dari salah satu dealer Mobil Honda yang berlokasi di Harapan Indah.

Saat ini, email merupakan satu hal yang umum, hampir semua orang memiliki email, karena berbagai platform akan terhubung melalui email sebagai alat komunikasi dan verifikasi.

Namun apakah Anda tahu Bagaimana cara sebuah aplikasi mengirimkan Email tersebut ? Bagi seorang Programmer sudah barang tentu hal ini menjadi salah satu kemampuan yang harus dimiliki, yaitu bagaimana cara Aplikasi nya mampu mengirim email.

Pengertian Email

Email adalah singkatan dari Elektronik Mail atau dalam bahasa Indonesia disebut Surat Elektronik merupakan sarana dalam mengirim surat yang dilakukan melalui media internet. Media internet yang dimaksud bisa melalui komputer atau handphone yang memiliki akses internet. Pada umumnya mengirim surat biasa akan di kenakan biaya ( Membeli Perangko ) namun pada Surat Elektronik hanya dibutuhkan sambungan internet.

Fungsi Email

Email adalah alamat resmi kita di dunia maya. Dengan menggunakan email kita dapat terhubung dalam berbagai kegiatan internet, seperti bergabung di situs jejaring sosial semisal Facebook dan twitter maka saat pendaftaran akan membutuhkan email. Saat ini, Email telah berkembang hingga memiliki fungsi yang sangat beragam. Mengirim file, foto, video dan Chat dengan teman dan keluarga menjadi lebih muda

Aplikasi CRM

Untuk mengirim Email menggunakan C# (csharp) kita dapat membuat sebuah aplikasi kecil yang bertujuan sebagai library.

Dengan membuat library sederhana, kita hanya cukup memanggilnya dengan beberapa baris perintah seperti berikut:

				
					 internal class Program
    {
        static void Main(string[] args)
        {
            MailRecipient recipient = new MailRecipient();
            recipient.name = "Suhendra Yohana Putra";
            recipient.address = "suhendra.xxx@gmail.com";

            Mailer mailer = new Mailer(recipient);
            mailer.subject = "Email Tester";
            mailer.content = $"<h3>Hello admin</h3>";
            string result = mailer.sendEmail();
            Console.WriteLine(result);
        }
    }
				
			
Cloud Hosting Indonesia

Berikut ini adalah sebuah class library untuk menangani pengiriman email:

				
					public class Mailer
    {
        private MailSetup mailSetup;
        private List<MailRecipients> recipients;
        private MailRecipient recipient;
        private string _emailContent;
        private string _emailSubject;
        public string content { get { return _emailContent; } set { _emailContent = value; } }
        public string subject { get { return _emailSubject; } set { _emailSubject = value; } }

        public Mailer(MailSetup _mailSetup, List<MailRecipients> _recipients, MailRecipient _recipient)
        {
            this.mailSetup = _mailSetup;
            this.recipients = _recipients;
            this.recipient = _recipient;
        }

        public Mailer(MailSetup _mailSetup, List<MailRecipients> _recipients)
        {
            this.mailSetup = _mailSetup;
            this.recipients = _recipients;
            this.recipient = new MailRecipient();
        }

        public Mailer(MailSetup _mailSetup, MailRecipient _recipient)
        {
            this.mailSetup = _mailSetup;
            this.recipients = new List<MailRecipients>();
            this.recipient = _recipient;
        }

        public Mailer(MailRecipient _recipient)
        {
            this.mailSetup = new MailSetup();
            this.recipients = new List<MailRecipients>();
            this.recipient = _recipient;
        }

        public Mailer(List<MailRecipients> _recipients)
        {
            this.mailSetup = new MailSetup();
            this.recipients = _recipients;
            this.recipient = new MailRecipient();
        }


        public string sendEmail()
        {
            try
            {
                MailMessage message = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                message.From = new MailAddress(this.mailSetup.username, this.mailSetup.name);
                if (this.recipients.Count > 0)
                {
                    for (int idxDestination = 0; idxDestination < this.recipients.Count; idxDestination++)
                    {
                        message.To.Add(new MailAddress(this.recipients[idxDestination].address, this.recipients[idxDestination].name));
                    }
                }
                else
                {
                    message.To.Add(new MailAddress(this.recipient.address, this.recipient.name));
                }

                message.Subject = this.subject;
                message.IsBodyHtml = true; //to make message body as html  
                message.Body = this.content;
                smtp.Port = this.mailSetup.Port;
                smtp.Host = this.mailSetup.Host;
                smtp.EnableSsl = this.mailSetup.EnableSSL;
                smtp.UseDefaultCredentials = this.mailSetup.useDefaultCredentials;
                smtp.Credentials = new NetworkCredential(this.mailSetup.username, this.mailSetup.password);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Send(message);

                MailResponse response = null;
                if (this.recipients.Count > 0)
                {
                    response = generateResponse(this.recipients, "success", "Please check email");
                }
                else
                {
                    response = generateResponse(this.recipient, "success", "Please check email");
                }
                string result = Newtonsoft.Json.JsonConvert.SerializeObject(response);
                return result;
            }
            catch (Exception e)
            {
                MailResponse response = null;
                if (this.recipients.Count > 0)
                {
                    response = generateResponse(this.recipients, "fail", e.Message);
                }
                else
                {
                    response = generateResponse(this.recipient, "fail", e.Message);
                }
                string result = Newtonsoft.Json.JsonConvert.SerializeObject(response);
                return result;
            }


        }

        private MailResponse generateResponse(List<MailRecipients> recipients, string _result, string _description)
        {
            MailResponse response = new MailResponse();

            response.result = _result;

            if (recipients.Count > 0)
            {
                List<string> tmpDestination = new List<string>();
                for (int idxDestination = 0; idxDestination < recipients.Count; idxDestination++)
                {
                    tmpDestination.Add($"{recipients[idxDestination].name} <{recipients[idxDestination].address}>");
                }
                response.destination = string.Join(", ", tmpDestination);
                response.description = $"{_description}";
            }
            else
            {
                response.destination = "Destination not found";
                response.description = $"1. No destination was found, 2.{_description}";
            }

            response.time = DateTime.Now;


            return response;
        }

        /// <summary>
        /// Generate email response
        /// </summary>
        /// <param name="recipient"></param>
        /// <param name="_result"></param>
        /// <param name="_description"></param>
        /// <returns></returns>
        private MailResponse generateResponse(MailRecipient recipient, string _result, string _description)
        {
            MailResponse response = new MailResponse();

            if (recipient == null || string.IsNullOrEmpty(recipient.name))
            {
                response.destination = "Destination not found";
                response.description = $"1. No destination was found, 2.{_description}";
            }
            else
            {
                response.destination = $"{recipient.name} <{recipient.address}>";
                response.description = $"{_description}";
            }

            response.time = DateTime.Now;
            response.result = "fail";

            return response;
        }

        /// <summary>
        /// Email response
        /// </summary>
        public class MailResponse
        {
            /// <summary>
            /// Email Destination
            /// </summary>
            public string destination { get; set; }
            public DateTime time { get; set; }
            public string result { get; set; }
            public string description { get; set; }
        }

        /// <summary>
        /// Email server setup
        /// </summary>
        public class MailSetup
        {


            /// <summary>
            /// Email from address
            /// </summary>
            public string username { get; set; } = "alamat_pengirim";
            /// <summary>
            /// Email from name
            /// </summary>
            public string name { get; set; } = "MAILER_NOTIFICATION";
            /// <summary>
            /// Email password
            /// </summary>
            public string password { get; set; } = "password_email";
            /// <summary>
            /// SMTP Host
            /// </summary>
            public string Host { get; set; } = "smtp.gmail.com";
            /// <summary>
            /// Server port
            /// </summary>
            public int Port { get; set; } = 587;
            /// <summary>
            /// Use SSL
            /// </summary>
            public bool EnableSSL { get; set; } = true;
            /// <summary>
            /// Use default creadential
            /// </summary>
            public bool useDefaultCredentials { get; set; } = false;

        }

        public class MailRecipients
        {
            public string name { get; set; }
            public string address { get; set; }
        }
        public class MailRecipient
        {
            public string name { get; set; }
            public string address { get; set; }
        }

    }
				
			

Pada saat aplikasi dijalankan, makan akan mendapatkan hasil sebagai berikut:

Kirim Email - Konsultan IT Bandung - Suhendra Yohana Putra
Email - Konsultan IT Bandung - Suhendra Yohana Putra