Hi @Fortecho Solutions Ltd , Welcome to Microsoft Q&A,
To send emails from a Microsoft account from a WinForms application or service, you can choose to use OAuth2 for authentication. OAuth2 is more secure than traditional usernames and passwords.
In the service, read the stored token and use it to authenticate and send emails:
public class EmailService
{
private static readonly string tokenCachePath = "token_cache.json";
public async Task SendEmailAsync(string to, string subject, string body)
{
var token = File.ReadAllText(tokenCachePath);
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Your Name", "your-email@example.com"));
message.To.Add(new MailboxAddress("", to));
message.Subject = subject;
message.Body = new TextPart("plain")
{
Text = body
};
using var client = new SmtpClient();
await client.ConnectAsync("smtp.office365.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
await client.AuthenticateAsync("your-email@example.com", token);
await client.SendAsync(message);
await client.DisconnectAsync(true);
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.