Send an email using System.Net.Mail via gmail

I want to send an email via gmail server. I put the following code, but it gets stuck while sending. Any idea please ....

MailMessage mail = new MailMessage(); mail.From = new System.Net.Mail.MailAddress("apps@xxxx.com"); //create instance of smtpclient SmtpClient smtp = new SmtpClient(); smtp.Port = 465; smtp.UseDefaultCredentials = true; smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; //recipient address mail.To.Add(new MailAddress("yyyy@xxxx.com")); //Formatted mail body mail.IsBodyHtml = true; string st = "Test"; mail.Body = st; smtp.Send(mail); 

xxxx.com is the mail domain in Google applications. Thank...

+46
c # email
Jan 13 2018-11-11T00:
source share
6 answers
 MailMessage mail = new MailMessage(); mail.From = new System.Net.Mail.MailAddress("apps@xxxx.com"); // The important part -- configuring the SMTP client SmtpClient smtp = new SmtpClient(); smtp.Port = 587; // [1] You can try with 465 also, I always used 587 and got success smtp.EnableSsl = true; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this smtp.UseDefaultCredentials = false; // [3] Changed this smtp.Credentials = new NetworkCredential(mail.From, "password_here"); // [4] Added this. Note, first parameter is NOT string. smtp.Host = "smtp.gmail.com"; //recipient address mail.To.Add(new MailAddress("yyyy@xxxx.com")); //Formatted mail body mail.IsBodyHtml = true; string st = "Test"; mail.Body = st; smtp.Send(mail); 
+70
Jan 13 '11 at 6:20
source share

I tried the above C # code to send mail from Gmail to my corporate id. At runtime, the application stopped indefinitely in the smtp.Send(mail); statement smtp.Send(mail);

While Googling, I came across a similar code that worked for me. I post this code here.

 class GMail { public void SendMail() { string pGmailEmail = "fromAddress@gmail.com"; string pGmailPassword = "GmailPassword"; string pTo = "ToAddress"; //abc@domain.com string pSubject = "Test From Gmail"; string pBody = "Body"; //Body MailFormat pFormat = MailFormat.Text; //Text Message string pAttachmentPath = string.Empty; //No Attachments System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage(); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com"); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465"); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusing", "2"); //sendusing: cdoSendUsingPort, value 2, for sending the message using //the network. //smtpauthenticate: Specifies the mechanism used when authenticating //to an SMTP //service over the network. Possible values are: //- cdoAnonymous, value 0. Do not authenticate. //- cdoBasic, value 1. Use basic clear-text authentication. //When using this option you have to provide the user name and password //through the sendusername and sendpassword fields. //- cdoNTLM, value 2. The current process security context is used to // authenticate with the service. myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //Use 0 for anonymous myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", pGmailEmail); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", pGmailPassword); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); myMail.From = pGmailEmail; myMail.To = pTo; myMail.Subject = pSubject; myMail.BodyFormat = pFormat; myMail.Body = pBody; if (pAttachmentPath.Trim() != "") { MailAttachment MyAttachment = new MailAttachment(pAttachmentPath); myMail.Attachments.Add(MyAttachment); myMail.Priority = System.Web.Mail.MailPriority.High; } SmtpMail.SmtpServer = "smtp.gmail.com:465"; SmtpMail.Send(myMail); } } 
+11
Jan 25 2018-11-11T00:
source share

Set smtp.UseDefaultCredentials = false and use smtp.Credentials = new NetworkCredential (gMailAccount, password);

+7
Jan 13 '11 at 6:16
source share

Use port number 587

With the following code, it will work successfully.

 MailMessage mail = new MailMessage(); mail.From = new MailAddress("abc@mydomain.com", "Enquiry"); mail.To.Add("abcdef@yahoo.com"); mail.IsBodyHtml = true; mail.Subject = "Registration"; mail.Body = "Some Text"; mail.Priority = MailPriority.High; SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); //smtp.UseDefaultCredentials = true; smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "<my gmail pwd>"); smtp.EnableSsl = true; //smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(mail); 

But there is a problem using gmail. The letter will be sent successfully, but the recipient's mailbox will have the gmail address in "from address" instead of "from address" mentioned in the code.

To resolve this issue, follow the steps in the following link.

http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html

Before you complete all of the above steps, you need to authenticate your gmail account to allow access to your application as well as devices. Check all account authentication steps at the following link:

http://karmic-development.blogspot.in/2013/11/allow-account-access-while-sending.html

0
Nov 01 '13 at 7:41
source share
  ActiveUp.Net.Mail.SmtpMessage smtpmsg = new ActiveUp.Net.Mail.SmtpMessage(); smtpmsg.From.Email = "abcd@test.com"; smtpmsg.To.Add(To); smtpmsg.Bcc.Add("vijay@indiagreat.com"); smtpmsg.Subject = Subject; smtpmsg.BodyText.Text = Body; smtpmsg.Send("mail.test.com", "abcd@sss.com", "user@1234", ActiveUp.Net.Mail.SaslMechanism.Login); 
0
Jun 12 '15 at 15:29
source share

This worked for me:

  MailMessage message = new MailMessage("myemail@gmail.com", toemail, subjectEmail, comments); message.IsBodyHtml = true; try { SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.Timeout = 2000; client.EnableSsl = true; client.DeliveryMethod = SmtpDeliveryMethod.Network; //client.Credentials = CredentialCache.DefaultNetworkCredentials; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassord"); client.Send(message); message.Dispose(); client.Dispose(); } catch (Exception ex) { Debug.WriteLine(ex.Message); } 

BUT (at the time of this writing, October 2017)

You need to enable "Allow less secure applications" in the "Applications with account access" option in the "My account" security settings for Google protection / privacy ( https://myaccount.google.com )

0
Oct 24 '17 at 20:37
source share



All Articles