Reputation: 590
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": "[email protected]",
"UserName": "[email protected]",
"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);
}
}
Upvotes: 0
Views: 2008
Reputation: 7101
Test your code by sending email to the very same address you are sending it from ([email protected]
). If this fails without any errors in C# - consider checking the logs to see where exactly it goes wrong.
If this test succeeds, but fails with a different email address - you should talk to the administrator of your mail.mydomain.com
about this.
Upvotes: 0