user1056344
user1056344

Reputation: 23

How can I use a variable to define MailAddress toAddress in c#?

I have an ASP.NET 4.0 aspx page from which I wish to send an email to the recipient specified in a text box named "supervisoremailTextBox". Is there any way that I can specify a variable as the recipient email address. The code I have used which doesn't work is shown below:

MailAddress fromAddress = new MailAddress("[email protected]", "Sender Name");
MailAddress toAddress = new MailAddress("supervisoremailTextBox.Value");
message.From = fromAddress;
message.To.Add(toAddress); 

Sorry if this a really dumb question and thanks in advance for your help.

Upvotes: 2

Views: 658

Answers (2)

Oded
Oded

Reputation: 499132

When you use MailAddress, you need to use a valid email address.

The string "supervisoremailTextBox.Value" is not a valid email address.

If you mean to use the value of a textbox with the ID supervisoremailTextBox, use:

MailAddress toAddress = new MailAddress(supervisoremailTextBox.Value);

Note that I have dropped the " to ensure you are not passing in a string.

Upvotes: 2

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120997

Try this instead:

MailAddress toAddress = new MailAddress(supervisoremailTextBox.Value);

Upvotes: 0

Related Questions