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