Reputation: 1742
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: "joe.bloggs@mail.com,jane.bloggs@mail.com",
from: "no-reply@company.com",
subject: "Some Automated Email",
body: "<strong>Lorem Ipsum</strong>",
isBodyHtml: true
);
I've tried a few scenarios:
joe.bloggs@mail.com;jane.bloggs@mail.com
No emails recieved: An invalid character was found in the mail header: ';'.
joe.bloggs@mail.com; jane.bloggs@mail.com
Only the first recipient receives the email
joe.bloggs@mail.com,jane.bloggs@mail.com
both recieved the email
joe.bloggs@mail.com, jane.bloggs@mail.com
both recieved the email
joe.bloggs@mail.com, non-existant@mail.com
First recieved the email, but uncaught exception: Mailbox unavailable. The server response was: 5.7.1 Unable to relay
non-existant@mail.com, joe.bloggs@mail.com
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 joe.bloggs@mail.com,jane.bloggs@mail.com
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.
Upvotes: 6
Views: 2201
Reputation: 69372
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).
Upvotes: 1
Reputation: 229
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).
Upvotes: 2