0

Hola estoy tratando de mandar un mail desde ASP.NET este es mi metodo

public ActionResult BolsaDeTrabajoContacto(string email_, string body, string nombre, string subject, HttpPostedFileBase files) {
  using(DBEnt db = new DBEnt()) {

    var cont = db.Contactos.ToList < Contactos > ().Where(u => u.tipo.Equals("mitipo")).FirstOrDefault();
    var dest = cont.destino;
    string path = "~/Content/Upload/";
    try {


      MailAddress from = new MailAddress("[email protected]");
      MailAddress to = new MailAddress("[email protected]");
      MailMessage mail = new MailMessage(from, to);


      mail.From = new MailAddress("[email protected]");
      //mail.To.Add("[email protected]");
      mail.Subject = nombre + " : " + subject;
      mail.Body = body;
      SmtpClient SmtpServer = new SmtpClient("pw.fs.com.mx", 587);

      SmtpServer.Port = 587;
      SmtpServer.Credentials = new NetworkCredential("[email protected]", "mi.pass.sh");
      SmtpServer.EnableSsl = true;
      SmtpServer.UseDefaultCredentials = false;

      if (files != null) {
        Directory.CreateDirectory(Server.MapPath(path));

        string filePath = Server.MapPath(path + files.FileName);
        files.SaveAs(filePath);

        mail.Attachments.Add(new Attachment(Server.MapPath(path + files.FileName)));

      }



      ServicePointManager.ServerCertificateValidationCallback = delegate(
        object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
        return true;
      };

      SmtpServer.Send(mail);
      //MessageBox.Show("mail Send");
    } catch (Exception ex) {
      //MessageBox.Show(ex.ToString());
      return Json("Error mandar mail " + ex.ToString());
    }

    return Json("Exito mandando mail ");
  }


}

este es mi error:

"Error mandar mail System.Net.Mail.SmtpFailedRecipientException: El buzón de correo no está disponible. La respuesta del servidor fue: SMTP AUTH is required for message submission on port 587\r\n en System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)\r\n en System.Net.Mail.SmtpClient.Send(MailMessage message)\r\n en WebFS.Controllers.HomeController.BolsaDeTrabajoContacto(String email_, String body, String nombre, String subject, HttpPostedFileBase files) en C:\Users\EjeDesarrolloCS-162\source\repos\WebFS\WebFS\Controllers\HomeController.cs:línea 555"

1
  • 2
    Por lo visto no esta reconociendo las credenciales, lo que no entiendo es porque usas pw.fs.com.mx para el smtp junto a un mail de gmail, el smtp de gmail es smtp.gmail.com el 7 feb. 2020 a las 3:28

1 respuesta 1

0

Yo para enviar por gmail, uso lo siguiente:

public static bool enviarEmailNormal(string destino, string asunto, string mensaje, bool esHtml, string cc = "", string bcc = "")
{
    bool resultado = false;

    try
    {
        if (!string.IsNullOrWhiteSpace(destino) && !string.IsNullOrWhiteSpace(asunto) && !string.IsNullOrWhiteSpace(mensaje))
        {
            var desde = new MailAddress(ConfigurationManager.AppSettings["smtpUsuario"].ToString(), ConfigurationManager.AppSettings["smtpNombreOrigen"].ToString());
            var para = new MailAddress(destino);
            string clave = ConfigurationManager.AppSettings["smtpClave"].ToString();

            var smtp = new SmtpClient
            {
                Host = ConfigurationManager.AppSettings["smtpServidor"].ToString(),
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(desde.Address, clave),
                Timeout = 20000
            };

            var message = new MailMessage();
            message.From = desde;
            message.To.Add(para);
            if (!string.IsNullOrWhiteSpace(cc))
            {
                var copia = new MailAddress(cc);
                message.To.Add(cc);
            }
            if (!string.IsNullOrWhiteSpace(bcc))
            {
                var copia = new MailAddress(bcc);
                message.Bcc.Add(bcc);
            }
            message.Subject = asunto;
            message.IsBodyHtml = esHtml;
            message.Body = mensaje;

            smtp.Send(message);
            resultado = true;
        }
        else
            resultado = false;
    }
    catch (Exception fallo)
    {
        resultado = false;
    }
    return resultado;
}

En el Web.config tengo definidas las siguientes variables

<add key="smtpServidor" value="smtp.gmail.com" />
<add key="smtpUsuario" value="[email protected]" />
<add key="smtpClave" value="tuclave" />
<add key="smtpNombreOrigen" value="NombreRemitente" />

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.