Note that there are some explanatory texts on larger screens.

plurals
  1. POSending Email with Attachment that accepts particular file type in ASP.NET C#
    primarykey
    data
    text
    <p>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.</p> <p>but my problem is here: catch(Exception err) {</p> <pre><code> 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 = "&lt;html&gt;&lt;body&gt;&lt;br/&gt;&lt;b&gt;Sender Name:&lt;/b&gt;&amp;nbsp;" + txtName.Text.ToString() + "&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Email:&lt;/b&gt;&amp;nbsp;" + txtEmail.Text.ToString() + "&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Contact Number:&lt;/b&gt;&amp;nbsp;" + txtContact.Text.ToString() + "&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Subject&lt;/b&gt;&amp;nbsp;" + txtSubject.Text.ToString() + "&lt;br/&gt;&lt;br/&gt;&lt;b&gt;CV Summary:&lt;/b&gt;&lt;br/&gt;&lt;br/&gt;" + txtSummary.Text.ToString() + "&lt;/body&gt;&lt;/html&gt;"; 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. &lt;br/&gt; 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 = ""; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload