Tuesday, 28 March 2017

Mail send in C#

=============================================

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;

namespace ARC.Utility
{
    public class MailSend
    {
        static string displayName = System.Configuration.ConfigurationManager.AppSettings["DisplayName"];

        #region send mail without attchment
        /// <summary> send mail without attchment
        /// <para>EmailMessageID</para> pass to emailMessage id
        /// <para>MailSubject</para> pass mail subject name
        /// <para>MailBody</para> pass Mail decription
        /// <para>cc</para> pass cc mail id
        /// <para>bc</para> pass bc mail id
        /// <para>FromMail</para> pass Network Credential from emailMessage id (user name)
        /// <para>password</para> pass Network Credential Password (user password)
        /// <para>host</para> pass Network Credential host for send mailMessage
        /// <para>port</para> pass Network Credential port for send mailMessage
        /// <DevelopedBy>Neelkamal bansal</DevelopedBy>
        /// </summary>
        public static void SendMail(string emailMessageId, string mailMessageSubject, string mailMessageBody, string cC, string bC, string fromMail, string password, string host, int port)
        {
            try
            {
                string username = Convert.ToString(ConfigurationManager.AppSettings["Username"]);
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add(emailMessageId);

                if (!string.IsNullOrEmpty(cC))
                {
                    mailMessage.CC.Add(cC);
                }

                if (!string.IsNullOrEmpty(bC))
                {
                    mailMessage.Bcc.Add(bC);
                }

                mailMessage.From = new MailAddress(fromMail, displayName);
                mailMessage.IsBodyHtml = true;
                mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
                mailMessage.Subject = mailMessageSubject;                          
                mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mailMessage.Body = mailMessageBody;
         

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Port = port;
                smtpClient.Host = host;
                smtpClient.EnableSsl = false;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(username, password);
                smtpClient.Send(mailMessage);
                smtpClient.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void ForgotPassSendMail(string emailMessageId, string mailMessageSubject, string mailMessageBody, string cC, string bC, string fromMail, string password, string host, int port)
        {
            try
            {
                string username = Convert.ToString(ConfigurationManager.AppSettings["FUsername"]);
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add(emailMessageId);

                if (!string.IsNullOrEmpty(cC))
                {
                    mailMessage.CC.Add(cC);
                }

                if (!string.IsNullOrEmpty(bC))
                {
                    mailMessage.Bcc.Add(bC);
                }

                mailMessage.From = new MailAddress(fromMail, displayName);
                mailMessage.Subject = mailMessageSubject;
                mailMessage.IsBodyHtml = true;
                mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mailMessage.Body = mailMessageBody;
                mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Port = port;
                smtpClient.Host = host;
                smtpClient.EnableSsl = false;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(username, password);
                smtpClient.Send(mailMessage);
                smtpClient.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region send mail with single attchment
        /// <summary> send mailMessage with single attchment
        /// <para>EmailMessageID</para> pass to emailMessage id
        /// <para>MailSubject</para> pass mailMessage subject name
        /// <para>MailBody</para> pass Mail decription
        /// <para>cc</para> pass cc mailMessage id
        /// <para>bc</para> pass bc mailMessage id
        /// <para>FromMail</para> pass Network Credential from emailMessage id (user name)
        /// <para>password</para> pass Network Credential Password (user password)
        /// <para>host</para> pass Network Credential host for send mailMessage
        /// <para>port</para> pass Network Credential port for send mailMessage
        /// <para>attachmentfile</para> pass attchment (ex- doc, image, pdf and any other)
        /// <DevelopedBy>Neelkamal bansal</DevelopedBy>
        /// </summary>
        public static void SendMail(string emailMessageId, string mailMessageSubject, string mailMessageBody, string cC, string bC, string fromMail, string password, string host, int port, byte[] attachmentFile, string filename)
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                string username = Convert.ToString(ConfigurationManager.AppSettings["Username"]);
                mailMessage.To.Add(emailMessageId);

                if (!string.IsNullOrEmpty(cC))
                {
                    mailMessage.CC.Add(cC);
                }

                if (!string.IsNullOrEmpty(bC))
                {
                    mailMessage.Bcc.Add(bC);
                }

                mailMessage.From = new MailAddress(fromMail, displayName);
                mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
                mailMessage.Subject = mailMessageSubject;
                mailMessage.IsBodyHtml = true;
                mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mailMessage.Body = mailMessageBody;

                // Attachment attachment = new Attachment(attachmentFile, MediaTypeNames.Application.Octet);
                Attachment attachment = new Attachment(new MemoryStream(attachmentFile), filename);
                mailMessage.Attachments.Add(attachment);
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Port = port;
                smtpClient.Host = host;
                smtpClient.EnableSsl = false;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;
              //  smtpClient.ServicePoint.MaxIdleTime = 1;
                smtpClient.Credentials = new NetworkCredential(username, password);
                smtpClient.Send(mailMessage);
                smtpClient.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region send mail with multiple attchment
        /// <summary> send mailMessage with multiple attchment
        /// <para>EmailMessageID</para> pass to emailMessage id
        /// <para>MailSubject</para> pass mailMessage subject name
        /// <para>MailBody</para> pass Mail decription
        /// <para>cc</para> pass cc mailMessage id
        /// <para>bc</para> pass bc mailMessage id
        /// <para>FromMail</para> pass Network Credential from emailMessage id (user name)
        /// <para>password</para> pass Network Credential Password (user password)
        /// <para>host</para> pass Network Credential host for send mailMessage
        /// <para>port</para> pass Network Credential port for send mailMessage
        /// <para>attachmentfile</para> pass multiple attchment in array (ex- doc, image, pdf and any other)
        /// <DevelopedBy>Neelkamal bansal</DevelopedBy>
        /// </summary>
        public static void SendMail(string emailMessageIDd, string mailMessageSubject, string mailMessageBody, string cC, string bC, string fromMail, string password, string host, int port, string[] attachmentFile)
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add(emailMessageIDd);

                if (!string.IsNullOrEmpty(cC))
                {
                    mailMessage.CC.Add(cC);
                }

                if (!string.IsNullOrEmpty(bC))
                {
                    mailMessage.Bcc.Add(bC);
                }

                mailMessage.From = new MailAddress(fromMail, displayName);
                mailMessage.Subject = mailMessageSubject;
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = mailMessageBody;

                if (attachmentFile.Length > 0)
                {
                    for (int i = 0; i < attachmentFile.Length; i++)
                    {
                        Attachment attachment = new Attachment(attachmentFile[i], MediaTypeNames.Application.Octet);
                        ContentDisposition disposition = attachment.ContentDisposition;
                        disposition.CreationDate = File.GetCreationTime(attachmentFile[i]);
                        disposition.ModificationDate = File.GetLastWriteTime(attachmentFile[i]);
                        disposition.ReadDate = File.GetLastAccessTime(attachmentFile[i]);
                        disposition.FileName = Path.GetFileName(attachmentFile[i]);
                        disposition.Size = new FileInfo(attachmentFile[i]).Length;
                        disposition.DispositionType = DispositionTypeNames.Attachment;
                        mailMessage.Attachments.Add(attachment);
                    }
                }

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Port = port;
                smtpClient.Host = host;
                smtpClient.EnableSsl = false;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(fromMail, password);
                smtpClient.Send(mailMessage);
                smtpClient.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public static void sendmailnew(string frommail ,string tomail)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(frommail, "Gaurav");
            mailMessage.To.Add(tomail);
            mailMessage.Subject = "This is test mail.";
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = "This is test mail.";

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Send(mailMessage);
            smtpClient.Dispose();
        }
    }
}

=====================================

Webconfig
==================================

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="mail.seologistics.com" enableSsl="false" port="25" userName="test_flexsin@seologistics.com" password="Ybv0B'mJKLIU*%$"/>
      </smtp>
    </mailSettings>
  </system.net>


key:

 <add key="DisplayName" value="تحدي القراءة العربي"/>
    <add key="EmailUname" value="test_flexsin@seologistics.com"/>
    <add key="Username" value="test_flexsin@seologistics.com"/>
    <add key="EmailPassword" value="Ybv0B'mJKLIU*%$"/>
    <add key="Host" value="mail.seologistics.com"/>
    <add key="Port" value="25"/>
    <add key="CC" value=""/>
    <add key="BC" value="neelkamal_bansal@seologistics.com"/>
    <add key="FDisplayName" value="Arab Reading Challenge"/>
    <add key="FEmailUname" value="test_flexsin@seologistics.com"/>
    <add key="FUsername" value="test_flexsin@seologistics.com"/>
    <add key="FEmailPassword" value="Ybv0B'mJKLIU*%$"/>
    <add key="FHost" value="mail.seologistics.com"/>
    <add key="FPort" value="25"/>
    <add key="FCC" value=""/>
    <add key="FBC" value="avdhesh_kumar12@seologistics.com"/>

No comments:

Post a Comment