Nuestro grupo especializado despúes de algunos días de trabajo y de recopilar de datos, hemos dado con la solución, esperamos que te sea útil para tu trabajo.
Solución:
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("[email protected]");
// 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("[email protected]"));
//Formatted mail body
mail.IsBodyHtml = true;
string st = "Test";
mail.Body = st;
smtp.Send(mail);
Probé el código C# anterior para enviar correo desde Gmail a mi identificación corporativa. Mientras ejecutaba la aplicación, el control se detuvo indefinidamente en la declaración smtp.Send(mail);
Mientras buscaba en Google, encontré un código similar, que funcionó para mí. Estoy publicando ese código aquí.
class GMail
public void SendMail()
string pGmailEmail = "[email protected]";
string pGmailPassword = "GmailPassword";
string pTo = "ToAddress"; //[email protected]
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);
Establecer smtp.UseDefaultCredentials = false y use smtp.Credentials = new NetworkCredential(gMailAccount, contraseña);
Sección de Reseñas y Valoraciones
Al final de la post puedes encontrar las aclaraciones de otros programadores, tú además tienes la libertad de mostrar el tuyo si lo crees conveniente.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)