Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Sending email via .net core 3 using Worker Service

Sending email via .net core 3 using Worker Service

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharphtmlasp-netdotnetlinq
4 Posts 4 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ElenaRez
    wrote on last edited by
    #1

    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

    Z L Richard DeemingR 3 Replies Last reply
    0
    • E ElenaRez

      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

      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • E ElenaRez

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Google is quite restrictive on the use of its SMTP server. Use your debugger to check exactly what happens inside the sending method.

        1 Reply Last reply
        0
        • E ElenaRez

          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

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups