0

Trying to send an email containing a password, when the user account is created. (with help from) How to send email from Asp.net Mvc-3?

public void SendEmail(string address, string subject, string message, string password)
    {
        string email = "emailAddress";
        //string password = "put-your-GMAIL-password-here";

        var loginInfo = new NetworkCredential(email, password);
        var msg = new MailMessage();
        var smtpClient = new SmtpClient("smtp.gmail.com", 587);

        msg.From = new MailAddress(email);
        msg.To.Add(new MailAddress(address));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;

        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = loginInfo;
        smtpClient.Send(msg);
    }

but smtpClient.Send(msg); returns the following error:

{System.Net.Mail.SmtpException: 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
   at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at ACCS.StockControl.Controllers.UserLoginRecordController.SendEmail(String address, String subject, String message, String password)
   at ACCS.StockControl.Controllers.UserLoginRecordController.EditModalPOST(UserLoginRecordEditModalVM model)}

any ideas?

Community
  • 1
  • 1
John
  • 3,965
  • 21
  • 77
  • 163
  • Have you seen [this](http://stackoverflow.com/questions/17227532/gmail-530-5-5-1-authentication-required-learn-more-at) – Liam Oct 21 '13 at 11:12
  • possible duplicate of [Can't auth to Gmail smtp via MailMessage & smtpClient](http://stackoverflow.com/questions/9104645/cant-auth-to-gmail-smtp-via-mailmessage-smtpclient) – Liam Oct 21 '13 at 11:13
  • Any Progress? Please make the changes in SSL and try again. Set Enable SSL to false – Incredible Oct 21 '13 at 11:22
  • Please check an answer or add more help to us – Krekkon Mar 15 '14 at 09:08

3 Answers3

0

It can be just with the wrong password and username.

Please try with another fake password, (e.g. "thisPasswordIsSurelyDontWork"). If your exception is same, then you have to check your creditentals, Username, Password

//or try

 smtpClient.UseDefaultCredentials = true;
Krekkon
  • 1,349
  • 14
  • 35
0
 MailMessage loginInfo = new MailMessage();
 loginInfo.To.Add("Emailaddress");
 loginInfo.From = new MailAddress("FromEmailaddress");
 loginInfo.Subject = "Subject";
 loginInfo.Body = "Body of the mail";
 loginInfo.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient();
 smtp.Host = hostname;
 smtp.Port = 25; //in my case i am using 25
 smtp.EnableSsl = false; //enable ssl is set to false
 smtp.Credentials = new System.Net.NetworkCredential(sEmailId, sPassword);
 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtp.Send(loginInfo);
 {
   Label.Visible = true;
   Label.Text = "A mail has been sent to you with the your username. Please check your inbox.";
 }
Incredible
  • 3,495
  • 8
  • 49
  • 77
0

Try by

 smtpClient.EnableSsl = false;
Monika
  • 2,172
  • 15
  • 24