What is wrong with WebMail.Send when it comes to multiple recipients?
Asked Answered
U

2

6

We've seen a few similar questions on StackOverflow before regarding System.Web.Helpers.Webmail.Send but I see no proper explanation for what's going on.

Regarding the to: parameter, the documentation says:

The email address of the recipient or recipients. Separate multiple recipients using a semicolon (;).

and I've seen answers saying "use a comma because the docs are wrong", or "use a semicolon", or "maybe it's an environment issue".

The code

WebMail.Send(
    to: "[email protected],[email protected]",
    from: "[email protected]",
    subject: "Some Automated Email",
    body: "<strong>Lorem Ipsum</strong>",
    isBodyHtml: true
);

I've tried a few scenarios:

[email protected];[email protected]

No emails recieved: An invalid character was found in the mail header: ';'.

[email protected]; [email protected]

Only the first recipient receives the email

[email protected],[email protected]

both recieved the email

[email protected], [email protected]

both recieved the email

[email protected], [email protected]

First recieved the email, but uncaught exception: Mailbox unavailable. The server response was: 5.7.1 Unable to relay

[email protected], [email protected]

No emails recieved: An invalid character was found in the mail header: ','.

Can anybody shed some light on this? I've actually had even more bizzare behaviour on a different server; I'm using Exchange for the above tests, but actually experienced different behaviour on hMailServer where [email protected],[email protected] resulted in a silent failure with no server errors and no outgoing mail in hMailServer logs. On the system with hMailServer I have only had success with a single address.

Unparalleled answered 29/8, 2013 at 16:30 Comment(0)
R
2

This probably has to do with the variety of relays you are connecting to, and the variety of methods they accept. Not only do the delimiter characters matter to each specific mail server, but the e-mail addresses also do (since different relays will be configure to accept certain e-mails, and will throw a variety of error codes back for bad e-mails, which will in turn throw a variety of exceptions).

The System.Net.Mail namespace has a MailMessage object that contains MailAddressCollection objects for To, CC, and Bcc that you can just call Add() on for each recipient.

I have a library that sends mail (without a relay) that uses it (everything goes to Bcc), you can check the code out there. If you happen to use the library, be sure to keep your IP address in good reputation and make sure your DNS records are all setup the same way you would if you were a relay (PTR and A records all setup).

Rob answered 29/8, 2013 at 17:8 Comment(0)
H
1

As I understand it, the mistake in the documentation is the likely scenario. I don't have this assembly, so I can't confirm it in ILSpy, but apparently the helper class simply uses System.Net.Mail. Following the four parameter overload through I get to this internal method.

internal Message(string from, string to) : this()
{
    //...
    this.to = new MailAddressCollection
    {
       to
    }
}

As a result, it simply creates a new MailAddressCollection which requires a comma delimiter. At no point did the to string ever replace or manipulate a semi-colon (unless this is done within the Helper class but that doesn't appear to be the case).

Haupt answered 29/8, 2013 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.