Tuesday, January 17, 2012

Send Mail through Gmail or Hotmail using C#.Net


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 class SendMail_App
{

    public static bool SendMail(string message, string mailFrom, string mailTo, string subject)
    {
        //  Set Mail Properties
        MailMessage ourMessage = new MailMessage();
        ourMessage.To.Add(new MailAddress(mailTo));
        ourMessage.From = new MailAddress(mailFrom);
        ourMessage.Body = message;
        ourMessage.IsBodyHtml = true;
        ourMessage.Subject = subject;


        //  Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
        //SmtpClient Client = new SmtpClient("smtp.live.com", 25); //Send Mail using Hotmail
        SmtpClient Client = new SmtpClient("smtp.gmail.com", 587); //Send Mail using Hotmail
        Client.EnableSsl = true;
        Client.Credentials = new System.Net.NetworkCredential("Your Email ID", "Password");


        try
        {
            Client.Send(ourMessage);    // Send your mail.
            return true;                // IF Mail sended Successfully
        }
        catch (Exception ex)
        {
            return false;              // Send error
        }
    }

}

4 comments:

  1. this give error failure Sending email.

    ReplyDelete
    Replies
    1. if you can give me the error description then i can look in to it..... because i have tested the above code and it works fine at my end.....

      Delete
  2. its throwing exception....
    failure sending mail.

    ReplyDelete
  3. Yahoo technical support team understands the problems and easily operates the Yahoo mail problem and makes you out from your problem. The technical associates of Yahoo are fully devoted to develop new and innovative solutions to resolve different Yahoo mail problems.
    How to Reach Gmail Support

    ReplyDelete