Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable can not send an email when called by Task Scheduler

Tags:

task

scheduler

I have written a C# .net executable that sends an email through an outlook exchange server. Everything works fine when I run it manually, but when I use a scheduled task to call the executable it doesn't send the email. Everything else works fine, but the email doesn't get sent. I set the scheduled task to run as my user account. When the task is running I can see in Task Manager that the executable is running under my username. This rules out any obvious permissions issues.

While debugging I made the program output some text to a file on a network share on the same machine on which Exchange is running. This file outputs fine, so I know that the program can connect to that machine.

Can anyone help?

like image 896
user2320861 Avatar asked May 10 '26 09:05

user2320861


1 Answers

Ok, as you can see above I was trying to send mail through a running instance of Outlook. Although I wasn't able to post code without in a comment box without pulling my hair out @amitapollo gave me the clue to use the System.Net.Mail namespace. At the end of the day I got it to work. Here's my code:

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("myExchangeServerIPAddress");
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = new System.Net.NetworkCredential("myDomain\\myUsername", "myPassword");
        smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;            
        smtpClient.EnableSsl = true;

System.Security.Cryptography.X509Certificates.X509Store xStore = new System.Security.Cryptography.X509Certificates.X509Store();
        System.Security.Cryptography.X509Certificates.OpenFlags xFlag = System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly;
        xStore.Open(xFlag);
        System.Security.Cryptography.X509Certificates.X509Certificate2Collection xCertCollection = xStore.Certificates;
        System.Security.Cryptography.X509Certificates.X509Certificate xCert = new System.Security.Cryptography.X509Certificates.X509Certificate();
        foreach (System.Security.Cryptography.X509Certificates.X509Certificate _Cert in xCertCollection)
        {
            if (_Cert.Subject.Contains("[email protected]"))
            {                    
                xCert = _Cert;
            }
        }

smtpClient.ClientCertificates.Add(xCert);

//I was having problems with the remote certificate no being validated so I had to override all security settings with this line of code...

System.Net.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("[email protected]", "[email protected]", "mySubject", "myBody");
like image 167
user2320861 Avatar answered May 13 '26 21:05

user2320861