469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

MailAddress string format

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

Feb 16 '06 #1
16 14666
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?

Feb 16 '06 #2
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

Feb 16 '06 #3
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("em****@test.com"));
msg.To.Add(new MailAddress("em****@test.com"));
.....
.....

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

Feb 16 '06 #4
Thanks for the help. I will look into doing it this way.

Feb 16 '06 #5
It looks like foreach loops can not work with objects of type
MailMessage. Do you have another route you might suggest going?

Feb 16 '06 #6


"gd*****@yahoo.com" wrote:
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.
Feb 16 '06 #7
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.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 16 '06 #8
Hi,

<gd*****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 = "si**@ffdot.state.ffl.us"
mail.Subject = "Level 3 incident"

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 16 '06 #9
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.

Feb 16 '06 #10
Hi,

<gd*****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 16 '06 #11
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.

Feb 16 '06 #12

gd*****@yahoo.com wrote:
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));

}

Feb 17 '06 #13
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.

Feb 17 '06 #14
I think that was the problem. I am on 2.0 as well. Thanks for the help.
I'll give that a shot.

Feb 17 '06 #15
Here is a good resource you should check out

http://www.systemnetmail.com

Feb 17 '06 #16
Hi,

"tdavisjr" <td******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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 :)
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 17 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bob | last post: by
7 posts views Thread by Alpha | last post: by
3 posts views Thread by Dominique Vandensteen | last post: by
4 posts views Thread by David Morris | last post: by
38 posts views Thread by nobody | last post: by
7 posts views Thread by L. Scott M. | last post: by
8 posts views Thread by Lucky | last post: by
5 posts views Thread by jpenguin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.