EDIT: This question is pointless, except as an exercise in red herrings. The issue turned out to be a combination of my idiocy (NO ONE was being emailed as the host was not being specified and was incorrect in web.config) and the users telling me that they sometimes got the emails and sometimes didn’t, when in reality they were NEVER getting the emails.
So, instead of taking proper steps to reproduce the problem in a controlled setting, I relied on user information and the “it works on my machine” mentality.
Good reminder to myself and anyone else out there who is sometimes an idiot.
I just hit something I think is inconsistent, and wanted to see if I’m doing something wrong, if I’m an idiot, or…
MailMessage msg = new MailMessage();
msg.To.Add("person1<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5b5919a98949c9bdb969a98">[email protected]</a>");
msg.To.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9d9ccdbdac6c79be9cdc6c4c8c0c787cac6c4">[email protected]</a>");
msg.To.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8f9a8d8c9091ccbf9b90929e9691d19c9092">[email protected]</a>");
msg.To.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a5a4f585945441e6a4e45474b434404494547">[email protected]</a>");
Really only sends this email to 1 person, the last one.
To add multiples I have to do this:
msg.To.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="38485d4a4b575609785c5755595156165b5755">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7c7d2c5c4d8d985f7d3d8dad6ded999d4d8da">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cecf9eeeff3f2afdcf8f3f1fdf5f2b2fff3f1">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98e8fdeaebf7f6acd8fcf7f5f9f1f6b6fbf7f5">[email protected]</a>");
I don’t get it. I thought I was adding multiple people to the To address collection, but what I was doing was replacing it.
I think I just realized my error — to add one item to the collection, use
.To.Add(new MailAddress(“[email protected]”))
If you use just a string, it replaces everything it had in its list.
EDIT: Other people have tested and are not seeing this behavior. This is either a bug in my particular version of the framework, or more likely, an idiot maneuver by me.
Ugh. I’d consider this a rather large gotcha! Since I answered my own question, but I think this is of value to have in the stackoverflow archive, I’ll still ask it. Maybe someone even has an idea of other traps that you can fall into.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I wasn’t able to replicate your bug:
var message = new MailMessage();
message.To.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96e3e5f3e4d6f3eef7fbe6faf3b8f5f9fb">[email protected]</a>");
message.To.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04717761763644617c65697468612a676b69">[email protected]</a>");
message.From = new MailAddress("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c4d5c3c4f0d5c8d1ddc0dcd59ed3dfdd">[email protected]</a>");
message.Subject = "Test";
message.Body = "Test";
var client = new SmtpClient("localhost", 25);
client.Send(message);
Dumping the contents of the To: MailAddressCollection:
MailAddressCollection (2 items)
DisplayName User Host Addressuser example.com [email protected]
user2 example.com [email protected]
And the resulting e-mail as caught by smtp4dev:
Received: from mycomputername (mycomputername [127.0.0.1])
by localhost (Eric Daugherty's C# Email Server)
3/8/2010 12:50:28 PM
MIME-Version: 1.0
From: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1d5c4d2d5e1c4d9c0ccd1cdc48fc2cecc">[email protected]</a>
To: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c696f796e5c79647d716c7079327f7371">[email protected]</a>, <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f184829483c3b19489909c819d94df929e9c">[email protected]</a>
Date: 8 Mar 2010 12:50:28 -0800
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Test
Are you sure there’s not some other issue going on with your code or SMTP server?
Method 2
private string FormatMultipleEmailAddresses(string emailAddresses)
{
var delimiters = new[] { ',', ';' };
var addresses = emailAddresses.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
return string.Join(",", addresses);
}
Now you can use it like
var mailMessage = new MailMessage();
mailMessage.To.Add(FormatMultipleEmailAddresses("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daaebfa9ae9abdb7bbb3b6f4b9b5b7">[email protected]</a>;<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5afaaadab85b7a0a1aca3a3eba6aaa8">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b1b190a18030a051f2b060a020745080406">[email protected]</a>"));
Method 3
You can do this either with multiple System.Net.Mail.MailAddress objects or you can provide a single string containing all of the addresses separated by commas
Method 4
You could try putting the e-mails into a comma-delimited string ("[email protected], [email protected]"):
C#:
ArrayList arEmails = new ArrayList();
arEmails.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9eeefbecedf1f0afdefaf1f3fff7f0b0fdf1f3">[email protected]</a>");
arEmails.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12627760617d7c2052767d7f737b7c3c717d7f">[email protected]</a>");
string strEmails = string.Join(", ", arEmails);
VB.NET if you’re interested:
Dim arEmails As New ArrayList
arEmails.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abdbced9d8c4c59aebcfc4c6cac2c585c8c4c6">[email protected]</a>")
arEmails.Add("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9feffaedecf0f1addffbf0f2fef6f1b1fcf0f2">[email protected]</a>")
Dim strEmails As String = String.Join(", ", arEmails)
Method 5
Add multiple System.MailAdress object to get what you want.
Method 6
Put in addresses this code:
objMessage.To.Add(***addresses:=***"[email protected] , [email protected] , [email protected]")
Method 7
Thanks for spotting this I was about to add strings thinking the same as you that they’d get added to end of collection. It appears the params are:
msg.to.Add(<MailAddress>) adds MailAddress to the end of the collection
msg.to.Add(<string>) add a list of emails to the collection
Slightly different actions depending on param type, I think this is pretty bad form i’d have prefered split methods AddStringList of something.
Method 8
I like the answer from Praveen, BUT, I had to adjust it somewhat to get it to work.
public class SendmailHelper
{
...
myMail.From = from;
string[] emails = FormatMultipleEmailAddresses(GlobalVariables.To_EMail);
int email_counter = 0;
while (email_counter < emails.Length)
{
myMail.To.Add(emails[email_counter]);
email_counter++;
}
...
{
public static string[] FormatMultipleEmailAddresses(string emailAddresses)
{
var delimiters = new[] { ',', ';' };
var addresses = emailAddresses.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
return addresses;
}
Method 9
I ran into a very similar error:
$to = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb9b8e99988485c584858eab8884869b8a8592c5888486">[email protected]</a>;<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5c4cbcad1cdc0d78bd5c0d7d6cacbe5c6cac8d5c4cbdc8bc6cac8">[email protected]</a>" $msg = New-Object Net.Mail.MailMessage($from, $to, $subject, $emailbody) New-Object: Exception calling ".ctor" with "4" argument(s): "An invalid character was found in the mail header: ';'."
When I changed the delimiter to a comma, it works fine:
$to = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41312433322e2f6f2e2f2401222e2c31202f386f222e2c">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94f5fafbe0fcf1e6bae4f1e6e7fbfad4f7fbf9e4f5faedbaf7fbf9">[email protected]</a>"
Method 10
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Web;
namespace HMS.HtmlHelper
{
public class SendmailHelper
{
//Created SendEMail method for sendiing mails to users
public bool SendEMail(string FromName, string ToAddress, string Subject, string Message)
{
bool valid =false;
try
{
string smtpUserName = System.Configuration.ConfigurationManager.AppSettings["smtpusername"].ToString();
string smtpPassword = System.Configuration.ConfigurationManager.AppSettings["smtppassword"].ToString();
MailMessage mail = new MailMessage();``
mail.From = new MailAddress(smtpUserName, FromName);
mail.Subject = Subject;
mail.To.Add(FormatMultipleEmailAddresses(ToAddress));
//mail.To.Add(ToAddress);
mail.Body = Message.ToString();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["smtpserverport"]);
smtp.Host = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"]; /
smtp.Credentials = new System.Net.NetworkCredential(smtpUserName, smtpPassword);
smtp.EnableSsl = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["ssl"]); ;
smtp.Send(mail);
valid = true;
}
catch (Exception ex)
{
valid =false ;
}
return valid;
}
public string FormatMultipleEmailAddresses(string emailAddresses)
{
var delimiters = new[] { ',', ';' };
var addresses = emailAddresses.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
return string.Join(",", addresses);
}
}
}``
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0