Skip to content

Instantly share code, notes, and snippets.

@grandsilence
Last active January 24, 2019 01:00
Show Gist options
  • Save grandsilence/e3f04ac8aa79c1ddcfd7d23a4f073635 to your computer and use it in GitHub Desktop.
Save grandsilence/e3f04ac8aa79c1ddcfd7d23a4f073635 to your computer and use it in GitHub Desktop.
C# Mail SMTP Client: Send Message async. Singleton.
using Leaf.Core.Extensions.Net;
//using Leaf.Core.Patterns;
public class MailClient // : Singleton<MailClient>
{
public static async Task SendAsync(string body, string subject = "Test Email")
{
SmtpClient client = null;
MailMessage mail = null;
try
{
client = new SmtpClient {
EnableSsl = Config.Smtp.Secure,
Port = Config.Smtp.Port, // 465 обычно, но для mail ru надо использовать 25, ибо это mail.ru
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Host = Config.Smtp.Server,
Timeout = 7 * 1000,
Credentials = new NetworkCredential(Config.Smtp.Email, Config.Smtp.Password)
};
mail = new MailMessage(Config.Smtp.Email, Config.Smtp.EmailTo) {
Subject = subject,
Body = body
};
await client.SendMailAsync(mail, MainUI.Instance.CancelToken);
}
finally
{
client?.Dispose();
mail?.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment