title: How to send mail with attachment in asp.net
body: <blockquote>
    <p>THIS IS MY ATTEMPT I AM GETTING AN ERROR:</p>
    
    <p>My SendMail.aspx</p>
  </blockquote>
  
  
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;table style="border: 1px solid" align="center"&gt;
              &lt;tr&gt;
                  &lt;td colspan="2" align="center"&gt;
                      &lt;b&gt;Send Mail with Attachment using asp.net&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      From:
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtFrom" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      To:
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtTo" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      Subject:
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtSubject" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      Attach a file:
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:FileUpload ID="fileUpload1" runat="server" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td valign="top"&gt;
                      Body:
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <blockquote>
    <p>My SendMail.aspx.cs</p>
  </blockquote>
  
  <pre><code>using System.Net.Mail;
  using System.Net;
  
  protected void btnSubmit_Click(object sender, EventArgs e)
          {
              MailMessage mail = new MailMessage();
              mail.To.Add(txtTo.Text);
              //mail.To.Add("amit_jain_online@yahoo.com");
              mail.From = new MailAddress(txtFrom.Text);
              mail.Subject = txtSubject.Text;
              mail.Body = txtBody.Text;
              mail.IsBodyHtml = true;
  
              //Attach file using FileUpload Control and put the file in memory stream
              if (fileUpload1.HasFile)
              {
                  mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
              }
              SmtpClient smtp = new SmtpClient();
               //Or Your SMTP Server Address
  
              NetworkCredential nc = new NetworkCredential();
              nc.UserName="myusername@gmail.com";
              nc.Password="mypassword";
              smtp.Credentials = nc;
              smtp.Port = 587;
              smtp.Host = "smtp.gmail.com";
              smtp.UseDefaultCredentials = false;
              //Or your Smtp Email ID and Password
              smtp.EnableSsl = true;
              smtp.Send(mail);
          }
  </code></pre>
  
  <blockquote>
    <p>I am getting this Error dont know what to do:</p>
    
    <p>The SMTP server requires a secure connection or the client was not authenticated. The >server response was: 5.5.1 Authentication Required.
    Learn more at</p>
  </blockquote>