Sending Email using ASP.NET

by Mohit Bhardwaj 24. March 2009 02:01

Last night I was thinking to write a small post about sending emails using .NET. Then I thought of using GMAIL smtp account to send email. So here is the code.

using System;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Email Sending Class for GMAIL account.
/// </summary>

public class Emails
{
public Emails()
{
//
// TODO: Add constructor logic here
//
}

 

public string sendmail(string body)
{
try
{
MailMessage mail=
new MailMessage();
mail.To.Add(new MailAddress("xxxx@gmail.com"));
mail.From=
new MailAddress("yyyy@gmail.com");
mail.Body=body;
SmtpClient smtp=
new SmtpClient("smtp.gmail.com",587);
smtp.Credentials=new NetworkCredential("yyyy@gmail.com", "#&&$$%#%"); // This is where you have to specify account properties of the email account you are using to send email. yyyy@gmail.com is the userid and #&&$$%#% is the password.
smtp.EnableSsl=true;
smtp.Send(mail);
return "Success";
}
catch(Exception ex)
{
return "Failed";
}
}
 

Now from here you can call this class and send email. I have used the static emails here but you can make it change accordingly to use it as dynamically. You will need to add more paramaters like "body" in calling this function.

Thanks.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Beginners

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About Me

Hi, I am Mohit Bhardwaj, living in New Delhi, India. I am an Independent software developer for Microsoft Technologies.

Hope you will like my posts here on this blog.

LinkedIn

Page List

    Calendar

    <<  March 2010  >>
    MoTuWeThFrSaSu
    22232425262728
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    View posts in large calendar