473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 14855
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
3
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b...
4
by: David Morris | last post by:
Hi Could somebody please explain what the following line of code means String.Format("{0}\{1}.{2:00}", C:\, myfile.txt, 1 It's actually the first argument that I don't understand. What is...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
1
by: pennyfx | last post by:
I ran into a strange problem today. I tried to send mail to a strange but valid email address, but the MailAddress("") object choked on me. It threw a formating exception saying it was not a valid...
5
by: jpenguin | last post by:
I'm doing a simple program to learn java. I have the input and math parts right. It just isn't displaying right. It outputs a list of about 11 things-- with the format "string $ xx.xx" In each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.