Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-10-05
7
Medium Priority
?
217 Views
Last Modified: 2012-05-12
Hello,
How do I setup my login page to send an email when users register to access my ASP.NET site?
0
Comment
Question by:vcharles
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 36922324
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

0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 36922336
Ooops, just noticed a copy/paste flub-up.


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


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

Author Comment

by:vcharles
ID: 36922351
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 36924716
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

0
 

Author Comment

by:vcharles
ID: 36934428
Hi,

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

Thanks,

Victor
0
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 2000 total points
ID: 36943955
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span>Name:</span>
        <asp:TextBox ID="tbName" runat="server" />
        <br />
        <span>Email:</span>
        <asp:TextBox ID="tbEmailAddress" runat="server" />
        <br />
        <asp:Button ID="btnRegister" runat="server" Text="Register" />
    </div>
    </form>
</body>
</html>

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

    End Sub
End Class

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 36968307
Thank You!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 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