Foros del Web » Programación para mayores de 30 ;) » .NET »

Enviar email con c# .net

Estas en el tema de Enviar email con c# .net en el foro de .NET en Foros del Web. Hola de nuevo a todos. Estoy intentando enviar email usando una cuenta de gmail, en una aplicacion web desarrollada en c#. He seguido los pasos ...
  #1 (permalink)  
Antiguo 05/06/2009, 11:11
 
Fecha de Ingreso: enero-2008
Mensajes: 27
Antigüedad: 16 años, 2 meses
Puntos: 0
Enviar email con c# .net

Hola de nuevo a todos.

Estoy intentando enviar email usando una cuenta de gmail, en una aplicacion web desarrollada en c#. He seguido los pasos que indican en muchos tutoriales, y al enviarlo me da el siguiente error siempre:

"No es posible conectar con el servidor remoto".

A alguien le ha pasado o sabe de que puede ser? He definido el servidor smtp de gmail (smtp.gmail.com), el puerto (587 y 465 probé tambien), active SSL, ...
A ver si alguien me puede ayudar.

Gracias!
  #2 (permalink)  
Antiguo 05/06/2009, 13:06
 
Fecha de Ingreso: junio-2009
Mensajes: 1
Antigüedad: 14 años, 10 meses
Puntos: 0
Respuesta: Enviar email con c# .net

Hola te envio un trozo de codigo que use para enviar mails con c# y me funciona bien. Fijate
------------------------------------------------------------------------------------------

using System.Net.Mail;
.
.
.

try
{

SmtpClient smtpMail;

//---Verificar servidor smtp y puertos
if (port != string.Empty)
{
smtpMail = new SmtpClient(smptAddress, Int32.Parse(port));
}
else
{
smtpMail = new SmtpClient(smptAddress);
}
//----------------------
MailAddress _from = new MailAddress(from, "TU_ASUNTO");
MailMessage _mensaje = new MailMessage();

_mensaje.Body = cuerpoMensaje;
_mensaje.Subject = Asunto;
_mensaje.From = _from;
foreach (String unDestino in listaMensaje)
{

_mensaje.To.Add(unDestino);
}

//--------------
if (EnableSsl.ToUpper().Equals("TRUE"))
smtpMail.EnableSsl = true;
//else

//------------Verficar Credenciales
if (user == string.Empty || password == string.Empty)
smtpMail.UseDefaultCredentials = true;
else
smtpMail.Credentials = new System.Net.NetworkCredential(user, password);
//-----------------------------------------------------


//Enviar e-mail
smtpMail.Send(_mensaje);

}
catch (Exception err)
{
Console.WriteLine("ERROR AL ENVIAR MAIL: "+err.Message);

}
  #3 (permalink)  
Antiguo 05/06/2009, 13:41
Avatar de freegirl
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: Catalonia
Mensajes: 4.334
Antigüedad: 20 años, 6 meses
Puntos: 156
Respuesta: Enviar email con c# .net

Hola

aquí tienes ejemplos de como enviar mails usando GMAIl con .NET:

Cita:
using System.Web.Mail;
using System;
public class MailSender
{
public static bool SendEmail(
string pGmailEmail,
string pGmailPassword,
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
try
{
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;
}

System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}


//Example usage of this class

EmailLib.SendEmail.SendEmail("your_gmail_id",
"your_gmail_password",
"[email protected]",
"This is email subject" ,
"This is email body",
Web.Mail.MailFormat.Text,
"Physical path to your Attachment")
FUENTES:

http://www.shabdar.org/send-email-us...et-csharp.html
http://snipplr.com/view/5035/send-em...-aspnet-and-c/
http://www.vbcode.com/Asp/showsn.asp?theID=12995

Y la verdad es que hay bastantes más en Google.

Espero que te sirva alguno de los anteriores.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:47.