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

How do I setup my login page to send an email when users register to access my ASP.NET site?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

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

Should be this:   MailMessage mailmessage = new MailMessage();
vcharlesAuthor Commented:
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?
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

vcharlesAuthor Commented:

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


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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.