<p>Cela fonctionne pour moi (modifié à partir de la <a href="https://stackoverflow.com/questions/3408397/asynchronously-sending-emails-in-c">source</a>)</p>
<pre><code class="lang-auto"> ThreadPool.QueueUserWorkItem(t =>
{
SmtpClient client = new SmtpClient("smtp.office365.com",587);
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("xxx@yyy.com", "password");
MailAddress from = new MailAddress("xxx@yyy.com", String.Empty, System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("xxx@yyy.com");
MailMessage message = new MailMessage(from, to);
message.Body = "The message I want to send.";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "The subject of the email";
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, I am passing the message itself
client.SendAsync(message, message);
});
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the message we sent
MailMessage msg = (MailMessage)e.UserState;
if (e.Cancelled)
{
// prompt user with "send cancelled" message
}
if (e.Error != null)
{
// prompt user with error message
}
else
{
// prompt user with message sent!
// as we have the message object we can also display who the message
// was sent to etc
}
(Réponse tronquée)</code></pre>