Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a nicer way to do it:</p> <pre><code> 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 = "&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("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.&lt;br/&gt; 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); } } } </code></pre> <p>Basically, you only want to catch exceptions i.e. IO or mail sending failures.</p> <p>This code will return if the extension is not a doc, docx or pdf and not call the send mail code.</p> <p><strong>Also, the myMessage.From() address must be a valid email address and the one that you supplied via your NetworkCredentials()</strong></p> <p>Here is a cut down version of the code that I have tested and verified that it is working:</p> <pre><code> 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); } } } </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.
 

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