Send mail in ASP.NET via GMail account

Good morning,

In this post, I want to show a simple code that demonstrate how to send a email in ASP.NET application via a GMail account.

ASPNET GMail Send mail in ASP.NET via GMail account

Now, open Visual Studio 2005/2008 and create new ASP.NET project.

Declare this assembly below:

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

Use this function, modify if needed

public static bool SendMail(string to)
        {
            MailMessage mail = new MailMessage();
 
            mail.To.Add(to);
            mail.Bcc.Add(new MailAddress("someone@domain.com"));
            mail.Subject = "Welcome to MicroSYNC Blog !";
 
            mail.IsBodyHtml = true; // if you want to use HTML tags
 
            string str = "Dear Mr.AD,"
						+ "Thank you for your member registering at MicroSYNC Blog...Blabblablalaa";
 
            mail.Body = str;
 
            try
            {
                SmtpClient client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Host = "smtp.gmail.com";
                client.Port = 587;
 
                // setup Smtp authentication
				string GMailAccount = "no-reply@microsync.net";
				string password = "sYyT%r#$&frdfHGy45764565f%TG$%%^DFS#$FDTYt"; // Very strong password :)
 
                NetworkCredential credentials = new NetworkCredential(GMailAccount, password);
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
 
                client.Send(mail);
 
                return true;
            }
            catch (Exception)
            {
                return false;
            }
         }
    }
}

That ’s really simple ?

No Responses to “Send mail in ASP.NET via GMail account”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:

Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
CommentLuv Enabled