954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Gmail Help

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....

nsutton
Light Poster
46 posts since Apr 2010
Reputation Points: 10
Solved Threads: 2
 

First thing I notice is "stmp.gmail.com". This should be "smtp.gmail.com".

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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"

Ketsuekiame
Master Poster
752 posts since May 2010
Reputation Points: 349
Solved Threads: 107
 

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

Thanks pritaeas i feel stupid now but thanks.

nsutton
Light Poster
46 posts since Apr 2010
Reputation Points: 10
Solved Threads: 2
 

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

thanks anyway

nsutton
Light Poster
46 posts since Apr 2010
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: