public void oldSend(SmtpMessage smtpMessage) { using (var emailClient = new MailKit.Net.Smtp.SmtpClient()) { try { emailClient.Connect("localhost", 0, MailKit.Security.SecureSocketOptions.None); //emailClient.Connect(_emailConfiguration.SmtpServer, _emailConfiguration.SmtpPort, false); } catch (MailKit.Security.SslHandshakeException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Client is connected: {0}", emailClient.IsConnected); emailClient.AuthenticationMechanisms.Remove("XOAUTH2"); emailClient.Authenticate(emailConfiguration.SmtpUsername, emailConfiguration.SmtpPassword); Console.WriteLine("Client is {0}", emailClient.IsConnected); emailClient.Send(smtpMessage.mimeMessage); emailClient.Disconnect(true); } }
public void Send(SmtpMessage message) { try { smtpClient.Send(message.mimeMessage); Console.WriteLine("Message sent"); } catch (Exception ex) { Console.WriteLine("Message send failed: {0}", ex.Message); } }
static void Main(string[] args) { //EmailMessage msg = new EmailMessage(); //msg.ToAddresses.Add(new EmailAddress() { Name = "haakon", Address = "*****@*****.**" }); //msg.FromAddresses.Add(new EmailAddress() { Name = "haakon", Address = "*****@*****.**" }); //msg.Subject = "Demo message"; SQLStuff sql = new SQLStuff(); var batch = sql.ctx.smtp_mail_batches.First(b => b.ID == Settings.Default.MailBatchId); var batchDetails = sql.ctx.smtp_mail_batch_details.First(bd => bd.batch_ID == batch.ID); var clientService = new SmtpClient(batch.smtp_server_host, batch.smtp_port.GetValueOrDefault(25), batch.smtp_user, batch.smtp_password); clientService.emailClient.Connect(); if (clientService.emailClient.IsConnected() == false) { Console.WriteLine("Client is not connected"); return; } clientService.emailClient.Authenticate(); if (clientService.emailClient.IsAuthenticated() == false) { Console.WriteLine("Client is not authenticated"); clientService.emailClient.Disconnect(); return; } var emails = sql.ctx.smtp_mail_details.Where(m => m.smtp_mail_batch_id == batch.ID); foreach (var email in emails) { var message = new SmtpMessage(); message.CreateMessage(batchDetails.mime_textpart, batchDetails.mime_htmlpart, email.mime_attachment_list); //create MimeMessage message.mimeMessage.Sender = new MimeKit.MailboxAddress(batchDetails.mime_sender_name, batchDetails.mime_sender); message.mimeMessage.To.Add(new MimeKit.MailboxAddress(email.mime_mail_to_name_list, email.mime_mail_to_list)); message.mimeMessage.From.Add(new MimeKit.MailboxAddress(batchDetails.mime_from_name_list, batchDetails.mime_from_list)); message.mimeMessage.Subject = batchDetails.mime_subject; //message.mimeMessage.Sender = new MimeKit.MailboxAddress("haakon", "*****@*****.**"); //message.mimeMessage.To.Add(new MimeKit.MailboxAddress("haakon", "*****@*****.**")); //message.mimeMessage.From.Add(new MimeKit.MailboxAddress("haakon", "*****@*****.**")); //message.mimeMessage.Subject = "Demo mail nr: " + ix; clientService.emailClient.Send(message); } clientService.emailClient.Disconnect(); }
static void Main(string[] args) { //EmailMessage msg = new EmailMessage(); //msg.ToAddresses.Add(new EmailAddress() { Name = "haakon", Address = "*****@*****.**" }); //msg.FromAddresses.Add(new EmailAddress() { Name = "haakon", Address = "*****@*****.**" }); //msg.Subject = "Demo message"; var clientService = new SmtpClient(); clientService.emailClient.Connect(); if (clientService.emailClient.IsConnected() == false) { Console.WriteLine("Client is not connected"); return; } clientService.emailClient.Authenticate(); if (clientService.emailClient.IsAuthenticated() == false) { Console.WriteLine("Client is not authenticated"); clientService.emailClient.Disconnect(); return; } for (int ix = 0; ix < 100; ix++) { var message = new SmtpMessage(); message.CreateMessage(); //create MimeMessage message.mimeMessage.Sender = new MimeKit.MailboxAddress("haakon", "*****@*****.**"); message.mimeMessage.To.Add(new MimeKit.MailboxAddress("haakon", "*****@*****.**")); message.mimeMessage.From.Add(new MimeKit.MailboxAddress("haakon", "*****@*****.**")); message.mimeMessage.Subject = "Demo mail nr: " + ix; clientService.emailClient.Send(message); } clientService.emailClient.Disconnect(); }