Decrease Font Size
Increase Font Size
   BLOG

ASP.NET Sending email through Hotmail

by bryian 8. November 2009 13:12

ASP .NET How to Send Email asynchronously | ASP .NET sends email using live.com account

I was browsing the ASP.NET forum the other day and saw a topic "Problem sending email" using hotmail account Asynchronously. Personally, I haven't tried that before and I did get it to work. Below is the code in vb.net and CSharp version. Make sure to set Async="true" in the page directive because we are sending the email Asynchronously.

Here is the entry in Web.config

<configuration>
<system.net>
    <mailSettings>
      <smtp from="myEmail@hotmail.com"> 
        <network host="smtp.live.com" defaultCredentials="false"  port="25" userName ="myHotmailEmail@hotmail.com" password="myHotmailPassword" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

This is the ASPX page

<%@ Page Language="VB" Async="true" AutoEventWireup="false" CodeFile="TestHotmail.aspx.vb" Inherits="TestHotmail" %>

<!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>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

VB.NET

Imports System.Net.Mail
Partial Class TestHotmail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler Button1.Click, AddressOf Button1_Click
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mailMessage As New System.Net.Mail.MailMessage

        mailMessage.To.Add(New MailAddress("toSomeone@gmail.com"))
        mailMessage.From = New MailAddress("fromMe@hotmail.com")

        mailMessage.Subject = "my test subject"
        mailMessage.Body = "my test body"
        mailMessage.IsBodyHtml = True

        Dim smtpClient As New SmtpClient()
        smtpClient.EnableSsl = True

        Dim userState As Object = mailMessage

        AddHandler smtpClient.SendCompleted, AddressOf smtpClient_OnCompleted
        smtpClient.SendAsync(mailMessage, userState)

    End Sub

    Public Sub smtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        Dim mailMessage As MailMessage

        mailMessage = CType(e.UserState, MailMessage)

        If (e.Cancelled) Then
            lblMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To(0).Address
        End If
        If Not (e.Error Is Nothing) Then
            lblMessage.Text = "Error occured, info :" + e.Error.Message
        Else
            lblMessage.Text = "Mail sent successfully"
        End If
    End Sub

End Class

 

If you are using CSharp, this is the code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class TestHotmailCsharp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click +=new EventHandler(Button1_Click);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

        mailMessage.To.Add(new MailAddress("toSomeone@gmail.com"));
        mailMessage.From = new MailAddress("fromMe@hotmail.com");

        mailMessage.Subject = "my test subject";
        mailMessage.Body = "my test body";
        mailMessage.IsBodyHtml = true;

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.EnableSsl = true;

        object userState = mailMessage;

        smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
        smtpClient.SendAsync(mailMessage, userState);
    }

    void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        MailMessage mailMessage = default(MailMessage);

        mailMessage = (MailMessage)e.UserState;

        if ((e.Cancelled))
        {
            lblMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To[0].Address;
        }
        if ((e.Error != null))
        {
            lblMessage.Text = "Error occured, info :" + e.Error.Message;
        }
        else
        {
            lblMessage.Text = "Mail sent successfully";
        }
    }
}

 

References:
Send Email Asynchronously with ASP.NET
Incoming and Outgoing Mail Server Settings for Hotmail, Yahoo! Mail, GMail, MSN, AOL and more

Comments

10/18/2009 2:35:58 AM #

trackback

ASP.NET Sending email through Hotmail

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

11/28/2009 10:18:21 PM #

miracle cabbage soup diet

Thanks for such a nice post.



Regards
Iron

miracle cabbage soup diet United States

12/12/2009 2:59:07 PM #

Niche Blueprint 2.0 Review

I love reading your posts. I wish you a Happy New Year!

Niche Blueprint 2.0 Review United States

12/15/2009 1:50:05 AM #

Astaga.com lifestyle on the net

WOW ... I learn lots of new stuff here ! regard !!

Astaga.com lifestyle on the net United States

1/6/2010 3:14:29 PM #

Best Car Rental

Thanks for such a nice post. I learnt a lot !

Best Car Rental United States

1/7/2010 7:50:48 AM #

Top Destinations

still works great

Top Destinations United States

1/9/2010 8:30:05 PM #

Lisa Lunndra

I am surprised at the things I overlooked before I read this post. Thanks for the great information.

Lisa Lunndra United States

1/15/2010 8:26:39 PM #

chicago furniture stores

it is so happy can visit the blog Smile

chicago furniture stores Indonesia

1/17/2010 5:59:59 AM #

bambang tri

Aaaargh, I wish I read this article sooner.
Nice share, thanks.

bambang tri Indonesia

1/23/2010 9:24:02 AM #

business opportunities from home

Insightful piece, thanks a lot!

business opportunities from home United States

1/25/2010 12:05:36 PM #

diets that work

Thanks for putting this together - this is a great article for those of us with our heads buried in the keyboard all day.

diets that work United States

1/26/2010 12:20:50 PM #

jillian

Thanks very much for the information. I have been searching for this for awhile with Google and it has been a real chore.

jillian United States

2/1/2010 6:18:39 PM #

35mm slide scanners

THX FOR THE CODE!

I always wonderd how i could send mails trough hotmail.

Greetz

Nick

35mm slide scanners Belgium

2/14/2010 8:50:09 AM #

ripped abs

This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article!

ripped abs Islamic Republic of Pakistan

2/14/2010 10:08:21 PM #

Tech Blog

Thanks. This makes my life a lot easier.

Tech Blog United States

2/14/2010 11:53:24 PM #

Watch Movies Online

Such a Wonderful Job... Solved my Problem.. i just needed that.

Watch Movies Online United Kingdom

2/21/2010 7:25:45 PM #

Acid Reflux Symptoms

Excellent read, I just passed this onto a colleague who was looking for this info. I have you bookmarked on Digg.

Acid Reflux Symptoms Jordan

3/2/2010 1:40:24 AM #

overseas removals

Useful info. Hope to see more good posts in the future.

overseas removals United Kingdom

3/2/2010 2:26:14 PM #

James @ overclock your cpu

This is interesting, post more often!

James @ overclock your cpu Tunisia

3/2/2010 5:43:15 PM #

James @ How to speed up windows Vista pc

Interesting blog, not like the others!

James @ How to speed up windows Vista pc Poland

3/11/2010 6:09:30 PM #

Port Aransas Texas Rentals

I am thankful for exerting your time and effort to post this very informative post. keep up the good work!

Port Aransas Texas Rentals United States

3/16/2010 2:57:02 AM #

Norbert Alcott

Thanks for all of your ASP.NET advice. It has been very useful.

Norbert Alcott United Kingdom

3/21/2010 3:58:56 AM #

laptop repair westchester ny

Interesting post Smile

laptop repair westchester ny Oman

4/16/2010 9:58:29 PM #

Sunderland

I generally don�t post in Blogs but your weblog pressured me to,
amazing perform.. gorgeous �

Sunderland Panama

4/26/2010 10:08:29 PM #

Tom

Love your blog!

Tom Germany

5/10/2010 9:33:47 PM #

Brian

Hi, I used the C# version.  Thanks.

Brian United States

8/25/2010 9:44:54 AM #

bryian

Use port 587 instead of 25

bryian United States

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading