NewsLetter

 protected void btn_Save_Click(object sender, EventArgs e)
        {

            Attachment atcmt = null;
       
            if (FileUpload1 != null)
            {
               
                atcmt = new Attachment(FileUpload1.PostedFile.InputStream , FileUpload1.FileName);
            }
            Thread thread = new Thread(() => Common.SendNewsletter(txt_To.Text.Split(',').ToList(), txt_Subject.Text, "message", atcmt));
            thread.Start();

            txt_To.Text = "";          
            txt_Subject.Text = "";
            lblMessage.Text = "Your Message Successfully Send.";
            lblMessage.Visible = true;
        }

   #region SendNewsletter
        public static void SendNewsletter(List<string> toAddresses, string subject, string messageBody, Attachment  attachments)
        {
            if (toAddresses == null || subject.Length == 0)
                return;

            string from = (null != ConfigurationManager.AppSettings["emailSenderId"])
               ? ConfigurationManager.AppSettings["emailSenderId"].ToString() : "";
            string smtpServer = (null != ConfigurationManager.AppSettings["smtpServer"])
               ? ConfigurationManager.AppSettings["smtpServer"].ToString() : "";
            int port = (null != ConfigurationManager.AppSettings["smtpPort"])
               ? Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]) : 0;
            string userName = (null != ConfigurationManager.AppSettings["smtpUserName"])
               ? ConfigurationManager.AppSettings["smtpUserName"].ToString() : "";
            string password = (null != ConfigurationManager.AppSettings["smtpPassword"])
               ? ConfigurationManager.AppSettings["smtpPassword"].ToString() : "";

            try
            {
                MailMessage mail = new MailMessage();

                mail.From = new MailAddress(from);
                foreach (var item in toAddresses)
                {
                    mail.To.Add(item);
                }
              if (attachments != null )  mail.Attachments.Add(attachments);
                mail.Subject = subject;
                mail.Body = messageBody;
                mail.IsBodyHtml=true;
                SmtpClient SmtpServer = new SmtpClient(smtpServer);
                SmtpServer.Port = port;
                SmtpServer.Host = (null != ConfigurationManager.AppSettings["smtpHost"])
               ? ConfigurationManager.AppSettings["smtpHost"].ToString() : "";
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                //LogException("Common.cs", "SendMail", ex.Message);
            }

        }
        #endregion


Webconfig
 <appSettings>
 <add key="smtpHost" value="smtp.gmail.com" />
    <add key="smtpPort" value="587" />
   <add key="smtpUserName" value="sample.smtpmail@gmail.com" />
    <add key="smtpPassword" value="123" />
    <add key="emailSenderId" value="sample.smtpmail@gmail.com" />
    <add key="emailReceverId" value="sample.smtpmail@gmail.com" />
    <add key="smtpServer" value="sample.smtpmail@gmail.com" />
  </appSettings>

Search This Blog

Arsip Blog

Powered by Blogger.

Recent

Comment

Author Info

Like This Theme

Popular Posts

Video Of Day

jishnukanat@gmail.com

Sponsor

Most Popular