0

I've tried sending mails using this code:

SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");

MailMessage mail = new MailMessage("email@gmail.com", "some1@gmail.com", "Subject", "Body");
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

client.Send(mail);

It doesn't work, the error looks like this: http://scr.hu/5gpw/tv6hn What's wrong with this code?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 1
    Possible duplicate of [mail sending with network credential as true in windows form not working](http://stackoverflow.com/questions/32475832/mail-sending-with-network-credential-as-true-in-windows-form-not-working) – Salah Akbari Dec 28 '15 at 15:44

1 Answers1

1

I would change the code into a Method and test passing in the the Port, Host etc look at this and see if it works for you I just tested it and it works great on my end.

public void Send(string from, string to,string smtpServer, int smtpPort,string username, string password)
{
    try
    {
        using (MailMessage mm = new MailMessage())
        {
            SmtpClient sc = new SmtpClient();
            mm.From = new MailAddress(from, "Test");
            mm.To.Add(new MailAddress(to));
            mm.IsBodyHtml = true;
            mm.Subject = "Test Message";
            mm.Body = "This is a test email message from Krzysztof Senska";
            mm.BodyEncoding = System.Text.Encoding.UTF8;
            mm.SubjectEncoding = System.Text.Encoding.UTF8;
            NetworkCredential su = new NetworkCredential(username, password);
            sc.Host = smtpServer;
            sc.Port = smtpPort;
            sc.Credentials = su;
            sc.Send(mm);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52