Wednesday, 13 November 2013

SEND EMAILS FROM YOUR APPLICATIONS

 SEND EMAIL FROM YOUR APPLICATION

protected void BtnSave_Click(object sender, EventArgs e)
    {
        string attachment = string.Empty;
        string emailList = string.Empty;
           
        foreach (GridViewRow curRow in GdvEmail.Rows)
        {
            CheckBox chkSelect = (CheckBox)curRow.FindControl("chkRow");
            if (chkSelect.Checked)
            {
                if (emailList != "")
                {
                    emailList = emailList + ";" + curRow.Cells[1].Text;
                }
                else
                {
                    emailList = curRow.Cells[1].Text;
                }
            }
        }
        if (emailList != "")
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new NetworkCredential("rajendar1210@gmail.com", "01234@56789");
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            MailMessage message = new MailMessage();
            message.From = new MailAddress("rajendar1210@gmail.com");
            if (emailList.Contains(";"))
            {
                string[] _EmailsTO = emailList.Split(";".ToCharArray());
                for (int i = 0; i < _EmailsTO.Length; i++)
                {
                    message.To.Add(new MailAddress(_EmailsTO[i]));
                }
            }
            else
            {
                if (!emailList.Equals(string.Empty))
                {
                    message.To.Add(new MailAddress(emailList));
                }
            }
            if (FileUploadEmail.PostedFile != null)
            {

                HttpPostedFile attFile = FileUploadEmail.PostedFile;

                int attachFileLength = attFile.ContentLength;

                if (attachFileLength > 0)
                {

                    string FileNameToAttache = Path.GetFileName(FileUploadEmail.PostedFile.FileName);
                    FileNameToAttache = "attachments/" + FileNameToAttache;
                    FileUploadEmail.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));

                    message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
                    attachment = FileNameToAttache;

                }

            }
            message.Subject = txtSubject.Text.Trim();
            message.Body = TxtMessage.Text.Trim();
            smtp.Timeout = 9999999;
            smtp.Send(message);
            if (File.Exists(attachment))
            {

                if (IsFolderReadOnly(attachment))
                {
                    System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(attachment);
                    oDir.Attributes = oDir.Attributes & ~System.IO.FileAttributes.ReadOnly;
                }

                File.Delete(attachment);//Error is thrown from here

            }
            attachment = null;

            Page.RegisterStartupScript("Sathyam Computer Education", "<script>alert('Mail sent thank you...');if(alert){ window.location='EmailNotification.aspx';}</script>");
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert(' Select the email in the grid ')", true);
        }
    }

No comments:

Post a Comment