Send email via c#

Asked

Viewed 885 times

2

Good afternoon,

I’m needing to send email via c#, ai for testing, wanted to know what are outlook/Hotmail credentials to send emails

  • You don’t have the credentials?

  • What would be the credentials, my outlook email and password or credentials within smtp? Inside outlook I don’t have

  • Another thing, in php I know that mail only works hosted, in c# this rule is also maintained?

  • The correct smtp host would not be smtp-mail.outlook.com?

  • Leandro, the credentials is the user and password normally as if to log in through the microsoft client or google

2 answers

1

It follows a way of sending emails. The login and password you use to log in to your account is used as credentials.

 try
        {
            var smtp = new SmtpClient
            {
                Host = "smtp_aqui",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("seu_e_mail", "sua_senha")
            };
            using (var smtpMessage = new MailMessage("seu_e_mail", "e_mail_destino"))
            {
                smtpMessage.Subject = "assunto";
                smtpMessage.Body = "Corpo";
                smtpMessage.IsBodyHtml = false;
                smtp.Send(smtpMessage);
            }

        } catch (Exception ex)
        {
            //todo: add logging integration
            //throw;
        }
  • c# smtp works without hosting or has to be hosted the same in php?

  • 1

    I don’t know how it works in php. As an example using gmail smtp would look (smtp.gmail.com)

  • I say that the system needs to be hosted to work in php, I believe this also happens in c#

1

I use a free service (up to a certain limit of emails, but it’s high!) called Sendgrid (using Sendgrid’s Nuget). * I’m not advertising , I just like and recommend the service, it helps me a lot. And you don’t get tied up in your provider’s credentials, use the guys' API, and you can monitor the emails sent, the ones that gave delivery error etc!

and the code becomes much simpler:

Follows:

public static async Task Send(Email email)
{
    var apiKey = SendGridAPIKey;
    var client = new SendGridClient(apiKey);
    var from = new EmailAddress("[email protected]", "Name");
    var subject = email.Subject;
    var to = new EmailAddress(email.AddressTo, "User");
    var plainTextContent = email.Message;
    var htmlContent = email.Message;
    var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
    var response = await client.SendEmailAsync(msg);
}

Nuget pack: https://www.nuget.org/packages/Sendgrid/

Website: https://sendgrid.com/

  • 1

    You’re damn right I’m gonna take a look at him

  • Amigo, good morning, is giving error in aplkey Cannot Convert from 'Object' to 'Sendgrid.Sendgridoptions.'

  • Apikey is a String, which you should pick up on the Sendgrid site.

Browser other questions tagged

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