Sending Email in ASP.NET Using Gmail Account

sending email using google mail asp.net

In this article, I tried to explain a common requirement which implemented more frequently in various ways.

Like this we can implement these requirements in many ways, In these scenarios, we get some error occur like SMTP Server related. In my previous article, I already discussed this issue and showed the solution and resolved the SMTP Server issue. I strongly recommend reading that SMTP Server Secure Connection and Authentication Error article before continuing this current tutorial.

HTML Markup: Create Sending Email Page

In the *.aspx page design the user interface according to the requirements shown in the above image. Create a web page with HTML markup to input the email address, compose the email message body, and add a sending button which executes the sending mail process.


 <div>
     <table>
      <tr>
      <td>
       <table>
      <tr>
       <th colspan="2" align="left">Single Mail Sending</th>
      </tr>
      <tr>
      <td style="width:20px;">To : </td><td><asp:TextBox ID="txt_single_mail" runat="server" Width="310px"></asp:TextBox></td>
      </tr>
      <tr>
      <td colspan="2">
       <asp:TextBox ID="txt_single_msg" runat="server" TextMode="MultiLine" Width="336px" 
              Height="199px"></asp:TextBox>
      </td>
      </tr>
      <tr>
       <td colspan="2">
        <asp:Button ID="Btn_single_mail" runat="server" Text="Send Single Mail" 
               onclick="Btn_single_mail_Click" />
       </td>
      </tr>
     </table>
      </td>
      </tr>
      </table>
      </div>

C# Coding: Sending Email

Next, implement the C# code to handle the button click event and send a single email using Gmail's SMTP server.


protected void Btn_single_mail_Click(object sender, EventArgs e)
    {
        MailMessage ms = new MailMessage("your Gmail ID", txt_single_mail.Text);
        ms.Subject = "Testing Sending Single Mail";
        ms.Body = txt_single_msg.Text;
        ms.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.Credentials = new System.Net.NetworkCredential("your Gmail ID", "your Gmail ID Password");
        smtp.EnableSsl = true;
        smtp.Send(ms);
    }

Tutorial: Resolving SMTP Server Issues

In my previous tutorial, I already discussed and resolved the SMTP Server issue that may occur during the email-sending process. For more details please click on the below image it will navigate to the SMTP Server article.

smtp server authentication error
Read more about resolving SMTP server errors here.

By following these tutorials, you'll become proficient in sending emails in ASP.NET using your Gmail account and overcoming common SMTP server challenges.

Post a Comment

0 Comments