Problem sending email using SmtpClient C #

-1

I have a problem sending the email when I run:

objClientSmtp.Send(mensagem_email)

In the + , "Non-public members" button, there shows the ClientSmtp.ServerSupportsEai property with an exception: "Object reference not set to an instance of an object."

Where do I define this object?

Does anyone know a solution?

My code looks like this:

 string emailBody = "";
                string SmtpHost, SmtpPort, SmtpUser, SmtpPass, SmtpEmail;

                SmtpHost = System.Configuration.ConfigurationSettings.AppSettings["SMTP_Host"].ToString();
                SmtpPort = System.Configuration.ConfigurationSettings.AppSettings["SMTP_Port"].ToString();
                SmtpUser = System.Configuration.ConfigurationSettings.AppSettings["SMTP_User"].ToString();
                SmtpPass = System.Configuration.ConfigurationSettings.AppSettings["SMTP_Password"].ToString();
                SmtpEmail = System.Configuration.ConfigurationSettings.AppSettings["SMTP_Email"].ToString();


                PrjBureauVeritasIntranet.Classes.ClsLogin login = new PrjBureauVeritasIntranet.Classes.ClsLogin();
                login.USUARIO = usuario;

                PrjBureauVeritasIntranet.Classes.ClsSalas reserva = new PrjBureauVeritasIntranet.Classes.ClsSalas();

                if (login.getEmailUsuario(usuario) != "Sem email")
                {
                    System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
                    SmtpClient ClienteSmtp = new SmtpClient();

                    ClienteSmtp.Host = SmtpHost;
                    ClienteSmtp.Port = Convert.ToInt32(SmtpPort);
                    ClienteSmtp.Credentials = new NetworkCredential(SmtpUser, SmtpPass);

                    if (Request.QueryString["ssl"] != null)
                    {
                        ClienteSmtp.EnableSsl = true;
                    }

                    email.To.Add(login.getEmailUsuario(usuario));
                    email.From = new MailAddress(SmtpEmail);

                    email.Subject = "Confirmação de Reserva de Sala";
                    email.IsBodyHtml = true;

                    emailBody = login.getNomeUsuario(usuario);
                    emailBody += reserva.getMsgUltimaSalaReservada(usuario);

                    email.Body = emailBody;
                    ClienteSmtp.Send(email);
                }
    
asked by anonymous 03.06.2015 / 23:04

1 answer

1

In what part of your code did you instantiate this object?

try this:

var objClientSmtp = new SmtpClient(_host, _port); // sendo o host e a port que você vai usar

objClientSmtp.UseDefaultCredentials = false;

objClientSmtp.Credentials = new NetworkCredential(_user, _pass); // sendo user e pass suas credenciais

objClientSmtp.EnableSsl = true;

var mensagem_email = new MailMessage(_sender, _recipient, _subject, _body){ IsBodyHtml = true }; // sendo email que vai enviar, o que vai receber, o assunto, e o email em HTML

after that you send:

objClientSmtp.Send(mensagem_email);
    
03.06.2015 / 23:27