1

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

nikhil mehta
  • 802
  • 11
  • 28

4 Answers4

1

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("xxxx@gmail.com", "xxxx");
Rendxx
  • 11
  • 1
0

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)

Setherith
  • 329
  • 2
  • 9
0

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("fromemail@email.com");
           msg.[To].Add("toemail@email.com");
           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("yourname@yourserver.com", "password");
           smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
           smtpClient.Send(msg);
           smtpClient.Dispose();
        }
er_jack
  • 122
  • 9
0

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

Community
  • 1
  • 1