Send email using ASP.NET MVC

Asked

Viewed 3,368 times

2

How do I send email using ASP.NET MVC? Do you have an option to send without specifying SMTP similar to mail() of PHP?

  • I’m finding the question very broad, but I’m going to try to answer because they answer broad things like that. I can’t tell you which is best because it would only be my opinion. But can you at least say something more specific about what doubt is? What can you do? Or what have you done? Does it matter to be ASP.Net MVC? Why? Which one? Which one?

  • Your answer answers what I wanted.

2 answers

5

Yes. For example, the Sendgrid. I set up a Helper for him like this:

public static class SendGridHelper
{
    public static async Task EnviarEmail(String assunto, String mensagemHtml, String mensagemText)
    {
        // Cria o objeto de e-mail
        var myMessage = new SendGridMessage();

        // Remetente
        myMessage.From = new MailAddress("[email protected]");

        List<String> recipients = new List<String>
            {
                @"Cigano Morrison Mendez <[email protected]>"
            };

        myMessage.AddTo(recipients);
        myMessage.Subject = assunto;

        myMessage.Html = mensagemHtml;
        myMessage.Text = mensagemText;

        // Você pode mandar por credenciais...
        // var username = "usuario";
        // var pswd = "senha";
        // var credentials = new NetworkCredential(username, pswd);
        //var transportWeb = new Web(credentials);

        // ...ou por chave de API
        var transportWeb = new Web("MinhaChaveDeApi");

        // Finalmente, envia.
        await transportWeb.DeliverAsync(myMessage);
    }
}

O Sendgrid is free for up to 12 thousand emails. Has management panel and is quite resilient to spam.

5


One way that I find interesting is this:

public bool Mail(MailAddress to, MailAddress from, string sub, string body) {
    var me = new EmailBusiness();
    var m = new MailMessage() {
        Subject = sub,
        Body = body,
        IsBodyHtml = true
    };
    to = new MailAddress("endereç[email protected]", "Nome");
    m.To.Add(to);
    m.From = new MailAddress(from.ToString());
    m.Sender = to;
    var smtp = new SmtpClient {
        Host = "url.do.servidor",
        Port = 587,
        Credentials = new NetworkCredential("usuario", "senha"),
        EnableSsl = true
    };
    try {
        smtp.Send(m);
    } catch (Exception e) { //não faça isto, por favor, é só um exemplo
        return false
    }
    return true;
}

I put in the Github for future reference.

I found her in response in the OS and adapted. There is more information if you want to include in an ASP.NET MVC application. But I understand that the focus of the question is sending itself and not ASP.NET MVC. Obviously it can be improved.

The question and the other answer on the same page has a slightly different shape.

Note that the mail() PHP uses an SMTP server. There is no sending without one. Unless you write a code or take a library that is an SMTP server. In general this is not a good idea.

Browser other questions tagged

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