Read multiple posts about .net mail delivery notifications and tested multiple things:
- Trying to send mail via SMTP. No mails arrives and no exception error
- https://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientsexception.aspx
- How to check MailMessage was delivered in .NET?
- https://www.emailarchitect.net/easendmail/sdk/html/o_deliverynotificationoptions.htm
- http://forums.asp.net/p/1189023/2038523.aspx
Though I'm still stuck. What I want to accomplish is the following: I only want to know if the email-address which the mail is send to is valid, and not valid as in a valid format but valid as in: does the email address exist.
Sometimes a user will send an email to an address but by accident types 1 letter wrong, the mail is valid since it contains a domain and @ but the mail address doesn't exist. The sender will think it has been send correctly since they didn't receive any notification.
I want to make sure the sender gets the undelivered notification.
When using code to send mails through Outlook for example you automatically get a undelivered message if the mail address doesn't exists, however with SMTP (I am) not so lucky. I tried it with this bit of code:
using (var msg = new MailMessage())
using (var client = new SmtpClient("spamfilter.mySpamFilter.com", 587))
{
msg.IsBodyHtml = true;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
msg.From = new MailAddress(FromAddress);
msg.To.Add(TO);
msg.Body = Body;
msg.Subject = Subject;
//Send mail
try
{
client.Send(msg);
}
catch (SmtpFailedRecipientsException ex)
{
throw ex;
}
}
For the To
address I created an address that is 100% invalid. Though, the exception is never reached.
Can the spamfilter be the problem? For example, when I check the spamfilter I can see the messages have the refused
send status for the invalid email addresses but the action status
is allowed
(so no spam). So it looks like the spamfilter never returns the exception back to the code.
Or is it just not possible what I want to achieve?