2

My code for sending email was working correctly the psat two weeks. But now it shows an error like

Failure Sending Mail.

I don't know why this problem has raise. Some new new firewall policies were placed last week, is it because of this the problem is raised? What should I do?

Here is my code:

 protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (TextBox1.Text == "")
            {

                string alertmessage = "";
                alertmessage = "Email ID. cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox1.Focus();
            }
            else if (TextBox2.Text == "")
            {

                string alertmessage = "";
                alertmessage = "CC To cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox2.Focus();
            }
            else if (TextBox3.Text == "")
            {

                string alertmessage = "";
                alertmessage = "Subject cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox3.Focus();
            }
            else if (TextBox4.Text == "")
            {

                string alertmessage = "";
                alertmessage = "Message Body cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox4.Focus();
            }
            //else if (upSignature.FileName == "")
            //{
            //    ctr = 1;
            //    string alertmessage = "";
            //    alertmessage = "Attachment  Missing...";
            //    this.CreateMessageAlert(this, alertmessage, "alertKey");
            //    upSignature.Focus();
            //}

            else
            {


                string photo = "Enquiry" + Session["MRNO"].ToString() + FileUpload1.FileName;
                string strpath = Request.MapPath("~/");
                FileUpload1.SaveAs(strpath + "/Enquiry/" + photo);


                try
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add(new MailAddress(TextBox1.Text.Trim()));
                    mail.From = new MailAddress("XXXXXXX");
                    mail.Subject = "Enquiry for MRNO " + " " + " " + Session["MRNO"].ToString() + " " + " " + "Reg.";
                    mail.CC.Add(TextBox2.Text.Trim());
                    mail.Body = TextBox4.Text.Trim();

                        mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
                        mail.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));

                    //Attachment attach = new Attachment(strpath + "/Enquiry/" + photo);
                    //mail.Attachments.Add(attach);


                    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);

                    smtp.EnableSsl = true;

                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("XXXXXX", "XXXXXXX");

                    smtp.Send(mail);

                    string alertmessage = "";
                    alertmessage = "Mail Has Been Sent";
                    this.CreateMessageAlert(this, alertmessage, "alertKey");


                    //   Page.RegisterStartupScript("close", "<script language=javascript>self.close();</script>");

                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
        catch (Exception ex1)
        {
            Response.Write(ex1.Message);
        }

    }
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
aravind annu
  • 23
  • 1
  • 3
  • 3
    I'll bet that it is the firewall that is causing the problem. You need to open up port 587 on the firewall. – Sani Huttunen Aug 27 '12 at 05:59
  • @Sani Huttunen: Can you tell me how to open up port 587 on the firewall friend – aravind annu Aug 27 '12 at 06:01
  • Did you debug the code where exactly the exception raised? – SMK Aug 27 '12 at 06:01
  • That depends on the firewall itself. Since I don't know which firewall it is then I can't help you. – Sani Huttunen Aug 27 '12 at 06:02
  • @Shoaib Muhammad Khan: s i did, but all the things were working fine in my code. But the exception also araised. That is what my problem friend.. Thats y am here for help – aravind annu Aug 27 '12 at 06:04
  • @ Sani Huttunen : i thought that was "Fortigaurd" – aravind annu Aug 27 '12 at 06:06
  • 5
    Your network admin should be able to help you with allowing port traffic across the firewall - if that is indeed the issue. Also if you post the exception details.. that'd help in diagnosing the issue. – Gishu Aug 27 '12 at 06:07

1 Answers1

1

You need to open Port 587 for sending mails. Check with your network team regarding this and make sure it is opened

Peru
  • 2,871
  • 5
  • 37
  • 66
  • But yahoo and gmail mail's can send and recieve the mails, my application only cant able to send. since i have to check the port?? – aravind annu Aug 27 '12 at 06:14
  • Check using http://stackoverflow.com/questions/273159/how-to-easy-determine-if-a-port-is-open-on-an-windows-server to see ports are opened – Peru Aug 27 '12 at 06:17
  • Also change the settings as followed in this link and see if Gmail works with your SMTP Server http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server Which will give you a Clear idea on where the issue is :) – Peru Aug 27 '12 at 06:21