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();
Question
sathenzar
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