• 0

C# gmail won't connect.


Question

Can someone tell me if there is anything they see wrong with this code? This is my first time trying to use C# to connect to my gmail account. It doesn't say it can't connect to the host or anything anymore I fixed that problem, but now it just kicks back an error saying Failure sending mail, I looked on google and found examples that match my code, what could I be doing wrong?

MailMessage message = new MailMessage();

					message.From = new MailAddress(fEmail);

					message.To.Add(new MailAddress(String.Format("{0}{3}{1}{2}", "myemail", "@", "woh.rr", ".com")));

					message.Subject = "TEST";

					if (newMsg != "")
						message.Body = newMsg;
					else
						message.Body = "<html><head></head><body><img src='http://i35.tinypic.com/n6os8z.jpg' border='0' /></body></html>";


					message.IsBodyHtml = true;


					var smtp = new SmtpClient
					{
						Host = "smtp.gmail.com",
						Port = 587,
						EnableSsl = true,
						DeliveryMethod = SmtpDeliveryMethod.Network,
						UseDefaultCredentials = false,
						Credentials = new NetworkCredential("myemail@gmail.com", "***********")
					};

					smtp.Send(message);
					message.Dispose();

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

i was actually doing this just today!

i have modified your code slightly.. this works ;)

		protected void Button1_Click(object sender, EventArgs e)
		{
			MailMessage message = new MailMessage("fEmail@test.com", "xxxxx@xxxxxx.com");
			//message.From = new MailAddress(fEmail);
			//message.To.Add(new MailAddress(String.Format("{0}{3}{1}{2}", "myemail", "@", "woh.rr", ".com")));

			message.Subject = "TEST";

			//if (newMsg != "")
			//	message.Body = newMsg;
			//else
			message.Body = "<html><head></head><body><img src='http://i35.tinypic.com/n6os8z.jpg' border='0' /></body></html>";

			message.IsBodyHtml = true;


			var smtp = new SmtpClient
			{
				Host = "smtp.gmail.com",
				Port = 587,
				EnableSsl = true,
				DeliveryMethod = SmtpDeliveryMethod.Network,
				UseDefaultCredentials = false,
				Credentials = new NetworkCredential("*********@gmail.com", "**************")
			};

			smtp.Send(message);
			message.Dispose();
		}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.