Swag
Swag

Reputation: 2140

Send automated e-mail to new user aspx

When someone registers on my website he/she also needs to fill in his/her e-mail address.

After clicking on the ''Register''-button the system needs to send an automated e-mail to the e-mail address which is written in the textbox.

Anyone who can help me with this?

Thanks in advance!

Upvotes: 2

Views: 1390

Answers (4)

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

Here is a simple function to implement. You have to get network credentials if not anonymous and SMTP info about your SMTP relay server. Otherwise it should be straight forward.

 using System.Net.Mail; //goes on top

 //goes in your class
 public void sendEmail(string emailMessage, 
     string emailSubject, 
     string emailAddress, string from, 
     string fromAddress, string emailCC, 
     string emailBCC)
    {
        try
        {
            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(fromAddress, from);
            msg.To.Add(emailAddress);

            if (emailCC != null && emailCC.ToString().Length > 1)
                msg.CC.Add(emailCC);

            if (emailBCC != null && emailBCC.ToString().Length > 1)
                msg.Bcc.Add(emailBCC);

            msg.Priority = MailPriority.High;

            msg.Subject = emailSubject;
            msg.Body = emailMessage;
            msg.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Host = info.SMTPServer;
            client.Port = 25;
            client.EnableSsl = true;
            // client.UseDefaultCredentials = some System.Net.NetworkCredential var;
            client.Credentials = info.networkCredentials;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            // Use SendAsync to send the message asynchronously
            client.Send(msg);
        }
        catch
        {
            //handle exception
        }
    }

USAGE & Implementation:

        this.sendEmail("test message", "your subject", "[email protected]","from person", "[email protected]","[email protected]","[email protected]"); 

Upvotes: 1

Darren Wainwright
Darren Wainwright

Reputation: 30747

The following script is a good starting place.

           using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage()) {
               System.Text.StringBuilder body = new System.Text.StringBuilder("Your message");
               System.Net.Mail.SmtpClient() smtp = new System.Net.Mail.SmtpClient();
               mail.To.Add("[email protected]");
               mail.From = new System.Net.Mail.MailAddress("[email protected]");
               mail.Subject = "a Subject";
               mail.Body = body.ToString();
               try {
                smtp.Send(mail);
                } catch (Exception ex) {
                       // handle your exception here..
                }
            }

This assumes that you have some details in your config about the SmtpClient. Like this: http://msdn.microsoft.com/en-us/library/ms164240.aspx

If not, then you can provide the details to the client in the SmtpClient() constuctor. More info on that here: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

Upvotes: 1

Kevin Kunderman
Kevin Kunderman

Reputation: 2134

To send email in asp.net you will want to look into System.Net.Mail. There are two steps two send mail from within asp.net

1) Email Account Settings - This can be set up globally in your web.config file

<system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="host" port="25" userName="username" password="password" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

2) Setting up your message - In your register pages code behind

MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");

message.To.Add(new MailAddress("[email protected]"));

message.Subject = "subject";
message.Body = "content";

SmtpClient client = new SmtpClient();
client.Send(message);

http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx

Upvotes: 2

Knaģis
Knaģis

Reputation: 21485

Here is a simple instruction on how to send e-mail from .NET code: http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx

The main keyword you are looking for is System.Net.Mail.

Upvotes: 1

Related Questions