help needed for email coding
hi,
i'm currently doing my major project in school. i'm doing c# programming and i am stuck at the registration part.
i'm supposed to send an email with an unique and randon activation code to the users to verify their account.
MailMessage m = new MailMessage();
m.From = new MailAddress("[email protected]", "Shannelle-administrator");
m.To = new MailAddress(tbxEmail.Text);
m.Subject = "McDonald's delivery - Verify your Account now";
m.BodyEncoding = System.Text.Encoding.UTF8;
m.Body = "Thanks for registering with us! " + "/n"
+ "Please note that you must complete this last step to become a registered member" + "/n"
+ "To complete your registration, please visit this url: " + "/n"
+ "http://localhost:3130/MP%20web/Registration_VerifyAcct.aspx" + "/n"
+ "Your username is: " + tbxNickname + "/n"
+ "Your activation code is: 00001 " ;
m.Priority = MailPriority.Normal;
try
{
// SmtpMail class
SmtpClient client = new SmtpClient();
client.Send(m);
}
catch (Exception ex)
{
Label1.Text = "SQL Error: " + ex.Message;
}
But i've some errors at the m.To line. the error says "Property or Indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only"
the other error says Cannot implicitly convert type System.Net.Mail.MailAddress' to 'System.Net.Mail.MailCollection'
this error is refering to the word new after m.To
i would appreciate it if someone could provide me with the code. im really desperate for it now ):
and also, if im using localhost, how am i supposed to test it, if there's no IIS installed?
thanks alot (:
cheers,
shannelle
Re: help needed for email coding
I'm pretty sure the problem is that the To property is already initialized as a collection, and you need to call a member on it to add e-mail addresses.
As for testing - Visual Studio has a built-in web development server. If you try to run the project (assuming it's a website), it'll launch in a browser. You might have to adjust the port number in the redirect since it's not always the same.
Re: help needed for email coding
change
Code:
m.To = new MailAddress(txtEmail.Text);
to
Code:
MailAddress m = new MailAddress(txtEmail.Text);
your trying to construct a new MailAddress Object in its To property. You usually Construct the object (in this case a MailAddress object) and then Assign its properties.
so you could also go.
Code:
MailAddress m = new MailAddress();
m.To = txtEmail.Text;
m.Name = "Joe Blow";
hth,
mcm
Re: help needed for email coding
The solution posted by MCMCOM does not work.
His/Her example shows an instance of MailAddress, not MailMessage. First, MailAddress does not have a .To property. mailMessage has one, but it cant be assigned to.
Here is the problem that has not been solved on this thread:
Dim mailMessage As System.Net.Mail.MailMessage
mailMessage.To = "some email address"
The project will not build because the 2nd line generates an error ..
Property 'To' is read only. So, ... how can a recipient be specified when using System.Net.Mail.Mailmessage?
Thanks,
Marc-
Re: help needed for email coding
Have you guys tried Google for reference on the System.Net.Mail namespace? The first link that came up for me shows exactly how to easily pull this off.
The 2nd link is the Microsoft reference for the code, which also shows what you need and provides details on what each object / property / method is...
Basically, the MailMessage.To property is a MailAddressCollection - you have to add MailAddress objects to it.