Sending email via .net core 3 using Worker Service
-
Hi. I want to implement a sending email service using .net core worker service. For doing that I use .net core 3 Worker service and in Worker class and I added NetCore.MailKit 2.0.2 to my project. I added the below lines of code: using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Mail; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.VisualBasic; using MimeKit; using MimeKit.Text; namespace MyWorkerService { public class Worker : BackgroundService { private readonly ILogger _logger; private HttpClient client; public Worker(ILogger logger) { _logger = logger; } public override async Task StartAsync(CancellationToken cancellationToken) { var message = new MimeMessage(); message.To.Add(new MailboxAddress("Mike", "mike@gmail.com")); message.From.Add(new MailboxAddress("Elen", "elen@gmail.com")); message.Subject = "Hi"; message.Body = new TextPart(TextFormat.Html) { Text = "Email for testing" }; //----------------------------------------------------------- using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); //SMTP server authentication if needed client.Authenticate("elen@gmail.com", "fava"); client.Send(message); client.Disconnect(true); } } public override async Task StopAsync(CancellationToken cancellationToken) { // DO YOUR STUFF HERE await base.StopAsync(cancellationToken); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } } } } And the program class is as follows: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespa
-
Hi. I want to implement a sending email service using .net core worker service. For doing that I use .net core 3 Worker service and in Worker class and I added NetCore.MailKit 2.0.2 to my project. I added the below lines of code: using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Mail; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.VisualBasic; using MimeKit; using MimeKit.Text; namespace MyWorkerService { public class Worker : BackgroundService { private readonly ILogger _logger; private HttpClient client; public Worker(ILogger logger) { _logger = logger; } public override async Task StartAsync(CancellationToken cancellationToken) { var message = new MimeMessage(); message.To.Add(new MailboxAddress("Mike", "mike@gmail.com")); message.From.Add(new MailboxAddress("Elen", "elen@gmail.com")); message.Subject = "Hi"; message.Body = new TextPart(TextFormat.Html) { Text = "Email for testing" }; //----------------------------------------------------------- using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); //SMTP server authentication if needed client.Authenticate("elen@gmail.com", "fava"); client.Send(message); client.Disconnect(true); } } public override async Task StopAsync(CancellationToken cancellationToken) { // DO YOUR STUFF HERE await base.StopAsync(cancellationToken); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } } } } And the program class is as follows: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespa
First, debug it and make sure the code works as is. Secondly, add logging so you can see what is happening.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
Hi. I want to implement a sending email service using .net core worker service. For doing that I use .net core 3 Worker service and in Worker class and I added NetCore.MailKit 2.0.2 to my project. I added the below lines of code: using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Mail; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.VisualBasic; using MimeKit; using MimeKit.Text; namespace MyWorkerService { public class Worker : BackgroundService { private readonly ILogger _logger; private HttpClient client; public Worker(ILogger logger) { _logger = logger; } public override async Task StartAsync(CancellationToken cancellationToken) { var message = new MimeMessage(); message.To.Add(new MailboxAddress("Mike", "mike@gmail.com")); message.From.Add(new MailboxAddress("Elen", "elen@gmail.com")); message.Subject = "Hi"; message.Body = new TextPart(TextFormat.Html) { Text = "Email for testing" }; //----------------------------------------------------------- using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); //SMTP server authentication if needed client.Authenticate("elen@gmail.com", "fava"); client.Send(message); client.Disconnect(true); } } public override async Task StopAsync(CancellationToken cancellationToken) { // DO YOUR STUFF HERE await base.StopAsync(cancellationToken); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } } } } And the program class is as follows: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespa
-
Hi. I want to implement a sending email service using .net core worker service. For doing that I use .net core 3 Worker service and in Worker class and I added NetCore.MailKit 2.0.2 to my project. I added the below lines of code: using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Mail; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.VisualBasic; using MimeKit; using MimeKit.Text; namespace MyWorkerService { public class Worker : BackgroundService { private readonly ILogger _logger; private HttpClient client; public Worker(ILogger logger) { _logger = logger; } public override async Task StartAsync(CancellationToken cancellationToken) { var message = new MimeMessage(); message.To.Add(new MailboxAddress("Mike", "mike@gmail.com")); message.From.Add(new MailboxAddress("Elen", "elen@gmail.com")); message.Subject = "Hi"; message.Body = new TextPart(TextFormat.Html) { Text = "Email for testing" }; //----------------------------------------------------------- using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); //SMTP server authentication if needed client.Authenticate("elen@gmail.com", "fava"); client.Send(message); client.Disconnect(true); } } public override async Task StopAsync(CancellationToken cancellationToken) { // DO YOUR STUFF HERE await base.StopAsync(cancellationToken); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } } } } And the program class is as follows: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespa
NB: I hope that's not your real GMail username and password that you've just posted to a public forum? :doh: If it is, you should change your password immediately, and review your account for any suspicious activity. But I would hope that Google wouldn't let you use such an insecure password.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer