• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Couple of errors that im unsure on (c#)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

asusradeon

Member
Joined
Oct 25, 2004
Location
127.0.0.1
When trying to run this code in Visual C# 2005:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;



namespace Email
{
    class Program
    {
        static void Main(string[] args)
        {
            string to;
            string subject;
            string message;
            string from;
            int times;
            MailMessage mail;

            Console.WriteLine("To, Subject, Message, from");
            to = Console.ReadLine();
            subject = Console.ReadLine();
            message = Console.ReadLine();
            from = Console.ReadLine();

            mail.To = to;
            mail.Subject = subject;
            mail.Body = message;

            SmtpClient smtp;

            smtp.Host = "localhost";
            smtp.Send(mail);


            Console.ReadLine();
            


        }
    }
}

I get the following errors:

Code:
Error    1    Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only    D:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\Email\Email\Program.cs    27    13    Email


Error    2    Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddressCollection'    D:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\Email\Email\Program.cs    27    23    Email


any ideas could do with some help ?

thanks
 
MailMessage.To is a collection. I suspect what you want to do is add an item to the collection. It should look something like this:
Code:
mail.To.Add(to);
 
thanks although this gives me some other errors:

Error 2 Use of unassigned local variable 'mail' D:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\Email\Email\Program.cs 27 13 Email

Error 3 Use of unassigned local variable 'smtp' D:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\Email\Email\Program.cs 33 13 Email


any ideas ?
 
Change the line where you create the mail variable to the following (same goes for smtp):
Code:
MailMessage mail = new MailMessage();

Anything thats not a value type has to be constructed like this. The one exception is the String class.

Value types are things like int, and some simple structs that aren't managed by the garbage collector.
 
why cant i do mail.from = from; ?

says it cant convert from string to System.Net.Mail.Address or something ?? surely it should just be a string anyway ?

thanks
 
It takes a MailAddress, which can be just an address, or address and an alias. The MailAddress type just allows for a little more flexibility. The compilier should be able to convert it automatically, but if its failing for some reason, you can explicitly converting it like this:

Code:
mail.From = new MailAddress(from);

If you wanted to have an alias as well, you could do this:
Code:
mail.From = new MailAddress("[email protected]", "John Smith");
 
Back