Home » Knowledgebase » Sample Scripts » Send Email using ASP.Net VB.Net

Send Email using ASP.Net VB.Net

Send Email using ASP.Net VB.Net

ASP.Net has built in class to send email via script. You can send email with or without authentication using ASP.Net namespace System.Net.Mail.

Following is the sample code to send mail using ASP.Net VB.Net:

<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Net.Mail" %> 
<script runat="server"> 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
 
        Dim strFrom = "yourname@yourdomain.com" 
        Dim strTo = "recipient_email@domain.com" 
        Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo)) 
        MailMsg.BodyEncoding = Encoding.Default 
        MailMsg.Subject = "This is a test email using VB.Net Code" 
        MailMsg.Body = "This is a test email using VB.Net Code" 
        MailMsg.Priority = MailPriority.High 
        MailMsg.IsBodyHtml = True
 
        Dim SmtpMail As New SmtpClient 
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential("smtp@yourdomain.com", "password") 
 
        SmtpMail.Host = "smtp.yourdomain.com" 
        SmtpMail.UseDefaultCredentials = False 
        SmtpMail.Credentials = basicAuthenticationInfo 
 
        SmtpMail.Send(MailMsg) 
        lblMessage.Text = "Mail Sent"     
    End Sub 
</script> 
<html> 
<body> 
    <form runat="server"> 
        <asp:Label id="lblMessage" runat="server"></asp:Label> 
    </form> 
</body>

Make sure to make necessary changes in your script (i.e. SMTP server, email address, password etc.)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: