1

I am looking for C# equivalent of mail.smtp.sendpartial property

http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html

If set to true, and a message has some valid and some invalid addresses, send the message anyway, reporting the partial failure with a SendFailedException. If set to false (the default), the message is not sent to any of the recipients if there is an invalid(mailbox doesnt exist on the server) recipient address.

public static void sendMails(string ptxtSubject, string ptxtBody)
{
    string txtTo = "valid1@aaa.com,valid2@aaa.com,invalid1@aaa.com";
    string txtFrom = "valid@aaa.com";
    string txtSubject = ptxtSubject;
    string txtBody = ptxtBody;

    MailMessage mail = new MailMessage();
    mail.To = txtTo;
    mail.From = txtFrom;
    mail.Subject = txtSubject;
    mail.Body = txtBody;

    try
    {
        SmtpMail.SmtpServer ="smtp.aaa.com";
        SmtpMail.Send(mail);
    }
    catch (Exception ex)
    {
       //log the exception
       throw;  
    }
}

This is the C# code I am using, but it fails to send email even if only one of the email addresses is invalid.

Google did not help,Any pointers are appreciated.

Aron
  • 107
  • 1
  • 6
  • what do you mean 'it fails'? does it throw an exception? if so - which one? or does it not send any of the emails? also, what do you mean by 'invalid email address'? do you mean formatted incorrectly, or there is no mail inbox associated on a server somewhere with that email account? – Mike Corcoran Dec 03 '12 at 21:05
  • Thanks for replying. It fails to send the email even to other valid email addresses. Invalid email address means there is no inbox on the server. – Aron Dec 03 '12 at 21:32

2 Answers2

2

Since .NET doesn't provide MailAddress.TryParse and the creation of new instance of MailAddress class with incorrect mail format raise exception, I recommend you to use:

http://cobisi.com/email-validation/.net-component

Another uglier option it's to try catch the creation/assignment of invalid mail addresses . We have it this way in our enterprise mail assembly because we don't wanna use third party assemblies and it is working okay.

Similar question that can help:

Check that email address is valid for System.Net.Mail.MailAddress

Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
1

The answer here depends really on what you mean by invalid.

From your example above, invalid1@aaa.com is actually a valid email address.

The only way right now in C# to make sure that the email address you want to use to send an email is in a valid format is to try to create a MailAddress instance by passing it the string address, and catching the FormatException, like this:

string email = "invalid1@aaa.com";
System.Net.Mail.MailAddress address = null;
try
{
    address = new MailAddress(email);
}
catch (FormatException fex)
{
    // do something about it here
}

I suspect what you want to know is whether an email inbox actually exists for invalid1@aaa.com, as in someone has an account with that address on a server that will be able to log in and see the message.

The short answer is that you can't verify that, and I would highly question the integrity of the response that the java class gives you if that is indeed what it tells you. If mail servers did this, it would be far too trivial for spammers to pound servers looking for random, valid email addresses.

If you wanted to get fancier, you could validate that the domain 'aaa.com' exists, but that's about as far as you can go to be sure.

Mike Corcoran
  • 14,072
  • 4
  • 37
  • 49