Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Send Email with Authentication using ASP.NET and VB.NET (0 Comments)
Admin: Posted Date: April 4, 2010

This tutorial will show you how to send a simple email message with authentication using ASP.NET 2.0 and VB.NET.

Send Email with Authentication using ASP.NET and VB.NET


This tutorial will show you how to send a simple email message with authentication using ASP.NET 2.0 and VB.NET

Sending a email with Priorities using ASP.NET 2.0 and VB.NET is actually very simple.

First, you will need to import the System.Net.Mail namespace.

The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and specify the user credentials necessary to send authenticated email.

Imports System.Net.Mail

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

We use the btnSubmit_Click event to do the work.

We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page. We also instantiate a System.Net.NetworkCredential object with the necessary authentication info and then assign that object to the Credentials property of our SmtpClient object.




Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim message As New MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text)
Dim emailClient As New SmtpClient(txtSMTPServer.Text)
Dim SMTPUserInfo As New System.Net.NetworkCredential(txtSMTPUser.Text, txtSMTPPass.Text)
emailClient.UseDefaultCredentials = False
emailClient.Credentials = SMTPUserInfo
emailClient.Send(message)
litStatus.Text = "Message Sent"
Catch ex As Exception
litStatus.Text = ex.ToString()
End Try
End Sub

The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc">
<tr>
<td height="50" align="center" class="lgHeader1">How to Send Email with Authentication using ASP.NET 2.0 and C#</td>
</tr>
</table>
<br />
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> To</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> From</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtFrom" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1"> SMTP Server</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPServer" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">SMTP User</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPUser" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">SMTP Pass</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPPass" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Subject</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Body</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">Action</td>
<td bgcolor="#FFFFFF"><asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" /></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Status</td>
<td bgcolor="#FFFFFF" class="basix"><asp:Literal ID="litStatus" runat="server"></asp:Literal></td>
</tr>
</table>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.


 

The flow for the code behind page is as follows.

Imports System.Net.Mail

Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim message As New MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text)
Dim emailClient As New SmtpClient(txtSMTPServer.Text)
Dim SMTPUserInfo As New System.Net.NetworkCredential(txtSMTPUser.Text, txtSMTPPass.Text)
emailClient.UseDefaultCredentials = False
emailClient.Credentials = SMTPUserInfo
emailClient.Send(message)
litStatus.Text = "Message Sent"
Catch ex As Exception
litStatus.Text = ex.ToString()
End Try
End Sub
End Class

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: