Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7501785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:39:13+00:00 2026-05-29T20:39:13+00:00

Now i fixed the filtering of file extension but i have a problem on

  • 0

Now i fixed the filtering of file extension but i have a problem on “catch” it doesn’t display any error message but good, it is not sending the email with wrong file type.

but my problem is here:
catch(Exception err)
{

            Console.WriteLine(err.Message);
            lblStatus.ForeColor = Color.Red;
            lblFile.ForeColor = Color.Red;
            lblStatus.Text = "There was an error occured while submitting your application";
            lblFile.Text = " Accepts .doc, .docx and .pdf files only!";
            return ;



protected void btnSubmit_Click(object sender, EventArgs e)
{

    if (fileUpload1.HasFile) 
        {
            var fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);

        try
            {
                string strExtension = Path.GetExtension(fileName);
                if (strExtension == ".docx" || strExtension == ".doc" || strExtension == ".pdf") 
                {

                    MailMessage myMessage = new MailMessage();
                    myMessage.To.Add(new MailAddress("someone@yahoo.com"));
                    myMessage.From = new MailAddress("me@gmail.com");
                    myMessage.Subject = txtSubject.Text;
                    myMessage.Body = "<html><body><br/><b>Sender Name:</b>&nbsp;" + txtName.Text.ToString() + "<br/><br/><b>Email:</b>&nbsp;" + txtEmail.Text.ToString() +
                                     "<br/><br/><b>Contact Number:</b>&nbsp;" + txtContact.Text.ToString() + "<br/><br/><b>Subject</b>&nbsp;" + txtSubject.Text.ToString() +
                                     "<br/><br/><b>CV Summary:</b><br/><br/>" + txtSummary.Text.ToString() + "</body></html>";
                    myMessage.IsBodyHtml = true;
                    myMessage.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));


                    SmtpClient mySmtp = new SmtpClient();
                    mySmtp.Host = "smtp.gmail.com";
                    mySmtp.Credentials = new System.Net.NetworkCredential("me@gmail.com", "mypassword");
                    mySmtp.EnableSsl = true;
                    mySmtp.Send(myMessage);

                    lblStatus.ForeColor = Color.Green;
                    lblStatus.Text = "We will contact you once you have been shortlisted to the position you are applying for. <br/> You may now close this window.";
                    txtName.Text = "";
                    txtEmail.Text = "";
                    txtSubject.Text = "";
                    txtSummary.Text = "";
                    txtContact.Text = "";
                }
            }
            catch(Exception err)
            {

                Console.WriteLine(err.Message);
                lblStatus.ForeColor = Color.Red;
                lblFile.ForeColor = Color.Red;
                lblStatus.Text = "There was an error occured while submitting your application";
                lblFile.Text = " Accepts .doc, .docx and .pdf files only!";
                return ;
            }
        }


}

protected void btnClear_Click(object sender, EventArgs e)
{
    txtName.Text = "";
    txtEmail.Text = "";
    txtSubject.Text = "";
    txtSummary.Text = "";
    txtContact.Text = "";

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:39:13+00:00Added an answer on May 29, 2026 at 8:39 pm

    Here is a nicer way to do it:

      protected void btnSubmit_Click(object sender, EventArgs e)
            {
                if (fileUpload1.HasFile) ;
                {
                    var fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
                    try
                    {
                        string strExtension = Path.GetExtension(fileName);
                        if (strExtension != ".docx" || strExtension != ".doc" || strExtension != ".pdf")
                        {
                            lblFile.ForeColor = Color.Red;
                            lblFile.Text = "You can attach .doc,.docx and pdf files only";
                            lblStatus.ForeColor = Color.Red;
                            lblStatus.Text = "There was an error occured while sending your email. Please try again later.";
                            return;
                        }
    
                        MailMessage myMessage = new MailMessage();
                        myMessage.From = new MailAddress(txtEmail.Text);
                        myMessage.To.Add(new MailAddress("EMAIL@yahoo.com"));
                        myMessage.Subject = txtSubject.Text;
                        myMessage.Body = "<html><body><br/><b>Sender Name:</b>&nbsp;" + txtName.Text.ToString() + "<br/><br/><b>Email:</b>&nbsp;" + txtEmail.Text.ToString() +
                                        "<br/><br/><b>Contact Number:</b>&nbsp;" + txtContact.Text.ToString() + "<br/><br/><b>Subject</b>&nbsp;" + txtSubject.Text.ToString() +
                                        "<br/><br/><b>CV Summary:</b><br/><br/>" + txtSummary.Text.ToString() + "</body></html>";
                        myMessage.IsBodyHtml = true;
                        myMessage.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));
    
    
                        SmtpClient mySmtp = new SmtpClient();
                        mySmtp.Host = "smtp.gmail.com";
                        mySmtp.Credentials = new System.Net.NetworkCredential("EMAIL@gmail.com", "PASSWORD");
                        mySmtp.EnableSsl = true;
                        mySmtp.Send(myMessage);
    
                        lblStatus.ForeColor = Color.Green;
                        lblStatus.Text = "Email successfully sent! We will contact you as soon as you have been shortlisted for the position you are applying for.<br/> Thank You. You can close this window now. ";
                        txtName.Text = "";
                        txtEmail.Text = "";
                        txtSubject.Text = "";
                        txtSummary.Text = "";
                        txtContact.Text = "";
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
    
            }
    

    Basically, you only want to catch exceptions i.e. IO or mail sending failures.

    This code will return if the extension is not a doc, docx or pdf and not call the send mail code.

    Also, the myMessage.From() address must be a valid email address and the one that you supplied via your NetworkCredentials()

    Here is a cut down version of the code that I have tested and verified that it is working:

        protected void Button1_Click(object sender, EventArgs e)
                {
                    if (FileUpload1.HasFile) ;
                    {
                        var fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                        try
                        {
                            string strExtension = Path.GetExtension(fileName);
                            if (strExtension != ".docx" || strExtension != ".doc" || strExtension != ".pdf")
                            {
                                lblFile.ForeColor = Color.Red;
                                lblFile.Text = "You can attach .doc,.docx and pdf files only";
                                lblStatus.ForeColor = Color.Red;
                                lblStatus.Text = "There was an error occured while sending your email. Please try again later.";
                                return;
                            }
    
                            MailMessage myMessage = new MailMessage();
                            myMessage.To.Add(new MailAddress("OtherPersonsEmail@email.xyz"));
                            myMessage.From = new MailAddress("YourEmailAddress@gmail.com");
                            myMessage.Subject = "Test";
                            myMessage.Body = "asd asd as d";
                            myMessage.IsBodyHtml = true;
                            myMessage.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, fileName));
    
    
                            SmtpClient mySmtp = new SmtpClient();
                            mySmtp.Host = "smtp.gmail.com";
                            mySmtp.Credentials = new System.Net.NetworkCredential("YourEmailAddress@gmail.com", "YourPassword");
                            mySmtp.EnableSsl = true;
                            mySmtp.Send(myMessage);
                        }
                        catch(Exception ex)
                        {
                            lblStatus.ForeColor = Color.Red;
                            lblStatus.Text = ex.Message;
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I have fixed all but two warnings now, so thank you all for
I had a problem (which is now fixed) but I've no idea why... I
THIS PROBLEM IS NOW FIXED see updated css below. I currently have: $(form#FanDetail).serialize(), function(){
PROBLEM FIXED FOR NOW. I fixed it by deleting my javascript section for a
In ruby, I'm able to do File.dirname(/home/gumby/bigproject/now_with_bugs_fixed/32/FOO_BAR_2096.results) and get /home/gumby/bigproject/now_with_bugs_fixed/32 but now I'd like
I have my current location fixed via CoreLocation. Now I want to get those
Update: I've fixed up my referencing below, and my problem is now more specific.
I'm reading the documentation now, and I have 1 thing to be fixed -
I have an int and float array of length 220 million (fixed). Now, I
I have an int and float array each of length 220 million (fixed). Now,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.