sending gmail from ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danmoran
    New Member
    • May 2010
    • 14

    sending gmail from ASP.NET

    Can someone help me with the following code? I am trying to send out gmail from my asp.net webform. here is what I have so far. Thanks so much....
    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Net;
    using System.Net.Mail;
    
    public partial class home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        public bool SendMail(string gMailAccount, string password, string to, string subject, string message)
        {
            try
            {
                NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(gMailAccount);
                msg.To.Add(new MailAddress(to));
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = loginInfo;
                client.Send(msg);
    
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    
        protected void btnSend_Click(object sender, EventArgs e)
        {
    
            if (SendMail("my@gmail.com", "mypassword", "to@gmail.com", "This is a my subject", "This is my message"))
            {
                lblStatus.Text = "Sent";
    
            }
            else
            {
                lblStatus.Text = "Not Sent";
            }
        }
    }
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    What do you want us to do? You need to specify a question as we don't know what's happening. Do you get an error? On which line? Please elaborate.

    Steven

    Comment

    • danmoran
      New Member
      • May 2010
      • 14

      #3
      Hi Steven, well the code I have does not work. It doesn't give me any errors it runs but without sending the gmail. I don't know if there is anything wrong or why it doesn't send the gmail.

      thanks

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Have you tried sending the email on port 465?

        -Frinny

        Comment

        • danmoran
          New Member
          • May 2010
          • 14

          #5
          Hi Frinny, how do I go about sending the email on port 465? I am not familiar with the concept of ports... I am fairly new to ASP.NET and to web technologies.
          thanks

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            The Send method takes 1 or 2 parameters. You can specify the port to use to send the email on by passing it as the second parameter....

            -Frinny

            Comment

            • danmoran
              New Member
              • May 2010
              • 14

              #7
              Hi Frinny, great I got it working thanks alot for your help

              Comment

              Working...