Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Asp.Net email through gmail

I am trying to send an email via GMail from ASP.Net using the code and config below. Unfortunatly it doesn't seem to be working and it also isn't throwing an error message. There is nothing in the server logs or the mail IIS mail folders, I even checked the trash of the from address to see if the mail ended up there. Any help would be really appreciated.

C# Section

    public void SendFeedback()
    {
        string emailFrom = this.Email.Text;

        MailMessage message = new MailMessage();
        // here is an important part:
        message.From = new MailAddress(emailFrom, "Mailer");
        // it's superfluous part here since from address is defined in .config file
        // in my example. But since you don't use .config file, you will need it.
        message.Subject = "www.gumpshen.com - Website Query";
        message.IsBodyHtml = true;
        message.Body = string.Format(" Name = {0}, Phone = {1}", Name.Text, Phone.Text);
        message.Body += Environment.NewLine;
        message.Body += Environment.NewLine;
        message.Body += ProjectDetails.Text; ;

        var client = new SmtpClient();

        try
        {
            client.Send(message);

This is the Config section:

<system.net>
  <mailSettings>
    <smtp from="[email protected]" deliveryMethod="Network" >
      <network host="smtp.gmail.com" port="587" 
        userName="[email protected]" password="myPassword"/>
    </smtp>
  </mailSettings>
</system.net>
like image 320
Burt Avatar asked Dec 30 '10 00:12

Burt


1 Answers

You need client.EnableSsl=true;

Check the code from this site: Email via Gmail

Here is an example on how to send HTML email from your ASP.NET page using your Google account. (This setup can be easily used to send messages via any other SMTP server that requires authentication). Note: the code snippet assumes you have a Label component on Page with named lblMsg that will show success/failure message to the user that is sending email. (But this can be easily changed).

   SmtpClient client = new SmtpClient();
   client.DeliveryMethod = SmtpDeliveryMethod.Network;
   client.EnableSsl = true;
   client.Host = "smtp.gmail.com";
   client.Port = 587;

   // setup Smtp authentication
   System.Net.NetworkCredential credentials = 
       new System.Net.NetworkCredential("[email protected]", "yourpassword");
   client.UseDefaultCredentials = false;
   client.Credentials = credentials;                

   MailMessage msg = new MailMessage();
   msg.From = new MailAddress("[email protected]");
   msg.To.Add(new MailAddress("[email protected]"));

   msg.Subject = "This is a test Email subject";
   msg.IsBodyHtml = true;
   msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");

   try
   {
       client.Send(msg);
       lblMsg.Text = "Your message has been successfully sent.";
   }
   catch (Exception ex)
   {
       lblMsg.ForeColor = Color.Red;
       lblMsg.Text = "Error occured while sending your message." + ex.Message;
   }
like image 172
gbs Avatar answered Oct 14 '22 20:10

gbs