0

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);
            }

        }
timmack
  • 590
  • 2
  • 12
  • 43

2 Answers2

2

Odds are high that the message ended up in the Spam folder.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • 1
    @jstedfast...it's not in the Spam or Trash folder either. I have already tried their alternative port from port 25 and still the same result. I have tried also using gmail smtp with port 465 and 587 and still the same result. Nothing came in the inbox or spam folders. – timmack Apr 09 '21 at 01:50
  • I tried sending from my own domain email service manually and it works fine but not through our email program in our website. – timmack Apr 09 '21 at 01:51
  • You can check the logs, but if there is no exception thrown by the SendAsync method, then the server is accepting the message. At that point, it’s in the hands of the smtp server. – jstedfast Apr 09 '21 at 02:54
0

Test your code by sending email to the very same address you are sending it from (myemail@mydomain.com). 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.

torvin
  • 6,515
  • 1
  • 37
  • 52