I have code based on MailKit. Where do I add the await keyword?
public async Task SendEmailAsync(string email, string subject, string mess)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "[email protected]"));
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "[email protected]"));
message.Subject = "How you doin'?";
message.Body = new TextPart("plain")
{
Text = @"Hey Chandler"
};
using (var client = new SmtpClient())
{
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("smtp.friends.com", 587, false);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
client.Authenticate("joey", "password");
client.Send(message);
client.Disconnect(true);
}
}
This is a similar implementation (but I couldn't find credential to make this work) so I changed to the above
public async Task SendEmailAsync(string email, string subject, string message)
{
using (var client = new HttpClient { BaseAddress = new Uri("smtp.gmail.com") })
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes("api:key-*")));
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("from", "postmaster@sandbox*.mailgun.org"),
new KeyValuePair<string, string>("to", email),
new KeyValuePair<string, string>("subject", subject),
new KeyValuePair<string, string>("text", message)
});
await client.PostAsync("sandbox*.mailgun.org/messages", content).ConfigureAwait(false);
}
}
If you are using .Net core and MailKit, you should reference the MailKit libraries and send via those. My MailKit class for sending email is as follows:
Interface