nikhil mehta
nikhil mehta

Reputation: 1032

Send email using asp.net and google smtp server

I have written following code in codebehind aspx page to send email.I want to use google smtp server. But some how I am not receiving the mails

                protected void btnSubmit_Click(object sender, EventArgs e)
                {              
                // Sender e-mail address.
                MailAddress From = new MailAddress(txtFrom.Text);
                // Recipient e-mail address.
                MailAddress To = new MailAddress(txtTo.Text);
                MailMessage Msg = new MailMessage(From,To);
                Msg.Subject = txtSubject.Text;
                Msg.Body = txtBody.Text;
                // your remote SMTP server IP.
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.Credentials = new System.Net.NetworkCredential
                 ("*******@gmail.com", "**********");
                client.EnableSsl = true;
                client.Port = 465;
                client.Send(Msg);
                client.Dispose();
                }

What wrong am I doing?Please help

Upvotes: 1

Views: 1612

Answers (4)

Rendxx
Rendxx

Reputation: 11

Here is what you need to do with the SmtpClient object:

     SmtpClient client = new SmtpClient();
     client.DeliveryMethod = SmtpDeliveryMethod.Network;
     client.EnableSsl = true;
     client.Host = "smtp.gmail.com";
     client.Port = 587;
     client.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxx");

Upvotes: 1

Erick CS
Erick CS

Reputation: 1

First of all, have you checked the webmail? In the sent folder? Many years ago I had the same problem, but I realized that my firewall was blocking me.

Other thing,

"SmtpClient class does not support Implicit SSL. It does support Explicit SSL, which requires an insecure connection to the SMTP server over port 25 in order to negotiate the transport level security (TLS)."

http://blog.ramsoftsolutions.com/2015/04/sending-mail-via-smtp-over-implicit-ssl.html

Source:

How can I send emails through SSL SMTP with the .NET Framework?

Regards

Upvotes: 0

er_jack
er_jack

Reputation: 112

Here is a generic email program.

It works on Port=25.

Remember gmail is an IMAP server.

  try 
  { 
           MailMessage msg = new MailMessage ();
           MailAddress fromAdd = new MailAddress("[email protected]");
           msg.[To].Add("[email protected]");
           msg.Subject = "Choose Session Members";
           msg.From = fromAdd;
           msg .IsBodyHtml = true;
           msg.Priority = MailPriority.Normal;
           msg .BodyEncoding = Encoding.Default;
           msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
           msg.Body = msg.Body + "</table></center>";
           SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
           smtpClient.EnableSsl = true;
           smtpClient.UseDefaultCredentials = false;
           smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
           smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
           smtpClient.Send(msg);
           smtpClient.Dispose();
        }

Upvotes: 0

Setherith
Setherith

Reputation: 329

Gmail uses OAuth 2.0 you may well have to supply some sort of API key to access their mail functions.

Accessing mail using IMAP and sending mail using SMTP is often done using existing IMAP and SMTP libraries for convenience. As long as these libraries support the Simple Authentication and Security Layer (SASL), they should be compatible with the SASL XOAUTH2 mechanism supported by Gmail.

In addition to the SASL XOAUTH2 protocol documentation, you may also want to read Using OAuth 2.0 to Access Google APIs for further information on implementing an OAuth 2.0 client.

The Libraries and Samples page provides code samples in a variety of popular languages using the SASL XOAUTH2 mechanism with either IMAP or SMTP. (source)

Upvotes: 0

Related Questions