.Net 2.0 send email with multiple “To” recipients
Hi,
I know how to send emails in .Net 2.0, but I can not send one with multiple email recipients in the “to” field.
If possible I would like to specify display names for all recipients in the “to” field but this is not required.
[615 byte] By [
Buzz] at [2007-12-23]
Why can't you send to multiple TOs?
What is the problem you are having?
We definitely support this feature.
Use code something like
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("blah1"));
msg.To.Add(new MailAddress("blah2"));
msg.To.Add(new MailAddress("blah3"));
etc.
After reading Durga's post, if you are still having problems, please post your code. Also, if you are getting an exception, please post the full exception message.
Thanks, that fixed it.
The thing that confused me is you could specify multiple to address’s separated by “;” in .Net 1.1, but this wont work in 2.0
e.g.
MailMessage msg = new MailMessage(‘FromAddress’,’ToAddress1;ToAddress2’);
No your statement is not correct
The following code works great for me.
You should use , separated addresses, not ;
If you used ; it would have thrown an exception
Unhandled Exception: System.FormatException: The specified string is not in the form required for an e-mail address.
at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName)
at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset)
at System.Net.Mail.MailAddressCollection.ParseValue(String addresses)
at System.Net.Mail.MailAddressCollection.Add(String addresses)
at System.Net.Mail.Message..ctor(String from, String to)
at System.Net.Mail.MailMessage..ctor(String from, String to)
at Test.Main()
| |
using System; using System.Net.Mail; public class Test { public static void Main() { SmtpClient client = new SmtpClient("smtphost", 25); MailMessage msg = new MailMessage("x@y.com", "a@b.com,c@d.com"); msg.Subject = "sdfdsf"; msg.Body = "sdfsdfdsfd"; client.UseDefaultCredentials = true; client.Send(msg); } }
|
Hi, I am seeing the same exception too. Following is my code:
public
void SendMail()
{
MailAddress from = new MailAddress(address1@xxx.com,"name3");
MailAddress to1 = new MailAddress("address2@xxx.com;","name1");
MailAddress to2 = new MailAddress("address3@xxx.com","name2");
MailMessage EMessage = new MailMessage(from, to1);
EMessage.Body = this.G_EmailContent;
EMessage.IsBodyHtml = true;
EMessage.Subject = "subject1";
EMessage.To.Add(to1.Address);
EMessage.To.Add(to2.Address);
EMessage.From = from;
SmtpClient mailClient = new SmtpClient("server_name");
mailClient.UseDefaultCredentials = true;
mailClient.Send(EMessage);
}
exception:
Unhandled Exception: System.FormatException: The specified string is not in the form required for an e-mail address.
at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName)
at System.Net.Mail.MailAddress.ParseValue(String address)
at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
at System.Net.Mail.MailAddress..ctor(String address, String displayName)
at TechNetAutomation.EmailBuildReport.SendMail()
at TechNetAutomation.EmailBuildReport.RunReport()
at TechNetAutomation.RunAutomation.Main(String[] args)
MailAddress to1 = new MailAddress("address2@xxx.com;","name1");
MailAddress to2 = new MailAddress("address3@xxx.com","name2");Thats not the right way to add multiple recipients
use the example I used above.
PLUS you are using a ; at the end of the first mail address.
One thing that I've recently found regarding the comma thing is that in 1.1 as was mentioned above, to go to a list of addresses using on string, you used to separate your addresses by semi-colons.
i.e.
MailMessage myMail = new MailMessage();
myMail.To = "blah@blah.com; blah2@blah.com";
Now in 2.0
that would look like:
MailMessage myMail = new MailMessage();
myMail.To.Add("blah@blah.com, blah2@blah.com");
Hope that is a little more clear for anyone else looking for this.
ok i am trying to pass in email addresses sepertated by comma's into the toaddress like so... they are in a listbox1 on my page.
Dim emailgroup As String
emailgroup = ListBox1.SelectedValue.ToString
'Create a new MailMessage object and specify the "From" and "To" addresses Dim fromAddress As New Net.Mail.MailAddress(info@learnbartending.com, "Jeff ") Dim toAddress As New Net.Mail.MailAddress("emailgroup") Dim msg As New Net.Mail.MailMessage(fromAddress, toAddress) Dim Email As New System.Net.Mail.MailMessage(fromAddress, toAddress)First of all, I don't see anywhere that you are creating a comma delimited list of to addresses.
i.e.
string emailGroup = string.Empty;
ListBox.SelectedIndexCollection indexes = listBox1.SelectedIndices;
Net.Mail.MailAddress toAddresses;
//go through each selected item and pull back the value
foreach (int index in indexes)
{
emailGroup += (string) listBox1.Items[index] + ",";
}//removes the last comma
emailGroup = emailGroup.Remove(emailGroup.Length - 1);Secondly, when you create the email address, you need to pass the name of the variable not enclosed in quotes.
i.e.
toAddresses = new Net.Mail.MailAddress(emailGroup)
Please let me know if this doesn't solve your problem.
Disclaimer: the above loop would be made more efficient with a stringbuilder class. For sake of clairity I used what Jeff was using.
OK, so why didn't Microsoft:
1. Put this in the documentation? MailAddressCollection.Add(string) mentions NOTHING about the delimiter.
2. Simply leave the delimiter alone, or support both? What a pain in the neck!
I'm converting a VB.NET 1.1 app to 2.0 and the mail functions are giving me the most trouble with the lack of support for delimited addresses. The following line of code throws an invalid address exception for me:
Dim
objMessage As MailMessage = New MailMessage()Just realized this was posted last October. I can't believe this still hasn't been revised in the documentation!
This is the only thing that worked for me:
mailMessage.To.Add("<add1@mail.com>,<add2@mail.com>")
You may pass it as a string.
I had the same problem, rather than change all of my code, I changed my main mail routine as follows.
This works for me, let me know if you have any questions.
Public Function SendEMail(ByVal strFrom As String, ByVal strTo As String, ByVal strCC As String, ByVal strBcc As String, ByVal strSubject As String, ByVal strBody As String, Optional ByVal strAttachment As String = "", Optional ByVal strBodyType As String = "") As String
Try
Dim MailMsg As New MailMessage
MailMsg.From = New MailAddress(strFrom)
If strTo <> "" Then
Dim strToArray As String()
strToArray = strTo.Split(";")
Dim ToInt As Integer = strToArray.Length
Dim Tocount As Integer = 0
While Tocount < ToInt
MailMsg.To.Add(New MailAddress(strToArray(Tocount)))
Tocount = Tocount + 1
End While
Else
Return "error; No To Adress Specified"
End If
If strCC <> "" Then
Dim strccArray As String()
strccArray = strCC.Split(";")
Dim ccInt As Integer = strccArray.Length
Dim ccCount As Integer = 0
While ccCount < ccInt
MailMsg.CC.Add(New MailAddress(strccArray(ccCount)))
ccCount = ccCount + 1
End While
End If
If strBcc <> "" Then
Dim strBccArray As String()
strBccArray = strBcc.Split(";")
Dim BccInt As Integer = strBccArray.Length
Dim BccCount As Integer = 0
While BccCount < BccInt
MailMsg.Bcc.Add(New MailAddress(strBccArray(BccCount)))
BccCount = BccCount + 1
End While
End If
MailMsg.Subject = strSubject
MailMsg.Body = strBody
If strBodyType <> "" Then
Select Case LCase(strBodyType)
Case "html"
MailMsg.IsBodyHtml = True
Case "text"
MailMsg.IsBodyHtml = False
End Select
End If
If strAttachment <> "" Then
Dim strAttachmentArray As String()
strAttachmentArray = strAttachment.Split(";")
Dim AttachmentInt As Integer = strAttachmentArray.Length
Dim AttachmentCount As Integer = 0
While AttachmentCount < AttachmentInt
MailMsg.Attachments.Add(New Attachment(strAttachmentArray(AttachmentCount)))
AttachmentCount = AttachmentCount + 1
End While
End If
Dim MailServerName As String = "PutYourServerNameHere"
Dim mSmtpClient As New SmtpClient
mSmtpClient.Host = MailServerName
Try
mSmtpClient.Send(MailMsg)
Catch objException As Exception
SendNotificationMsg = objException.Message
End Try
mSmtpClient = Nothing
For Each aAttach As Attachment In MailMsg.Attachments
aAttach.Dispose()
Next
MailMsg.Attachments.Dispose()
MailMsg.Dispose()
MailMsg = Nothing
Catch ex As Exception
Return "error"
End Try
Return "Sent"
End Function