3

I have the following code (this is a simplified test) in a console application running under my own user account (contoso\test):

Update

I added a try catch and some exception handling. The last catch block is triggered. In my ULSlog it says SmtpException but it doesn't catch that one

            try
            {

                var ClientContext = new ClientContext(siteUri);
                var emailProps = new EmailProperties();
                List<string> userNames = new List<string>();
                userNames.Add("contoso\\test");
                emailProps.To = userNames;
                emailProps.Subject = "Subject";
                emailProps.Body = "Test";
                Utility.SendEmail(ClientContext, emailProps);
                ClientContext.ExecuteQueryRetry();
            }
            catch (System.Net.Mail.SmtpException smtpEx)
            {
                WriteToEventLog("SmtpException" + smtpEx.StackTrace);
            }
            catch (System.IO.IOException ioEx)
            {
                WriteToEventLog("IOException" + ioEx.StackTrace);
            }
            catch (System.Net.Sockets.SocketException socketEx)
            {
                WriteToEventLog("SocketException" + socketEx.StackTrace);
            }
            catch (Exception e)
            {
                WriteToEventLog("Exception" + e.StackTrace);
            }

In 1 in a 200 times the SendMail gives me the following exception:

Exception Info: Microsoft.SharePoint.Client.ServerException
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(System.IO.Stream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(Microsoft.SharePoint.Client.ChunkStringBuilder)
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(Microsoft.SharePoint.Client.ClientRuntimeContext, Int32, Int32, System.String)
   at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryRetry(Microsoft.SharePoint.Client.ClientRuntimeContext, Int32, Int32, System.String)
   at RemindingEmailTimerJob.Program.Main(System.String[])

I want to run this code as a Windows Scheduled task. When I configure the task and let this task run under my own user account (contoso\test) the SendMail gives me the same exception 75% of the time.

Update

I see these errors in my ULS

Original error: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine    
 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)     -
 -- End of inner exception stack trace ---    
 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)    
 at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)    
 at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)    
 at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)    
 at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)    
 at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, 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)     -
 -- End of inner exception stack trace ---    
 at System.Net.Mail.SmtpClient.Send(MailMessage message)    
 at Microsoft.SharePoint.Utilities.SPUtility.SendEmail_Client(EmailProperties properties)    
 at Microsoft.SharePoint.ServerStub.Utilities.SPUtilityServerStub.InvokeStaticMethod(String methodName, XmlNodeList xmlargs, ProxyContext proxyContext, Boolean& isVoid)    
 at Microsoft.SharePoint.Client.ServerStub.InvokeStaticMethodWithMonitoredScope(String methodName, XmlNodeList args, ProxyContext proxyContext, Boolean& isVoid)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.InvokeStaticMethod(String typeId, String methodName, XmlNodeList xmlargs, Boolean& isVoid)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStaticMethod(XmlElement xe)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessOne(XmlElement xe)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStatements(XmlNode xe)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.Process()

And

 SocialRESTExceptionProcessingHandler.DoServerExceptionProcessing - SharePoint Server Exception [System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine    
 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)     -
 -- End of inner exception stack trace ---    
 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)    
 at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)    
 at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)    
 at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)    
 at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)    
 at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, 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)     -
 -- End of inner exception stack trace ---    
 at System.Net.Mail.SmtpClient.Send(MailMessage message)    
 at Microsoft.SharePoint.Utilities.SPUtility.SendEmail_Client(EmailProperties properties)    
 at Microsoft.SharePoint.ServerStub.Utilities.SPUtilityServerStub.InvokeStaticMethod(String methodName, XmlNodeList xmlargs, ProxyContext proxyContext, Boolean& isVoid)    
 at Microsoft.SharePoint.Client.ServerStub.InvokeStaticMethodWithMonitoredScope(String methodName, XmlNodeList args, ProxyContext proxyContext, Boolean& isVoid)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.InvokeStaticMethod(String typeId, String methodName, XmlNodeList xmlargs, Boolean& isVoid)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStaticMethod(XmlElement xe)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessOne(XmlElement xe)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStatements(XmlNode xe)    
 at Microsoft.SharePoint.Client.ClientMethodsProcessor.Process()]

Anybody any idea?

Update

I tried to test my code with another development SMTP server. I was using Smtp4Dev. I tried installing hMailServer and now my code is no longer giving errors.

1 Answer 1

0

Use the try catch and logging to identify the issue...

$ErrorMessage = ""
Try
{
   //your code goes here
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    Break
}
Finally
{
    $Time=Get-Date
    "This script made a read attempt at $Time" +  $ErrorMessage  | out-file d:\errorlog.log -append
}

You can also use native PowerShell (Net.Mail.SmtpClient or Send-MailMessage ) to send email

Net.Mail.SmtpClient

$smtpServer = "XXXXX"
$smtpFrom = "XXXXX"
$smtpTo = "XXXXX"
$messageSubject = "XXXXX"
$messageBody = "XXXXX"

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

Send-MailMessage

$smtpServer = "XXXXX"
$emailFrom = "XXXXX"
$emailTo = "XXXXX"
$emailSubject = "XXXXX"
$emailBody = @"
Here is a message
XXXXX
"@
Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -SmtpServer $emailSmtpServer

Make sure sender and receiver have email addresses, i.e. not null :)

4
  • I tried the try catch. See my updated example and uls log. In uls I see a smtp exception but my catch for that doesn't do anything. I get into the last catch block
    – Danny
    Commented Jul 12, 2018 at 8:15
  • I suggest to ClientContext.Dispose() in finally block. Did you try PowerShell version that I posted above? Commented Jul 12, 2018 at 11:55
  • Yes from PowerShell no issues. From Visual Studio no issues. Starting console from explorer no issues. From scheduled task I do have issues. I did solve the problem by using another test mail server on my development server. I was using Smtp4Dev and now I am using hMailServer
    – Danny
    Commented Jul 13, 2018 at 8:46
  • Excellent it worked out in the end, you may mark this post as answer :) Commented Jul 13, 2018 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.