Ali Lazy

Ali Lazy

  • NA
  • 14
  • 6.3k

mail sending error using c# windows forms

Apr 1 2014 5:19 AM
MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            
            mail.From = new MailAddress("[email protected]");
            mail.To.Add("[email protected]");
            mail.Subject = "Test Mail - 1";
            mail.Body = "mail with attachment";
            
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(@"C:\Users\Engineer\Desktop\1.txt");
            mail.Attachments.Add(attachment);

this is mail sending code it not works 
error message is : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

I am using windows 8.1. is this is due to windows ? please help me 

            SmtpServer.Port = 587;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential("myeamil.com", "mypassword");
            SmtpServer.EnableSsl = true;
            
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;

            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain,
                                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
            {
                return true;
            };
            SmtpServer.Send(mail);

Answers (2)