I followed a tutuorial to send gmails through a C# app.

Im making a console app and this code fails I have no idea whats wrong...

class MainClass
	{
		public static void Main (string[] args)
		{
			sendMail mailSend = new sendMail();
			 mailSend.mail("stmp.gmail.com",465,"sendersemail@gmail.com","senders password","fromadress@gmail.com","toadress@gmail.com","Subject","Body",true);
		}		
	}
public class sendMail
	{
		public void mail(string host, int port, string userName, string pswd, string fromAddress, string toAddress, string body, string subject, bool sslEnabled) {
      		MailMessage msg = new MailMessage(new MailAddress(fromAddress), new MailAddress(toAddress));    //  Create a MailMessage object with a from and to address
        	msg.Subject = subject;  //  Add your subject
        	msg.SubjectEncoding = System.Text.Encoding.UTF8;
      		msg.Body = body;    //  Add the body of your message
        	msg.BodyEncoding = System.Text.Encoding.UTF8;
      		msg.IsBodyHtml = false; //  Does the body contain html

      		SmtpClient client = new SmtpClient(host, port); //  Create an instance of SmtpClient with your smtp host and port
        	client.Credentials = new NetworkCredential(userName, pswd); //  Assign your username and password to connect to gmail
        	client.EnableSsl = sslEnabled;  //  Enable SSL
	    	try {
          		client.Send(msg);   //  Try to send your message
            	Console.WriteLine("Sent!");
      			}
      		catch {
				Console.WriteLine("Failed");	
      		}
}

Help PLEASE! This is so frustrating....

First, make the correction that Pritaeas told you. If that works, then great :)

If not, please tell us what the error is. Saying "Doesn't work" and dumping code, isn't exactly helpful. It's a bit like me bringing you a car and saying, "broken"

How long does it take to send typically? The console just blinks maybe im too impatient...

Thanks pritaeas i feel stupid now but thanks.

Alright it doesnt work..so forget it in my almost a year of coding I cant send email...wow...

thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.