Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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

Hello,
How do I setup my login page to send an email when users register to access my ASP.NET site?
Avatar of P1ST0LPETE
P1ST0LPETE
Flag of United States of America image

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 = "noreply@your-domain.com";
    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("mail.your-domain.com");  //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.To.Add(To);
        mailmessage.Subject = Subject;
        AlternateView html = AlternateView.CreateAlternateViewFromString(Message, null, "text/html");
        mailmessage.AlternateViews.Add(html);

        try
        {
            smtp.Send(mailmessage);
            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 = "username@email.com"

/* 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 www.your-domain.com.");
message.Append("<br /><br /> To activate your registration, please visit the following page:");
message.Append("<br /> http://www.your-domain.com/Validate.aspx?key=" + 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());
mail.Send();

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();
Avatar of Victor  Charles

ASKER

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?
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:

IP: yourdomain.secureserver.net
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 = "noreply@your-domain.com"
    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.To.Add(Me._To)
        mailmessage.Subject = Me.Subject
        mailmessage.AlternateViews.Add(html)
        
        Try
            smtp.Send(mailmessage)
            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 = "user@email.com"

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())
mail.Send()

Open in new window

Hi,

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

Thanks,

Victor
ASKER CERTIFIED SOLUTION
Avatar of P1ST0LPETE
P1ST0LPETE
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thank You!