Sending Email in ASP.NET 2.0 using gmail relay server, Sending Web page via Email

In this article I will explain about how to sed email from ASP.NET 2.0.

Normally when you sending email from any source(Outlook or yahoo or gmail), the email program establishes a connection with relay server(sendr server) and send the contents of the email, along with the other information such as date of the email send and format of the body(text or HTML or image or any other) and the recipient(s) details.
Then the relay server accepts the message and connects to the recipient(s) SMTP server and sends the message. After some point of time recipient receives the message using their own protocol(sush as IMAP Or POP3).

Here I am showing simple way to send email from ASP.Net 2.0 by using gmail server as relay server.
For this you need to have account in gmail.

We need to import two namspaces System.Net and System.Net.Mail to send email from ASP.Net.
You can find source code to send email using gmail server as below.


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head2" runat="server"> <title>Send Email From ASP.NET 2.0</title> <script type="text/javascript"> function check() { if(document.getElementById('toEmail').value=='') { alert('Please enter Receiver Email Address'); document.getElementById('toEmail').focus(); return false; } if(document.getElementById('Subject').value=='') { alert('Please enter Subject'); document.getElementById('Subject').focus(); return false; } if(document.getElementById('Body').value=='') { alert('Please enter your message'); document.getElementById('Body').focus(); return false; } } </script> </head> <body> <form id="form1" runat="server"> <div> <div style="margin-top:30px; "> <asp:Label ID="lbl_response" runat="Server" style="color:Red;" ></asp:Label> <br /><br /> <asp:Label ID="msg_display" style="color:Red;" runat="Server" ></asp:Label> <br /> <b>To:</b><asp:TextBox style="margin-left:95px;margin-top:20px;" runat="server" ID="toEmail" Columns="30"></asp:TextBox><br /> <b>Subject:</b><asp:TextBox style="margin-left:70px;margin-top:30px;" runat="server" ID="Subject" Columns="30"></asp:TextBox><br /><br /> <b style="margin-top:40px;">Body:</b><br /><asp:TextBox style="margin-left:120px;" TextMode="MultiLine" runat="server" ID="Body" Rows="10" Columns="50"></asp:TextBox><br /> <asp:Button runat="server" OnClientClick="return check();" style="margin-left:150px;margin-top:40px;width:100px;" ID="SendEmail" Text="SendEmail" /> </div> </div> </form> </body> </html>

The code behind class to send Email using gmail server is as shown below.

Imports System Imports System.Net Imports System.Net.Mail Partial Class _Default Inherits System.Web.UI.Page Dim mailSent As Boolean Sub sendmail() Try Dim client As New SmtpClient() 'Change these techinfocorner@gmail.com and xxxxx to your gmail account ' Credentials (user name and password) client.Credentials = New System.Net.NetworkCredential("techinfocorner@gmail.com", "xxxxx") client.Port = 587 client.Host = "smtp.gmail.com" client.EnableSsl = True Dim msg As New MailMessage() msg.To.Add(toEmail.Text) 'Change these techinfocorner@gmail.com and xxxxx to your email address 'and display name(which one you want to display on the mail) msg.From = New MailAddress("techinfocorner@gmail.com", "xxxxx", System.Text.Encoding.UTF8) msg.Subject = Subject.Text msg.SubjectEncoding = System.Text.Encoding.UTF8 msg.Body = Body.Text 'msg.Attachments="path of the file which you want to send" msg.BodyEncoding = System.Text.Encoding.UTF8 msg.IsBodyHtml = False msg.Priority = MailPriority.High Dim userState As Object = msg client.Send(msg) lbl_response.Text = "Email sent Successfully" Catch ex As Exception lbl_response.Text = ex.Message End Try End Sub Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click sendmail() End Sub End Class

In the above code you can observe that, first you have to create object for SmtpClient class, then you have to mention realy server(such as sender server) credentials.

For gmail relay server the ports is 587 and server name is "smtp.gmail.com".
For client Credentials, you have to mention your gmail account credentials correctly. If you mention wrong Credentials, client will show exception message.

After creating client, create object for MailMessage() class, which is the actual message going to the relay server and receiver emails.
It requires from mail address,subject and body. You can attach files by giving path of the file to the Attachments property of MailMessage() object.
Even you can mention body format as HTML by setting the IsBodyHtml property to True of the MailMessage() object.

Here we provided the three text boxes to enter receiver email address,subject for message and body.
In this way you can send email from any of your ASP.Net page by creating simple environment.


Sending web page via Email

In the above I explained about how to send simple email from Asp.Net page.
Here I am discussing about how to send ASP.NET page via email.

The code to send web page via email is shown below.

 
Imports System Imports System.Net Imports System.Net.Mail Imports System.IO Partial Class _Default Inherits System.Web.UI.Page Sub sendmail() Try Dim client As New SmtpClient() 'Change these techinfocorner@gmail.com and xxxxx to your gmail account 'user name and password client.Credentials = New System.Net.NetworkCredential("techinfocorner@gmail.com", "xxxxx") client.Port = 587 client.Host = "smtp.gmail.com" client.EnableSsl = True Dim msg As New MailMessage() msg.To.Add(toEmail.Text) 'Change these techinfocorner@gmail.com and xxxxx to your gmail email address 'and display name(which one you want to display on the mail) msg.From = New MailAddress("techinfocorner@gmail.com", "xxxxx", System.Text.Encoding.UTF8) msg.Subject = Subject.Text msg.SubjectEncoding = System.Text.Encoding.UTF8 msg.Body = sendHtmlPage(txturl.text) msg.BodyEncoding = System.Text.Encoding.UTF8 msg.IsBodyHtml = True msg.Priority = MailPriority.High Dim userState As Object = msg client.Send(msg) lbl_response.Text = "Email sent Successfully" Catch ex As Exception lbl_response.Text = ex.Message End Try End Sub Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click sendmail() End Sub Function sendHtmlPage(ByVal url As String) Dim result As String = String.Empty Dim req As HttpWebRequest Dim resp As HttpWebResponse req = CType(WebRequest.Create(url), HttpWebRequest) req.Method = WebRequestMethods.Http.Get resp = CType(req.GetResponse, HttpWebResponse) Dim sr As New StreamReader(resp.GetResponseStream) While sr.Peek > -1 result = sr.ReadToEnd End While sr.Close() Return result End Function End Class

In the above code you can observe that, we define the one sendHTML page function which takes the URL of the page as input and returns the HTML as output.
In this way we can send web page via email from an Asp.Net page.
Download source code here