none
The SMTP server requires a secure connection or the client was not authenticated.

    Question

  • I was initially trying to write this to send out emails with attachments, but have not been able to get the code to do anything...so I tried trimming it down to just get the sending of the email portion of the code to work. Here is the block of code I have come with in trying to send an email to my gmail account when a document is added to a document library.

    public override void ItemAdded(SPItemEventProperties properties)
    {
       try
       {
    	   SPSecurity.RunWithElevatedPrivileges(delegate()
    	   {
    
    		   using (SPSite currentSite = new SPSite(properties.Web.Url))
    		   {
    			   using (SPWeb currentWeb = currentSite.OpenWeb(properties.Web.Url))
    			   {
    				   SPFolder mylibrary = properties.Web.Folders["My Docs"];
    				   properties.Web.AllowUnsafeUpdates = true;
    
    				   MailMessage message = new MailMessage();
    				   message.From = new MailAddress("myUsername@gmail.com");
    				   message.To.Add(new MailAddress("myUsername@gmail.com"));
    				   message.IsBodyHtml = true;
    				   message.Body = "This is the email body";
    				   message.Subject = "Email received";
    
    				   SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
    				   NetworkCredential cred = new NetworkCredential("myUsername@gmail.com", "myPassword");
    				   smtpClient.Port = 587;
    				   smtpClient.EnableSsl = true;
    				   smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    				   smtpClient.UseDefaultCredentials = false;
    				   smtpClient.UseDefaultCredentials = true;
    				   
    				   //Added this to bypass the certificate validation
    				   ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain,
    						System.Net.Security.SslPolicyErrors sslPolicyErrors)
    					{
    						return true;
    					};
    
    				   smtpClient.Send(message);
    			   }
    		   }
    			
    
    	   });
       }
       catch (Exception ex)
       {
    	   SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Category", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
       }
       finally
       {
    
       }
    }

    When I run this, i get an SMTPException that states "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". 

    Does anyone have a clue why am getting this, and how i can get past this? I am using a VM as my dev environment with a Windows Server 2008 R2 Standard instance. I really appreciate the help.

    here is the stack trace:

       at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
       at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at SendEmailSampleEventReceiver.EventReceiver1.EventReceiver1.<>c__DisplayClass3.<ItemAdded>b__0()
       at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
       at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)


    Friday, November 22, 2013 7:50 PM

Answers

  • I just got it to work...the part of my code that I changed was

    //I changed this
    NetworkCredential cred = new NetworkCredential("myUsername@gmail.com", "myPassword");
    
    //to this
    smtpClient.Credentials = new NetworkCredential("myUsername@gmail.com", "myPassword");

    Thanks for all the help everyone...I had not done this before, but I sure learnt quite a few things from the suggestions I got.

    • Marked as answer by Spawn10 Monday, November 25, 2013 5:57 PM
    Monday, November 25, 2013 5:56 PM

All replies

  • You're trying to send from an @gmail.com address and they will require authentication, but you don't have credentials specified.  Try sending from @nowhere.net and see what happens.
    Friday, November 22, 2013 7:55 PM
  • Hi,                                                             

    As SmallFoxx said, you seem not to specify the credential for the SmtpClient object.

    What's more, Gmail prevents unrecognized access to your accounts, please try to execute this code and then go to you gmail and it will ask you if the access try was from you, allow it and execute the code to see whether it is work now.

    Or you can use SPUtility.SendEmail Method to send the email for you, more details can be seen in the link below:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail(v=office.14).aspx

    Best regards

    Patrick Liang
    TechNet Community Support

    Monday, November 25, 2013 1:44 PM
  • I get the same exception when i try sending the email to either yahoo or hotmail, and the credentials are good. And I am trying to use smtpClient.send() because once i am able to send the emails, I also want to enhance the code to add an attachment, which the SPUtility.SendMail does not allow (if i am correct).

    Are there anything that I should possibly look into with regards to my dev environment? Its a vm and this is the first time i am trying to accomplish this task.

    I followed this post (http://www.petri.co.il/configure-email-sharepoint-2010-part-1.htm#) to try to configure my vm and I stopped at the point of configuring the exchange server, cause I didn't think it applied to me. I have a feeling that am missing something basic that am not aware of. Please help.

    Monday, November 25, 2013 4:31 PM
  • Did anyone have any issues using my block of code? That will help me narrow down the issue to environment, if there has been any success using the code.
    Monday, November 25, 2013 5:39 PM
  • I just got it to work...the part of my code that I changed was

    //I changed this
    NetworkCredential cred = new NetworkCredential("myUsername@gmail.com", "myPassword");
    
    //to this
    smtpClient.Credentials = new NetworkCredential("myUsername@gmail.com", "myPassword");

    Thanks for all the help everyone...I had not done this before, but I sure learnt quite a few things from the suggestions I got.

    • Marked as answer by Spawn10 Monday, November 25, 2013 5:57 PM
    Monday, November 25, 2013 5:56 PM