MailAddress string format

  • Thread starter Thread starter gdodd10
  • Start date Start date
G

gdodd10

Hello,
I am putting email function into a web app I am making using C#. I am
struggling to find out the proper format for the string of To email
addresses. I want to put multiple addresses in the string, but
everything I try does not work.
Here is my code:
protected void SendEmail_Click(object sender, EventArgs e)
{
string emailSubject = "Test Subject";
string emailTo = "emailaddress1;emailaddress2";
string emailFrom = "emailaddress";
string emailBody = "Test Email";
string SMTPServer = "localhost";

this.SendReminderEmail(this.Context, emailSubject, emailTo,
emailFrom, emailBody, SMTPServer);
}

Obviously I will replace "emailaddress1" with a real email address, I
just left out the email addresses for security reasons. I have tried
putting semicolon with no space, semicolon with a space, comma with no
space, and comma with a space, to divide the email addresses. Nothing
works.
Thanks.
Greg
 
What is it that isn't working? Do you get an error, does it only send
to one or the other, or does it not send at all?
 
This is the error I get:

The specified string is not in the form required for an e-mail address.

I just am trying to find the proper format for inserting multiple email
addresses in the code. When the administrator clicks a button, it is
supposed to send out an email. It works just fine when I have one
email address in there, but when I try to put more than one, it does
not work. I figure there has to be a way to do more than one email
address, I just can't get the format right.
Thanks
 
The System.Net.MailMessage class has a To property which is a
collection of MailAddresses ( MailAddressCollection). To send an email
to multiple recipients you have to add a MailAddress to that
collection. Here is some sample code:

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("(e-mail address removed)"));
msg.To.Add(new MailAddress("(e-mail address removed)"));
.....
.....

Obviously this would have to be programed in your SendReminderEmail
method. What you can do is have the emailTo parameter to be an array
of stings and then inside your method do a foreach on this string array
and then add the address to the collection like I shown above.

I hope this helps
 
It looks like foreach loops can not work with objects of type
MailMessage. Do you have another route you might suggest going?
 
string emailTo = "emailaddress1;emailaddress2";

Obviously I will replace "emailaddress1" with a real email address, I
just left out the email addresses for security reasons. I have tried
putting semicolon with no space, semicolon with a space, comma with no
space, and comma with a space, to divide the email addresses. Nothing
works.

For me with SmtpClient semi-colons worked for me in 1.1 and commas worked in
2.0.
 
Hi,

Obviously I will replace "emailaddress1" with a real email address, I
just left out the email addresses for security reasons. I have tried
putting semicolon with no space, semicolon with a space, comma with no
space, and comma with a space, to divide the email addresses. Nothing
works.

semicolon with no space works fine, I just wrote such a piece of code 30 min
ago, what error you get?
wrap everything in a try/catch and drill down the Exception, IIRC the
second InnerException will have the more detailed explanation of the error.
 
Hi,

It looks like foreach loops can not work with objects of type
MailMessage. Do you have another route you might suggest going?

What you mean with that?

Here is the code I used, it's in VB.net but it is simple enough to
understand it



Dim rcpt As System.Text.StringBuilder = New
System.Text.StringBuilder
Dim mail As System.Web.Mail.MailMessage = New
System.Web.Mail.MailMessage

For Each dr As System.Data.DataRow In
dm.GetUsersRequiredEmails().Tables(0).Rows
If rcpt.Length > 0 Then
rcpt.Append(";")
End If
rcpt.Append(dr("email"))
Next
mail.To = rcpt.ToString()
mail.From = "(e-mail address removed)"
mail.Subject = "Level 3 incident"
 
Hi Ignacio.
Maybe that is a difference with C# and VB, because it says that foreach
loops don't work with anything of Type MailMessage.
Also, I don't know VB, I started learning programming using C#. I am
fairly new to it, so I can't really translate from VB to C# yet. I
think I might just use a Stored Proc because I only need to grab
certain addresses.
 
Hi,

Hi Ignacio.
Maybe that is a difference with C# and VB, because it says that foreach
loops don't work with anything of Type MailMessage.
Also, I don't know VB, I started learning programming using C#. I am
fairly new to it, so I can't really translate from VB to C# yet. I
think I might just use a Stored Proc because I only need to grab
certain addresses.

But why you want to use MailMessage in a foreach, what are you going to do
with that?

It's no difference in VB or C# in this case, it's the same thing as both use
the very same MailMessage

Translating from VB is trivial , you can do it only by seeing the code you
can read it almost as plain english
 
I am using a foreach loop to populate an object of type List that will
hold all the email addresses that I need to send an email to. That list
of email addresses changes based on a certain factor. But I am doing
it a different way now, so I am good. Thanks for the help.
 
I am using a foreach loop to populate an object of type List that will
hold all the email addresses that I need to send an email to. That list
of email addresses changes based on a certain factor. But I am doing
it a different way now, so I am good. Thanks for the help.

Remember the emailTo variable should be a string array

string[] emailTo = new string[]{"email1", "email2" };
OR
string[] emailTo = new string[];
emailTo[0] = "email1";
emailTo[1] = "email2";


pass this string array into your SendEmailMethod(.....);

Then within this method you have to foreach on the string array
(emailTo)

foreach(string s in emailTo)
{
// here is where we add each email address in the array you passed
in into the method to the MailAddressCollection which is the type of
the To Property on the MailMessage class

msg.To.Add(new MailAddress(s));

}
 
Please note that I am using the New MailMessage class that is located
in the System.Net.Mail namespace not the System.Web.Mail. This is .NET
2.0 specific. Ignacio's sample code seem to be using the old classes
for composing and sending emails.
 
I think that was the problem. I am on 2.0 as well. Thanks for the help.
I'll give that a shot.
 
Hi,

tdavisjr said:
Please note that I am using the New MailMessage class that is located
in the System.Net.Mail namespace not the System.Web.Mail. This is .NET
2.0 specific. Ignacio's sample code seem to be using the old classes
for composing and sending emails.


That's correct, I'm using the 1.1

try my code with the "old" class :)
 
Back
Top