Problem sending email using Smtpclient C#

Asked

Viewed 101 times

-1

I have a problem sending the email when I run:

objClientSmtp.Send(mensagem_email)

On the button +, "Non-public Members", there shows the property ClientSmtp.ServerSupportsEai with an Exception: "Undefined object reference for an object instance".

Where I define this object ?

Someone knows a solution ?

My code is 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);
                }

1 answer

1


in what part of your code you have prompted 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.