title: Attaching Binary Image in Email through ASP.NET
body: <p>I am trying to retrieve a QR code image path that is in binary form from a SQL database, then insert that as a full image into the email using <code>MailMessage</code> and <code>SmtpClient</code>.</p>
  <p>I understand that we would have to convert the <code>base 64</code> string into an image file, so that it can be visualized.
  But I am currently having no clues as to how to do this. Is there any simple explanation or coding examples that I can refer to?</p>
  <p>Below is my code for retrieving the QR code image path from the SQL database</p>
  <pre><code> //retrieve QR Code image 
  string getQRimage = &quot;SELECT QRCode FROM Parcel WHERE ConsignmentNum =@num&quot;;
  SqlCommand cmdGetQR = new SqlCommand(getQRimage, conn);
  cmdGetQR.Parameters.AddWithValue(&quot;@num&quot;, txtConsignmentNum.Text);
  
  SqlDataReader dr1 = cmdGetQR.ExecuteReader();
  bool recordFound1 = dr1.Read();
  if (recordFound1)
  {
      byte[] bytes = (byte[])cmdGetQR.ExecuteScalar();
      string strBase64 = Convert.ToBase64String(bytes);
      Image1.ImageUrl = &quot;data:Image/png;base64,&quot; + strBase64;
  }
  </code></pre>
  <p>What I am trying to do is to send <code>Image1</code> in the email body. The coding for the email is as below:</p>
  <pre><code>//send email 
  try
  {
      //retrieve student email 
      if (conn.State == System.Data.ConnectionState.Closed)
      {
          conn.Open();
  
          string getEmail = &quot;SELECT Email FROM Student WHERE StudentID='&quot; + txtStudentID.Text + &quot;'&quot;; 
          SqlCommand cmdGetMail = new SqlCommand(getEmail, conn);
          cmdGetMail.Parameters.AddWithValue(&quot;@StudentID&quot;, txtStudentID.Text);
  
  
          SqlDataReader dr = cmdGetMail.ExecuteReader();
          bool recordFound = dr.Read();
          if (recordFound)
          {
              lblStudentEmail.Text = dr[&quot;Email&quot;].ToString();
              lblStudentName.Text = dr[&quot;StudentName&quot;].ToString();
          }
      }
  
      MailMessage mailMessage = new MailMessage();
      mailMessage.From = new MailAddress(&quot;contact.now.testing@gmail.com&quot;);
      mailMessage.To.Add(lblStudentEmail.Text);
      mailMessage.IsBodyHtml = true; 
      mailMessage.Subject = &quot;Parcel&quot; + txtConsignmentNum.Text + &quot;Available for Pick Up&quot;;
      mailMessage.Body = &quot;Hi &quot; + &quot;sample text&quot; + Image1;
  
      SmtpClient smtpClient = new SmtpClient();
      smtpClient.Host = &quot;smtp.gmail.com&quot;; 
      smtpClient.Port = 587;
      smtpClient.EnableSsl = true;
      smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
      smtpClient.UseDefaultCredentials = false; 
      smtpClient.Credentials = new System.Net.NetworkCredential(&quot;email&quot;, &quot;password&quot;);
      smtpClient.Send(mailMessage);
      Response.Write(&quot;&lt;script&gt;alert('Your email has been sent!')&lt;/script&gt;&quot;);
  }
  catch (Exception ex)
  {
      throw; 
      // Response.Write(&quot;&lt;script&gt;alert('Your email could not be sent successfully due to error found: &quot; + ex.ToString() + &quot;')&lt;/script&gt;&quot;);
  }
  </code></pre>
  <p>Please let me know if there is any solution or suggestion to this.</p>