I have a verification email functionality in my .net core web application and I'm currently using my active domain's email service in my web host account. In .net core3.1, I'm using the MailKit and MimeKit as my dependencies in .net core to send an email. My code below seems to work fine it has no any errors but when I check the recepient's email inbox, nothing came in and no email was delivered. I can't figure out at the moment if I still missed anything. I'm hoping anybody could help me with this. Here are my codes below.
appsettings.json
"OpvUsersViewModel": {
"EmailId": "myemail@mydomain.com",
"UserName": "myemail@mydomain.com",
"UserPassword": "mypassword",
"SenderName": "Admin",
"Host": "mail.mydomain.com",
"Port": 25
},
Startup.cs
services.Configure<OpvUsersViewModel>(Configuration.GetSection("OpvUsersViewModel"));
Methods to send email:
private async void SendVerificationEmail(string recepientEmail, string message)
{
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(_mailSettings.EmailId));
email.To.Add(MailboxAddress.Parse(recepientEmail));
email.Subject = "CODE VERIFICATION";
email.Body = new TextPart(MimeKit.Text.TextFormat.Plain)
{ Text = message };
using (var smtp = new MailKit.Net.Smtp.SmtpClient())
{
await smtp.ConnectAsync(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(_mailSettings.UserName, _mailSettings.UserPassword);
await smtp.SendAsync(email);
await smtp.DisconnectAsync(true);
}
}