sending email by smtp

0

I made a class that has the following code to send an email:

try
        {
            WebMail.SmtpServer = "smtp-mail.outlook.com";
            WebMail.SmtpPort = 25;
            WebMail.EnableSsl = true;
            WebMail.UserName = "meuemail";
            WebMail.Password = "minhasenha";
            WebMail.From = "[email protected]";

            WebMail.Send("[email protected]", "Notificação",
                Model.Nome + " é " + ((Model.VaiParticipar ?? false) ? "" : "Não") + "Sim");
        }
        catch (Exception)
        {
           @:<b>Desculpe, Email não enviado</b>
        }

Is anything else missing? It is always falling in catch , the email is not sent.

    
asked by anonymous 30.06.2016 / 06:40

3 answers

5

You should change the port (SmtpPort) from 25 to 587.

The Internet Governance Committee in Brazil (CGI.br) has determined that as of January 1, 2013, all access providers and telephone companies will no longer allow the sending of e-mails through port 25.

This means that all users who use email clients such as Outlook, Windows Mail, Thunderbird or Apple mail among others should change their SMTP port from 25 to 587. This practice is intended to reduce spam traffic in Brazil and consequently to an improvement of the reputation of the IPs of Brazil in CBL (list of block that adds IP addresses that proved to have sent spam).

I suggest you read an interesting article on a simpler way to write your mailing class at link

    
01.07.2016 / 16:51
0

Change your source to catch the error message and understand where is the problem according to the modification below, something else, check the configuration of port and ssl, because from what I have used, there are rarely any servers using the port 25 today:

    }
    catch (Exception **erro**)
    {
       @:<b>Desculpe, Email não enviado</b> @:erro.Message
    }
    
30.06.2016 / 16:49
0

Try to use this ready function that I use and does not give an error, only if it is your user's problem.

    public class EmailServerAccount
    {
        public string EmailOrigem { get; set; }
        public string NomeOrigem { get; set; }
        public string Server { get; set; }
        public int Port { get; set; }
        public string User { get; set; }
        public string Pass { get; set; }
        public string Retorno { get; set; }
        public Boolean Autentica { get; set; }

    }

    public static string EnviarMensagem(EmailServerAccount conta, string[] destino, string[] emailcc, string mensagem, string titulo, string anexo)
    {
        string para = destino[0];

        if (String.IsNullOrEmpty(para)) 
        {
            return "Erro sem e-mail ! Assunto:" + titulo;
        }

        if (conta == null)
            return "Erro, conta de e-mail não existente !";

        MailMessage message = new MailMessage();
        message.From = new MailAddress(conta.EmailOrigem, conta.NomeOrigem);
        message.ReplyToList.Add(new MailAddress(conta.Retorno));

        string[] emaildestino = para.Split(';');
        //Destinatário
        foreach (string vEmailP in emaildestino)
        {
            message.To.Add(new MailAddress(vEmailP));
        }

        // message.To.Add(new MailAddress(""));

        //prioridade do email
        message.Priority = MailPriority.Normal;

        //utilize true pra ativar html no conteúdo do email, ou false, para somente texto
        message.IsBodyHtml = true;

        //Assunto do email
        message.Subject = titulo;

        //corpo do email a ser enviado
        message.Body = mensagem;

        // Envia a mensagem
        SmtpClient client = new SmtpClient(conta.Server, conta.Port);

        Boolean ssl = conta.Autentica;
        client.EnableSsl = ssl;

        // Insere as credenciais se o Servidor SMTP exigir
        ///  client.Credentials = CredentialCache.DefaultNetworkCredentials;

        //endereço do servidor SMTP(para mais detalhes leia abaixo do código)
        client.Host = conta.Server;

        //para envio de email autenticado, coloque login e senha de seu servidor de email
        //para detalhes leia abaixo do código
        client.Credentials = new NetworkCredential(conta.EmailOrigem, conta.Pass);

        try
        {
            client.Send(message);
            return "";
        }
        catch (Exception ex)
        {
            return " Erro no envio de email para !  " + para + "\r\n" + " " + ex.Message + "      -       " + ex.StackTrace + System.Environment.NewLine;
        }
    }
    
01.07.2016 / 16:25