Last active
January 3, 2023 13:53
-
-
Save wattanar/e8cee974456fd80d9ce649d951f36608 to your computer and use it in GitHub Desktop.
C# Send Email With MailKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var message = new MimeMessage(); | |
var bodyBuilder = new BodyBuilder(); | |
// from | |
message.From.Add(new MailboxAddress("from_name", "from_email@example.com")); | |
// to | |
message.To.Add(new MailboxAddress("to_name", "to_email@example.com")); | |
// reply to | |
message.ReplyTo.Add(new MailboxAddress("reply_name", "reply_email@example.com")); | |
message.Subject = "subject"; | |
bodyBuilder.HtmlBody = "html body"; | |
message.Body = bodyBuilder.ToMessageBody(); | |
var client = new SmtpClient(); | |
client.ServerCertificateValidationCallback = (s, c, h, e) => true; | |
client.Connect("MAIL_SERVER", 465, SecureSocketOptions.SslOnConnect); | |
client.Authenticate("USERNAME", "PASSWORD"); | |
client.Send(message); | |
client.Disconnect(true); | |
} | |
} | |
} |
@vmakwana you can use non-SSL by set only host parameter like this client.Connect("sc.mail.com");
Update 2019 !! :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using false for useSsl in client.connect, still expected SSL/TLS certificate, this is an unexpected behavior, how can I use non-SSL???
`An error occurred while attempting to establish an SSL or TLS connection.
The SSL certificate presented by the server is not trusted by the system for one or more of the following reasons: