The following example will demonstrate to you how to send email in ASP.Net VB using the SMTP class. VB.Net allows sending e-mails from your web application. The System.Net.Mail namespace contains classes used for sending e-mails to a Simple Mail Transfer Protocol (SMTP) server for delivery.
- Keep reading on How to read and display RSS feed in ASP.NET C#
How to send email in ASP.Net VB Code
Find the below points which are required to send an email:-
- You must specify the SMTP host server that you use to send an e-mail. The Host and Port properties will be different for the different host servers. We will be using the Gmail server.
- You need to give the credentials for authentication if required by the SMTP server.
- You should also provide the email address of the sender and the e-mail address or addresses of the recipients using the MailMessage.From and MailMessage.To properties, respectively.
- You should also specify the message content using the MailMessage.Body property.
Now let’s create an asp.net web application and add a VB.aspx web form, find the source code below:-
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB.aspx.vb" Inherits="VB" %> <!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="Head1" runat="server"> <title>How to Send Email in ASP.Net VB</title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } table { border: 1px solid #ccc; border-collapse: collapse; } table th, table td { padding: 5px; } table, table table td { border: 0px solid #ccc; } </style> </head> <body> <form id="form1" runat="server"> <h2>How to Send Email in ASP.Net VB - Demo</h2> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td>To:</td> <td><asp:TextBox ID="txtTo" runat="server" /></td> </tr> <tr> <td>Subject:</td> <td><asp:TextBox ID="txtSubject" runat="server" /></td> </tr> <tr> <td valign = "top">Body:</td> <td><asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="150" Width="200" /></td> </tr> <tr> <td></td> <td><asp:Button ID="btnSend" Text="Send" runat="server" OnClick = "SendEmail" /></td> </tr> </table> </form> </body> </html>
VB.aspx.vb
Imports System.Net Imports System.Net.Mail Imports System.Net.Configuration Partial Class VB Inherits System.Web.UI.Page Protected Sub SendEmail(sender As Object, e As System.EventArgs) Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection) Using mm As MailMessage = New MailMessage(smtpSection.From, txtTo.Text.Trim()) mm.Subject = txtSubject.Text.Trim() mm.Body = txtBody.Text.Trim() mm.IsBodyHtml = False Dim smtp As SmtpClient = New SmtpClient smtp.Host = smtpSection.Network.Host smtp.EnableSsl = smtpSection.Network.EnableSsl Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password) smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials smtp.Credentials = networkCred smtp.Port = smtpSection.Network.Port smtp.Send(mm) Response.Write("Your email has been sent sucessfully.") End Using End Sub End Class
Web.config
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.net> <mailSettings> <smtp deliveryMethod="Network" from="sender@gmail.com"> <network host="smtp.gmail.com" port="587" enableSsl="true" userName="sender@gmail.com" password="sender@gmail.com password" defaultCredentials="true"/> </smtp> </mailSettings> </system.net> <system.web> <compilation targetFramework="4.0" debug="true"/> <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/> </system.web> </configuration>
Conclusion
I hope you liked this article on how to send email in asp.net vb. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Leave a Reply