Email campaign save databse

  protected void btn_Save_Click1(object sender, EventArgs e)
        {
            String fileName ="";
            Attachment atcmt = null;
            if (FileUpload1.HasFile)
            {
                fileName = SaveFile(FileUpload1.PostedFile);
                atcmt = new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName);
            }

            List<String> ToAddresses = txt_To.Text.Trim().Split(',').ToList();
            String Subject = txt_Subject.Text.Trim();
            String Content = txt_Content.Text.Trim();
            DateTime CreatedDates=DateTime.Now;

            AddEmailCampaign(ToAddresses, Subject, Content, fileName, CreatedDates);

            Thread thread = new Thread(() => Common.SendEmailCampaign(ToAddresses, Subject, Content, atcmt));
            thread.Start();
         
            txt_To.Text = "";
            txt_Content.Text = "";
            txt_Subject.Text = "";
            lblMessage.Text = "Your Message Successfully Send.";
            lblMessage.Visible = true;

        }

        #region Add_Email_Campaign

        void AddEmailCampaign(List<string> ToAddresses, string Subject, string Content, string filename, DateTime CreatedDates)
        {
                   
            CRMEntities dc = new CRMEntities();

            tbl_EmailCampaign tbl = new tbl_EmailCampaign();
            //What is the LINQ way to join a string array
            tbl.Toaddress = String.Join(",", ToAddresses.ToArray()); // To Split The Array
            tbl.Subject = Subject;
            tbl.Contents = Content;
            tbl.Attachments = filename;
            tbl.CreatedDate = CreatedDates;
            dc.tbl_EmailCampaign.AddObject(tbl);
            dc.SaveChanges();

                     
        }
        #endregion
        #region Attachmentsave

        String  SaveFile(HttpPostedFile file)
        {
            // Specify the path to save the uploaded file to.
            string savePath = "Attachments";

            // Get the name of the file to upload.
            string fileName =   DateTime.Now.Ticks+ FileUpload1.FileName;

            // Create the path and file name to check for duplicates.
            string pathToCheck = savePath+ "//" + fileName;

            // Create a temporary file name to use for checking duplicates.
            string tempfileName = "";

            // Check to see if a file already exists with the
            // same name as the file to upload.

            if (System.IO.File.Exists(pathToCheck))
            {
                int counter = 2;
                while (System.IO.File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempfileName = counter.ToString() + fileName;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }

                fileName = tempfileName;

                // Notify the user that the file name was changed.
                //UploadStatusLabel.Text = "A file with the same name already exists." +
                //    "<br />Your file was saved as " + fileName;
            }
            else
            {
                // Notify the user that the file was saved successfully.
                UploadStatusLabel.Text = " ";
            }

            // Append the name of the file to upload to the path.
            savePath +="//"+ fileName;
            savePath = Server.MapPath(savePath);
            // Call the SaveAs method to save the uploaded
            // file to the specified directory.
            FileUpload1.SaveAs(savePath);
            return fileName;
        }

        #endregion

   #region SendEmailCampaign

         public static void SendEmailCampaign(List<string> toAddresses, string subject, string messageBody,Attachment  attachments)
        {
            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