Help with sending email to admistrator when user registers in Login Page

Posted on 2011-10-05
Last Modified: 2012-05-12
How do I setup my login page to send an email when users register to access my ASP.NET site?
Question by:vcharles
    LVL 10

    Expert Comment

    If you're using a hosting service, you will probably have to look up their documentation on the security/procedures they have for sending email.  At any rate, below is a class I wrote to send email.  Just plug in the IP address and port and other info that is unique to your web environment.

    Here is a email class that I wrote:
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Mail;
    using System.Web;
    /// <summary>
    /// Summary description for Email
    /// </summary>
    public class Email
        public String From = "";
        public String To = string.Empty;
        public String Subject = string.Empty;
        public String Message = string.Empty;
        public Email() { }
        public Email(string to, string subject, string message)
            this.To = to;
            this.Subject = subject;
            this.Message = message;
        public Email(string from, string to, string subject, string message)
            this.From = from;
            this.To = to;
            this.Subject = subject;
            this.Message = message;
        public Boolean Send()
            SmtpClient smtp = new SmtpClient("");  //your smtp IP address here
            smtp.Port = 26;
            smtp.EnableSsl = false;
            smtp.Credentials = new NetworkCredential(this.From, "your password");
            MailMessage mailmessage = mailmessage = new MailMessage();
            mailmessage.From = new MailAddress(From, "From Title");
            mailmessage.Subject = Subject;
            AlternateView html = AlternateView.CreateAlternateViewFromString(Message, null, "text/html");
                return true;
            catch (Exception ex)
                //TODO: possibly log error
                string error = ex.ToString();
                return false;

    Open in new window

    Then to use it you would do something like shown below.  Also, don't get too hung up on the activation key thing, I was just adding that in as a rough example of what I do when a users registers at one of my websites.
    string email = ""
    /* some code to register (add to database) user */
    string activationKey = MaybeGenerateAnActivationKey();
    StringBuilder message = new StringBuilder();
    message.Append("Somebody with this e-mail address registered a user account at");
    message.Append("<br /><br /> To activate your registration, please visit the following page:");
    message.Append("<br />" + activationKey);
    message.Append("<br /><br /> Note that this page will expire in 24 hours, so act quickly.");
    message.Append("<br /> If clicking the above link does not work, please copy and paste the link into your browser.");
    message.Append("<br /><br /> -------------------------------------------------------------------------------------");
    message.Append("<br /> If you didn't request it, you may safely delete this message; we won't bug you again.");
    Email mail = new Email(email, "Your Domain Name: Activate Your New Account", message.ToString());

    Open in new window

    LVL 10

    Expert Comment

    Ooops, just noticed a copy/paste flub-up.

    This:   MailMessage mailmessage = mailmessage = new MailMessage();

    Should be this:   MailMessage mailmessage = new MailMessage();

    Author Comment

    Thanks for the code, but my project is in VB.NET, do you have VB.NET version. I don't understand why I need to enter the IP address and port information. I will deploy this product to a web server, do you mean I need the server's IP address and port information in order to use this feature?
    LVL 10

    Expert Comment

    Ok, so you need to know the IP address and port number that your mail server is using.  Most hosting companies, such as GoDaddy, have several servers separated from their web servers, that do nothing but relay email.  The hosting company will tell you the IP and Port needed to connect to these mail servers.  At GoDaddy they are something like this:

    Port: 25

    Also, almost all hosting companies have their mail servers setup to require a username and password when sending mail. The username is typically the email address, and the password is whatever password you used when setting up the email address.  These are used to authenticate to the mail server so that only known email accounts can send email.

    Even if you are setting up your own web server in your house, the .Net Framework's email sending functionality requires the IP address and Port number, as they are needed for the system to know how to send the message over the network.

    Below is the above C# code converted to VB:
    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Collections.Generic
    Imports System.Net
    Imports System.Net.Mail
    Imports System.Web
    Public Class Email
        Public From As String = ""
        Public _To As String = String.Empty
        Public Subject As String = String.Empty
        Public Message As String = String.Empty
        Public Sub Email()
        End Sub
        Public Sub Email(ByVal _to As String, ByVal subject As String, ByVal message As String)
            Me._To = _to
            Me.Subject = subject
            Me.Message = message
        End Sub
        Public Sub Email(ByVal from As String, ByVal _to As String, ByVal subject As String, ByVal message As String)
            Me.From = from
            Me._To = _to
            Me.Subject = subject
            Me.Message = message
        End Sub
        Public Function Send() As Boolean
            Dim smtp As New SmtpClient("")
            smtp.Port = 26
            smtp.EnableSsl = False
            smtp.Credentials = New NetworkCredential("username", "password")
            Dim html As AlternateView = AlternateView.CreateAlternateViewFromString(Me.Message, Nothing, "text/html")
            Dim mailmessage As New MailMessage()
            mailmessage.From = New MailAddress(Me.From, "Email Display Name")
            mailmessage.Subject = Me.Subject
                Return True
            Catch ex As Exception
                Dim err As String = ex.ToString()
                Return False
            End Try
        End Function
    End Class

    Open in new window

    And to use it would be like this:
    Dim email As String = ""
    Dim message As New StringBuilder()
    message.Append("Some message text to send.")
    message.Append("Some more stuff.")
    message.Append("<p>Some embeded html in the email</p>")
    Dim mail As Email = New Email(email, "Thanks for Registering", message.ToString())

    Open in new window


    Author Comment


    Where in the form do I include the second part of your code?


    LVL 10

    Accepted Solution

    Right, so you probably will have some web form to register a user.  Something like this:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
            <asp:TextBox ID="tbName" runat="server" />
            <br />
            <asp:TextBox ID="tbEmailAddress" runat="server" />
            <br />
            <asp:Button ID="btnRegister" runat="server" Text="Register" />

    Open in new window

    And then your code behind for that page would look something like this:
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click
            Dim name As String = tbName.Text
            Dim email As String = tbEmailAddress.Text
            'do some code here to actually load the newly registered user into your database
            Dim message As New StringBuilder()
            message.Append("Greetings " + name + ",")
            message.Append("<p>You have successfully registered to my website.</p>")
            Dim mail As Email = New Email(email, "Thanks for Registering", message.ToString())
        End Sub
    End Class

    Open in new window


    Author Closing Comment

    Thank You!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now