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(EmailMessage msg) { var message = new MimeMessage(); message.To.AddRange(msg.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address))); message.From.AddRange(msg.FromAddresses.Select(x => new MailboxAddress(x.Name, x.Address))); message.Subject = msg.Subject; message.Body = new TextPart("plain") { Text = msg.Content }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(_eConfig.SmtpServer, _eConfig.SmtpPort); client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate(_eConfig.SmtpUsername, _eConfig.SmtpPassword); client.Send(message); client.Disconnect(true); } }
public Task SendEmailAsync(string email, string subject, string htmlMessage) { try { var message = new MimeMessage(); message.From.Add(new MailboxAddress("Bus Application", "*****@*****.**")); message.To.Add(new MailboxAddress("Client", email)); message.Subject = subject; message.Body = new TextPart("html") { Text = htmlMessage }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.poczta.onet.pl", 465, true); client.Authenticate("*****@*****.**", "asdasDsdf4g#g&dsSdD3@sd@fsd@"); client.Send(message); client.Disconnect(true); } } catch (Exception ex) { } return(Task.CompletedTask); }
/// <summary> /// Method for sending email /// </summary> /// <param name="DisplayName">Display name of email</param> /// <param name="emailSubject">Subject of email</param> /// <param name="emailBody">Body of email</param> /// <param name="emailAddress">Receiver's email address</param> /// <returns>send Mail</returns> public async Task SendMail(string DisplayName, string emailSubject, string emailBody, string emailAddress) { try { var message = new MimeMessage(); message.From.Add(new MailboxAddress(DisplayName, _configuration["MailSetting:MailId"])); message.To.Add(MailboxAddress.Parse(emailAddress)); message.Subject = emailSubject; message.Body = new TextPart(TextFormat.Html) { Text = emailBody }; using var smtp = new MailKit.Net.Smtp.SmtpClient(); smtp.Connect(_configuration["MailSetting:Host"], int.Parse(_configuration["MailSetting:Port"]), SecureSocketOptions.StartTlsWhenAvailable); smtp.Authenticate(_configuration["MailSetting:Username"], _configuration["MailSetting:Password"]); smtp.ServerCertificateValidationCallback = (s, c, h, e) => true; await smtp.SendAsync(message); smtp.Disconnect(true); } catch (Exception exception) { Logger.Info(exception.Message, exception); } }
public async void Send(string toAddress, string subject, string body, bool sendAsync = true) { var mimeMessage = new MimeMessage(); // MIME : Multipurpose Internet Mail Extension mimeMessage.From.Add(new MailboxAddress(_fromAddressTitle, _fromAddress)); mimeMessage.To.Add(new MailboxAddress(toAddress)); mimeMessage.Subject = subject; var bodyBuilder = new MimeKit.BodyBuilder { HtmlBody = body }; IEnumerable <InternetAddress> ccList = new List <InternetAddress>(); foreach (var itemAppUser in _userDal.Users) { InternetAddress address = new MailboxAddress(itemAppUser.Email); mimeMessage.Cc.Add(address); } mimeMessage.Body = bodyBuilder.ToMessageBody(); using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(_smtpServer, _smtpPort, _enableSsl); client.Authenticate(_username, _password); // If using GMail this requires turning on LessSecureApps : https://myaccount.google.com/lesssecureapps if (sendAsync) { await client.SendAsync(mimeMessage); } else { client.Send(mimeMessage); } client.Disconnect(true); } }
public void SendEmail(string toEmail, string subject, string textMessage, Stream attachment = null, string attachmentName = null) { try { var settings = _context.Settings.FirstOrDefault(); var message = new MimeMessage(); message.From.Add(new MailboxAddress(settings.FromEmail)); message.To.Add(new MailboxAddress(toEmail)); message.Subject = subject; var bodyBuilder = new BodyBuilder { HtmlBody = textMessage, TextBody = textMessage }; message.Body = bodyBuilder.ToMessageBody(); if (!string.IsNullOrEmpty(attachmentName)) { bodyBuilder.Attachments.Add(attachmentName, attachment); } using var client = new MailKit.Net.Smtp.SmtpClient(); client.Connect(settings.SmtpHost, settings.SmtpPort, SecureSocketOptions.StartTls); client.Authenticate(settings.FromEmail, settings.FromEmailPassword); client.Send(message); client.Disconnect(true); } catch (Exception ex) { _logger.LogError("Error Sending Email", ex); } }
public async Task <bool> SendEmailAsync(SmtpConfig config, string toAddress, string toName, string subject, string messages) { try { var message = new MimeMessage(); message.From.Add(new MailboxAddress(config.FromName, config.FromAddress)); message.To.Add(new MailboxAddress(toName, toAddress)); message.Subject = subject; message.Body = new TextPart("plain") { Text = messages }; using var client = new MailKit.Net.Smtp.SmtpClient { ServerCertificateValidationCallback = (s, c, h, e) => true }; client.Connect(config.Server, config.Port, config.UseSsl); client.Authenticate(config.Username, config.Password); client.Disconnect(true); client.Send(message); await client.SendAsync(message); return(true); } catch (Exception) { return(false); } }
public IActionResult SendBroadcast(string title, string message) { var msg = new MimeMessage(); var user = from i in _AppDbContext.employees.OrderBy(x => x.Id) select i; foreach (var i in user) { var nama = i.Name; var emailAddress = i.Email; msg.From.Add(new MailboxAddress("HR", "*****@*****.**")); msg.To.Add(new MailboxAddress(nama, emailAddress)); msg.Subject = title; msg.Body = new TextPart("Plain") { Text = message }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect("smtp.mailtrap.io", 587, false); client.Authenticate("785fd04dea6d9c", "6057ae43ba12a4"); client.Send(msg); client.Disconnect(true); } } return(RedirectToAction("Send", "Broadcast")); }
public static void SendMail(SmtpConfig smtpConfig, string strContactName, string strContactEmail, string strContactMessage) { MimeKit.MimeMessage message = new MimeKit.MimeMessage(); message.From.Add(new MimeKit.MailboxAddress(strContactEmail, strContactEmail)); message.To.Add(new MimeKit.MailboxAddress("Noob", "*****@*****.**")); message.To.Add(new MimeKit.MailboxAddress("Noob2", "*****@*****.**")); message.Subject = "Neue Nachricht von www.daniel-steiger.ch"; message.Body = new MimeKit.TextPart("plain") { Text = strContactMessage }; string pw = CoinBaseSharp.StringHelper.ReverseGraphemeClusters(smtpConfig.Pass); using (MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient()) { // client.Connect("smtp.gmail.com", 587); ////Note: only needed if the SMTP server requires authentication // client.Authenticate("*****@*****.**", "mypassword"); client.Connect(smtpConfig.Server, smtpConfig.Port, false); client.Authenticate(smtpConfig.User, pw); client.Send(message); client.Disconnect(true); } }
protected void SendEmail(string status, Feedback feedback, Customer customer) { StringBuilder sb = new StringBuilder(); try { var bodyBuilder = new BodyBuilder(); var pathImage = Path.Combine(_hostingEnvironment.WebRootPath, "images/Texican_Logo_Blue.jpg"); var image = bodyBuilder.LinkedResources.Add(pathImage); image.ContentId = MimeUtils.GenerateMessageId(); bodyBuilder.HtmlBody = string.Format(@"<html><body><div style='text-align:center;'><img src='cid:{0}' style='width:60%;height:auto;' /></div><br /><br /><b>Dear {1}</b><br /><br />Thank you for providing us with your valuable feedback.<br /><br />Have a great day!<br /><br />Kind Regards<br />Texican</body></html>", image.ContentId, customer.Name); var message = new MimeMessage(); message.From.Add(new MailboxAddress("Texican", "*****@*****.**")); message.To.Add(new MailboxAddress(customer.Name, customer.Email)); message.Subject = "Texican Feedback Response"; message.Body = bodyBuilder.ToMessageBody(); MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient(); client.Connect("41.185.13.224", 587, false); client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate("*****@*****.**", "JrpcSolutions17"); client.Send(message); client.Disconnect(true); } catch (Exception ex) { } }
protected void SendMailTo(string status, Feedback feedback, Customer customer) { try { var bodyBuilder = new BodyBuilder(); var pathImage = Path.Combine(_hostingEnvironment.WebRootPath, "images/Texican_Logo_Blue.jpg"); var image = bodyBuilder.LinkedResources.Add(pathImage); image.ContentId = MimeUtils.GenerateMessageId(); bodyBuilder.HtmlBody = string.Format(@"<html><body><div style='text-align:center;'><img src='cid:{0}' style='width:60%;height:auto;' /></div><br /><br /><b>Dear {1}</b><br /><br />You have received a " + status + " review.<br /><br />Kind Regards<br />Texican</body></html>", image.ContentId, _context.Forms.FirstOrDefault(f => f.Form_ID == feedback.Form_ID).FormName); var message = new MimeMessage(); message.From.Add(new MailboxAddress("Texican", "*****@*****.**")); message.To.Add(new MailboxAddress("Rudi", "*****@*****.**")); message.Subject = "Texican Feedback: " + feedback.RefNo; message.Body = bodyBuilder.ToMessageBody(); MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient(); client.Connect("41.185.13.224", 587, false); client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate("*****@*****.**", "JrpcSolutions17"); client.Send(message); client.Disconnect(true); } catch (Exception ex) { } }
public void SendEmailCustom()//with mailkit { try { MimeMessage message = new MimeMessage(); message.From.Add(new MailboxAddress("Events Co", "*****@*****.**")); message.To.Add(new MailboxAddress("*****@*****.**")); message.Subject = "Message from Events Co"; message.Body = new BodyBuilder() { HtmlBody = "<div style=\"color:green;\"> Message with your email and pss for sign on web page</div>" }.ToMessageBody(); using (MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 465, true); client.Authenticate("*****@*****.**", "Abrakadabra2015"); client.Send(message); client.Disconnect(true); logger.LogInformation("Email sent succesfully"); } } catch (Exception e) { logger.LogError(e.GetBaseException().Message); } }
//currently not used private async Task <string> SendMail() { try { MimeMessage message = new MimeMessage(); MailboxAddress from = new MailboxAddress("Admin", "*****@*****.**"); MailboxAddress to = new MailboxAddress("toname", "to_email"); message.From.Add(from); message.To.Add(to); message.Subject = "Signity"; BodyBuilder bodyBuilder = new BodyBuilder(); bodyBuilder.HtmlBody = "<h1>Signity</h1>"; bodyBuilder.TextBody = ""; message.Body = bodyBuilder.ToMessageBody(); using (MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); client.Authenticate("*****@*****.**", "edwjfdgxqymujexb"); await client.SendAsync(message); await client.DisconnectAsync(true); } return("Email Send"); } catch (Exception) { throw; } }
public void SendMessage(MMailMessage message) { var smtpClient = new MailKit.Net.Smtp.SmtpClient(); if (options.IgnoreSSLError) { smtpClient.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; } SecureSocketOptions secOpts = SecureSocketOptions.Auto; if (!options.UseSSL) { secOpts = SecureSocketOptions.None; } smtpClient.Connect(options.SMTPServer, options.SMTPServerPort, secOpts); smtpClient.AuthenticationMechanisms.Remove("XOAUTH2"); if (options.Credentials != null) { smtpClient.Authenticate(options.Credentials); } smtpClient.Send(message); smtpClient.Disconnect(true); }
static void smtp_send(MimeMessage message) { // all our outgoing emails get sent from what's // configured in config file var from_address = new MailboxAddress( bd_config.get(bd_config.OutgoingEmailDisplayName), bd_config.get(bd_config.SmtpUser)); message.From.Add(from_address); if (bd_config.get(bd_config.DebugSkipSendingEmails) == 0) { using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect( bd_config.get(bd_config.SmtpHost), bd_config.get(bd_config.SmtpPort), MailKit.Security.SecureSocketOptions.Auto); string smtp_user = bd_config.get(bd_config.SmtpUser); string smtp_password = bd_config.get(bd_config.SmtpPassword); client.Authenticate(smtp_user, smtp_password); client.Send(message); client.Disconnect(true); } } }
public void SendEmailCustom(string email, string url) { try { MimeMessage message = new MimeMessage(); message.From.Add(new MailboxAddress("Confirm registration", "*****@*****.**")); message.To.Add(new MailboxAddress(email)); message.Subject = "Confirm registration"; message.Body = new BodyBuilder() { HtmlBody = "<div style=\"color: green;\">Confirm registration <a href=" + '"' + url + '"' + ">here</a> and enjoy</div>" }.ToMessageBody(); using (MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient()) { IConfigurationSection EmailAuthSection = Configuration.GetSection("Email:MailKit"); client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls); client.Authenticate(EmailAuthSection["Login"], EmailAuthSection["Password"]); client.Send(message); client.Disconnect(true); logger.LogInformation("Register confirmation email has been sent successfully"); } } catch (Exception e) { logger.LogError(e.GetBaseException().Message); } }
public EmailMessage NewSendMail(EmailMessage emailMessage) { //Using Mail Kit var message = new MimeMessage(); message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address))); message.From.AddRange(emailMessage.FromAddresses.Select(x => new MailboxAddress(x.Name, x.Address))); message.Subject = emailMessage.Subject; message.Body = new TextPart(TextFormat.Text) { Text = emailMessage.Content }; using (var emailClient = new MailKit.Net.Smtp.SmtpClient()) { emailClient.ServerCertificateValidationCallback = (s, c, ch, e) => true; emailClient.Connect(appSettings.SmtpServer, appSettings.SmtpPort, SecureSocketOptions.StartTls); emailClient.AuthenticationMechanisms.Remove("XOAUTH2"); emailClient.Authenticate(appSettings.Username, appSettings.Password); emailClient.Send(message); emailClient.Disconnect(true); return(emailMessage); } }
private async Task <string> EmailSendAsync(UsrEmail usrEmail) { try { MimeMessage message = new MimeMessage(); message.From.Add(new MailboxAddress(senderName, emailFrom)); message.To.Add(new MailboxAddress(emailTo)); message.Subject = emailSubject; message.Body = new BodyBuilder() { HtmlBody = $"<div><p>Новое сообщение от {usrEmail.Name}</p><p>{usrEmail.TextMessage}</p><p>адрес для ответа: {usrEmail.EMail}</p></div>" }.ToMessageBody(); MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient(); client.Connect(smtpServer, (int)port, (bool)useSSL); client.Authenticate(login, password); await client.SendAsync(message); client.Disconnect(true); return(null); } catch (Exception ex) { _logger.LogError(ex, "Fail send email"); return("Fail send email"); } }
private async Task Send(MimeMessage message, EmailNotificationSettings settings) { try { using (var client = new SmtpClient()) { client.Connect(settings.EmailHost, settings.EmailPort); // Let MailKit figure out the correct SecureSocketOptions. // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); if (settings.Authentication) { client.Authenticate(settings.EmailUsername, settings.EmailPassword); } Log.Info("sending message to {0} \r\n from: {1}\r\n Are we authenticated: {2}", message.To, message.From, client.IsAuthenticated); await client.SendAsync(message); await client.DisconnectAsync(true); } } catch (Exception e) { Log.Error(e); } }
public async Task SendEmailAsync(string email, string subject, string htmlMessage) { var message = new MimeMessage(); message.Sender = new MailboxAddress(mailSettings.DisplayName, mailSettings.Mail); message.From.Add(new MailboxAddress(mailSettings.DisplayName, mailSettings.Mail)); message.To.Add(MailboxAddress.Parse(email)); message.Subject = subject; var builder = new BodyBuilder(); builder.HtmlBody = htmlMessage; message.Body = builder.ToMessageBody(); // dùng SmtpClient của MailKit using var smtp = new MailKit.Net.Smtp.SmtpClient(); try { smtp.Connect(mailSettings.Host, mailSettings.Port, SecureSocketOptions.StartTls); smtp.Authenticate(mailSettings.Mail, mailSettings.Password); await smtp.SendAsync(message); } catch (Exception ex) { // Gửi mail thất bại, nội dung email sẽ lưu vào thư mục mailssave System.IO.Directory.CreateDirectory("mailssave"); var emailsavefile = string.Format(@"mailssave/{0}.eml", Guid.NewGuid()); await message.WriteToAsync(emailsavefile); logger.LogInformation("Lỗi gửi mail, lưu tại - " + emailsavefile); logger.LogError(ex.Message); } smtp.Disconnect(true); logger.LogInformation("send mail to: " + email); }
//string title, string msg, MailEntity mail = null /// <summary> /// 发送邮件 /// </summary> /// <param name="title"></param> /// <param name="msg"></param> /// <param name="mail"></param> /// <returns></returns> public async Task <bool> SendMail([FromBody] SendMailModel model) { try { if (model.MailInfo == null) { model.MailInfo = await GetMailInfo(); } var message = new MimeMessage(); message.From.Add(new MailboxAddress(model.MailInfo.MailFrom, model.MailInfo.MailFrom)); foreach (var mailTo in model.MailInfo.MailTo.Replace(";", ";").Replace(",", ";").Replace(",", ";").Split(';')) { message.To.Add(new MailboxAddress(mailTo, mailTo)); } message.Subject = string.Format(model.Title); message.Body = new TextPart("html") { Text = model.Content }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(model.MailInfo.MailHost, 465, true); client.Authenticate(model.MailInfo.MailFrom, model.MailInfo.MailPwd); client.Send(message); client.Disconnect(true); } return(true); } catch (System.Exception) { return(false); } }
public async Task MailKitSendAsync(string to, string subject, string message) { var mimeMessage = new MimeMessage(); // MIME : Multipurpose Internet Mail Extension mimeMessage.From.Add(new MailboxAddress(_fromAddressTitle, _fromAddress)); mimeMessage.To.Add(new MailboxAddress(to, to)); mimeMessage.Subject = subject; var bodyBuilder = new MimeKit.BodyBuilder { HtmlBody = message, }; mimeMessage.Body = bodyBuilder.ToMessageBody(); using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(_smtpServer, _smtpPort, _enableSsl); client.Authenticate(_username, _password); // If using GMail this requires turning on LessSecureApps : https://myaccount.google.com/lesssecureapps await client.SendAsync(mimeMessage); client.Disconnect(true); } }
public void SendEmail(byte[] content, params string[] emails) { foreach (var email in emails) { var message = new MimeMessage(); message.From.Add(new MailboxAddress("Стремный график", "*****@*****.**")); message.To.Add(new MailboxAddress(email)); message.Subject = "Твой график"; var builder = new BodyBuilder { HtmlBody = "<div style=\"color: green;\">Твой странный график ы</div>" }; builder.Attachments.Add("Your graph.jpeg", content); builder.Attachments.Add("тихон.jpg"); message.Body = builder .ToMessageBody(); using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect("smtp.gmail.com", 465, true); client.Authenticate("*****@*****.**", "tupyegraphiki"); client.Send(message); client.Disconnect(true); } } }
private static string SendBySMTP(MimeMessage message, Cuenta cuenta) { string res = "error"; using (var client = new MailKit.Net.Smtp.SmtpClient()) { // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(cuenta.ServCorreoSal, (int)cuenta.PuertoCorreoSal, 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(cuenta.Usuario, cuenta.Clave); client.Send(message); client.Disconnect(true); res = "ok"; } return(res); }
/// <summary> /// 发送邮件 /// </summary> /// <param name="title"></param> /// <param name="msg"></param> /// <param name="mail"></param> /// <returns></returns> public static void SendMail(SendMailModel model) { try { var message = new MimeMessage(); message.From.Add(new MailboxAddress(model.MailInfo.MailFrom, model.MailInfo.MailFrom)); foreach (var mailTo in model.MailInfo.MailTo.Replace(";", ";").Replace(",", ";").Replace(",", ";").Split(';')) { message.To.Add(new MailboxAddress(mailTo, mailTo)); } message.Subject = string.Format(model.Title); message.Body = new TextPart("html") { Text = model.Content }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(model.MailInfo.MailHost, 465, true); client.Authenticate(model.MailInfo.MailFrom, model.MailInfo.MailPwd); client.Send(message); client.Disconnect(true); } } catch (System.Exception) { } }
/// <summary> /// Sends an email based on the method-parameters /// </summary> /// <param name="toMail">Recipients emaila</param> /// <param name="fromMail">Senders email</param> /// <param name="senderName">senders name</param> /// <param name="recipientName">recipients name</param> /// <param name="subject">Email subject</param> /// <param name="bodyMessage">Email message</param> /// <param name="type">Enum message type: 1=New customer, 2=Forgot Password, 3=OrderConfirmation, 4=Contact Form</param> /// <returns>True if successful, else false</returns> private bool SendMail(string toMail, string fromMail, string senderName, string recipientName, string subject, string bodyMessage, string type = "text") { try { // Create a MailKit object var message = new MimeMessage(); message.From.Add(new MailboxAddress(senderName, fromMail)); message.To.Add(new MailboxAddress(recipientName, toMail)); message.Subject = subject; message.Body = new TextPart(type) { Text = bodyMessage }; // Send away... using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(_config["Mail:SMTP"], int.Parse(_config["Mail:Port"]), false); client.Authenticate(_config["Mail:Address"], _config["Mail:Password"]); client.Send(message); client.Disconnect(true); } } catch (Exception) { // Handle error here, log... return(false); } return(true); }
public void Send(EmailMessage emailMessage) { var message = new MimeMessage(); message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address))); message.From.AddRange(emailMessage.FromAddresses.Select(x => new MailboxAddress(x.Name, x.Address))); message.Subject = emailMessage.Subject; //We will say we are sending HTML. But there are options for plaintext etc. message.Body = new TextPart(TextFormat.Html) { Text = emailMessage.Content }; //Be careful that the SmtpClient class is the one from Mailkit not the framework! using (var emailClient = new MailKit.Net.Smtp.SmtpClient()) { //The last parameter here is to use SSL (Which you should!) emailClient.Connect(_emailConfiguration.SmtpServer, _emailConfiguration.SmtpPort, false); //Remove any OAuth functionality as we won't be using it. emailClient.AuthenticationMechanisms.Remove("XOAUTH2"); emailClient.Authenticate(_emailConfiguration.SmtpUsername, _emailConfiguration.SmtpPassword); emailClient.Send(message); emailClient.Disconnect(true); } }
public async Task SendEmailAsync(Message j) { try { var mes = new MimeMessage(); mes.From.Add(new MailboxAddress("*****@*****.**")); var adresses = j.To.Split(";"); foreach (var to in adresses) { mes.To.Add(new MailboxAddress(to.Trim())); } mes.Subject = j.Subject; mes.Body = new BodyBuilder() { TextBody = j.Body }.ToMessageBody(); using (MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 25, false); client.Authenticate("*****@*****.**", "svic..azen92276"); await client.SendAsync(mes); client.Disconnect(true); } } catch { } }
/// <inheritdoc/> public void SendEmail(Email email) { // First build the body BodyBuilder bodyBuilder = new BodyBuilder(); bodyBuilder.TextBody = email.TextContents.FirstOrDefault(text => text.MimeType == Constants.TextMimeType)?.RawText; bodyBuilder.HtmlBody = email.TextContents.FirstOrDefault(text => text.MimeType == Constants.HtmlMimeType)?.RawText; foreach (Interfaces.Models.Attachment attachment in email.Attachments) { bodyBuilder.Attachments.Add(GetAttachment(attachment)); } // Build the message MimeMessage mimeMessage = new MimeMessage(); mimeMessage.Subject = email.Subject; mimeMessage.Body = bodyBuilder.ToMessageBody(); mimeMessage.From.Add(GetInternetAddress(email.From)); foreach (MailAddress from in email.To) { mimeMessage.To.Add(GetInternetAddress(from)); } // Send the message using (SmtpClient smtpClient = new SmtpClient()) { smtpClient.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; smtpClient.Connect(Localhost, _options.SmtpPort); smtpClient.Send(mimeMessage); } }
private void SendMail() { string mailSubject = "終業打刻忘れ通知"; string mailBody = "終業打刻がされてないっぽいです"; var addrList = _smtpMailToRaw.Split(";"); var message = new MimeKit.MimeMessage(); message.From.Add(new MimeKit.MailboxAddress(_smtpAccount)); foreach (var addr in addrList) { message.To.Add(new MimeKit.MailboxAddress(addr.Trim())); } message.Subject = mailSubject; var textpart = new MimeKit.TextPart(MimeKit.Text.TextFormat.Plain); textpart.Text = mailBody; message.Body = textpart; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(this._smtpServer, this._smtpPort); client.Authenticate(_smtpAccount, this._smtpPass); client.Send(message); client.Disconnect(true); } }