{"id":9943,"date":"2021-01-25T00:00:13","date_gmt":"2021-01-25T08:00:13","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=9943"},"modified":"2022-04-19T21:53:13","modified_gmt":"2022-04-20T04:53:13","slug":"send-email-without-outlook","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/windows-services\/send-email-without-outlook\/","title":{"rendered":"3 Proven Ways to Send Email from a Windows Service (Without Outlook)"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load image-padding\" src=\"\/blog\/images\/send-email-from-windows-service.webp\" title=\"Sending Email from a Windows Service\" alt=\"Sending Email from a Windows Service\" border=\"0\" width=\"307\" height=\"189\" \/><\/div>\n<h2 class=\"blog-caption\">Not all Outlook functions work from a Windows Service<\/h2>\n<p>Calling Outlook from a Windows Service can be problematic. Even though many operations work fine, Microsoft has issued some <a href=\"https:\/\/docs.microsoft.com\/en-us\/outlook\/troubleshoot\/user-interface\/outlook-object-model-unsuitable-to-run-in-windows-service\" target=\"_blank\" rel=\"noopener noreferrer\">pointed advice<\/a> for customers looking to run any Office application in the background in <a href=\"\/WindowsServices\/FAQ.html#WhatIsSession0Isolation\">Session 0<\/a>:<\/p>\n<div class=\"blog-qa-question-box\">\n<img decoding=\"async\" src=\"\/images\/quotes-transparent-21x21.png\" \/> All current versions of Microsoft Office were designed, tested, and configured to run as end-user products on a client workstation. They assume an <b>interactive desktop and user profile<\/b>. They do not provide the level of reentrancy or security that is necessary to meet the needs of server-side components that are designed to run unattended.<\/p>\n<p>Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and <b>NT Services<\/b>), because Office may exhibit unstable behavior and\/or deadlock when Office is run in this environment.<\/p>\n<p>If you are building a solution that runs in a server-side context, you should <b>try to use components that have been made safe for unattended execution<\/b>. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully.\n<\/div>\n<p>Very disappointing!<\/p>\n<p>So instead of calling Outlook, which may be unreliable when run in the context of a service, look to one of these alternative solutions instead:<\/p>\n<h2 class=\"blog-caption\">Solution #1: Have your Windows Service call PowerShell to send basic email<\/h2>\n<p>If you don&#8217;t want to install any third-party utilities, you can leverage Microsoft&#8217;s ubiquitous <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/overview\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell<\/a> utility to deliver your messages. And to help, we&#8217;ve created a simple script that, given eight required parameters, will send an email to any address:<\/p>\n<div style=\"margin: 20px 0px 20px 0px;\">\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">&lt;#<\/span>\r\n<span style=\"color: #888888\">   send-email.ps1<\/span>\r\n<span style=\"color: #888888\">   <\/span>\r\n<span style=\"color: #888888\">   Summary: Sends email.<\/span>\r\n\r\n<span style=\"color: #888888\">   Usage: <\/span>\r\n<span style=\"color: #888888\">      send-email.ps1 &lt;FROM-EMAIL&gt; &lt;TO-EMAIL&gt; &lt;SUBJECT&gt; &lt;BODY&gt; &lt;SMTP-SERVER&gt; &lt;USERNAME&gt; &lt;PASSWORD&gt;<\/span>\r\n\r\n<span style=\"color: #888888\">      Example:<\/span>\r\n<span style=\"color: #888888\">         send-email.ps1 &quot;admin@coretechnologies.com&quot; &quot;alerts@coretechnologies.com&quot; &quot;Server down&quot; <\/span>\r\n<span style=\"color: #888888\">           &quot;Server &#39;FileServer1&#39; is down!&quot; &quot;smtp.gmail.com&quot; 587 &quot;admin@coretechnologies.com&quot; &quot;pwd74YKYRO&quot;<\/span>\r\n\r\n<span style=\"color: #888888\">   2020, Core Technologies Consulting, LLC (https:\/\/coretechnologies.com)<\/span>\r\n<span style=\"color: #888888\">#&gt;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold\">if<\/span> (<span style=\"color: #996633\">$args<\/span>.Count <span style=\"color: #333333\">-ne<\/span> 8) {\r\n   <span style=\"color: #007020\">Write-Host<\/span> <span style=\"background-color: #fff0f0\">&quot;Passed&quot;<\/span> <span style=\"color: #996633\">$args<\/span>.Count <span style=\"background-color: #fff0f0\">&quot;parameters; expected 8.&quot;<\/span>\r\n   <span style=\"color: #007020\">Write-Host<\/span> <span style=\"background-color: #fff0f0\">&quot;Usage:&quot;<\/span>\r\n   <span style=\"color: #007020\">Write-Host<\/span> <span style=\"background-color: #fff0f0\">&quot;send-email.ps1 &lt;FROM-EMAIL&gt; &lt;TO-EMAIL&gt; &lt;SUBJECT&gt; &lt;BODY&gt; &lt;SMTP-SERVER&gt; &lt;SMTP-PORT&gt; &lt;USERNAME&gt; &lt;PASSWORD&gt;&quot;<\/span>\r\n   exit 1\r\n}\r\n\r\n<span style=\"color: #996633\">$fromEmail<\/span> = <span style=\"color: #996633\">$args<\/span>[0]\r\n<span style=\"color: #996633\">$toEmail<\/span> = <span style=\"color: #996633\">$args<\/span>[1]\r\n<span style=\"color: #996633\">$subject<\/span> = <span style=\"color: #996633\">$args<\/span>[2]\r\n<span style=\"color: #996633\">$body<\/span> = <span style=\"color: #996633\">$args<\/span>[3]\r\n<span style=\"color: #996633\">$smtpServer<\/span> = <span style=\"color: #996633\">$args<\/span>[4]\r\n<span style=\"color: #996633\">$smtpPort<\/span> = <span style=\"color: #996633\">$args<\/span>[5]\r\n<span style=\"color: #996633\">$userName<\/span> = <span style=\"color: #996633\">$args<\/span>[6]\r\n<span style=\"color: #996633\">$password<\/span> = <span style=\"color: #996633\">$args<\/span>[7]\r\n\r\n<span style=\"color: #996633\">$smtpClient<\/span> = <span style=\"color: #007020\">New-Object<\/span> Net.Mail.SmtpClient(<span style=\"color: #996633\">$smtpServer<\/span>, <span style=\"color: #996633\">$smtpPort<\/span>)\r\n<span style=\"color: #996633\">$smtpClient<\/span>.EnableSsl = <span style=\"color: #996633\">$true<\/span>\r\n<span style=\"color: #996633\">$smtpClient<\/span>.Credentials = <span style=\"color: #007020\">New-Object<\/span> System.Net.NetworkCredential(<span style=\"color: #996633\">$userName<\/span>, <span style=\"color: #996633\">$password<\/span>)\r\n\r\n<span style=\"color: #996633\">$smtpClient<\/span>.Send(<span style=\"color: #996633\">$fromEmail<\/span>, <span style=\"color: #996633\">$toEmail<\/span>, <span style=\"color: #996633\">$subject<\/span>, <span style=\"color: #996633\">$body<\/span>)\r\n<\/pre>\n<\/div>\n<\/div>\n<p>You can <a href=\"\/blog\/files\/send-email.ps1\">download the PowerShell script here<\/a>. Please feel free to adapt it for your needs.<\/p>\n<p>Your email provider will determine the SMTP server and port number you should use. For example, if your provider is Gmail, the SMTP server is &#8220;smtp.gmail.com&#8221; and the port is 587.<\/p>\n<p>To invoke the script from an application, run the PowerShell executable with the <b>-File<\/b> option. Specify the full path to the script along with the eight required parameters.<\/p>\n<p>For example, if you&#8217;ve saved the script in C:\\Utilities and you&#8217;re sending via Gmail, your command line will look like this:<\/p>\n<div class=\"code-box\">\npowershell.exe -File &quot;C:\\Utilities\\send-email.ps1&quot; from@coretechnologies.com to@coretechnologies.com &quot;Server Down Alert&quot; &quot;Server &apos;FileServer1&apos; is down!&quot; smtp.gmail.com 587 from@coretechnologies.com &quot;PWD8581JG$&quot;\n<\/div>\n<p>Watch out for quotes in the subject and body and escape accordingly!<\/p>\n<h2 class=\"blog-caption\">Solution #2: Use SwithMail to deliver complex messages from your Windows Service<\/h2>\n<p><a href=\"https:\/\/www.tbare.com\/software\/swithmail\/\" target=\"_blank\" rel=\"noopener noreferrer\">SwithMail<\/a> is a free, no-nonsense utility that can send very detailed email messages. It supports all the important messaging options, including:<\/p>\n<ul>\n<li>HTML formatting<\/li>\n<li>Multiple attachment files<\/li>\n<li>CC and BCC recipients<\/li>\n<li>&#8220;ReplyTo&#8221; configuration<\/li>\n<\/ul>\n<p>After <a href=\"https:\/\/sourceforge.net\/projects\/swithmail\/files\/latest\/download\" target=_blank\">downloading the SwithMail zip file<\/a> and extracting its contents to a suitable location, double-click SwithMail.exe to reveal the comprehensive command line:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/swithmail-command-line-usage.png\" class=\"zoomPopup\" title=\"SwithMail command line usage\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/swithmail-command-line-usage.png\" title=\"SwithMail command line usage (click to enlarge)\" alt=\"SwithMail command line usage\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Simply specify the options you need. For example, here is a sample command line that sends the same message as the PowerShell script above:<\/p>\n<div class=\"code-box\">\nSwithMail.exe \/Silent \/FromAddress from@CoreTechnologies.com \/ToAddress to@coretechnologies.com \/Subject &quot;Server down&quot; \/HTML \/Body &quot;Server &lt;b&gt;FileServer1&lt;\/b&gt; is down!&quot; \/Server smtp.gmail.com \/Port 587 \/SSL \/Username from@coretechnologies.com \/Password &quot;PWD8581JG$&quot;\n<\/div>\n<p>If you prefer, you can compose an XML file with all the details and pass to SwithMail instead, like this:<\/p>\n<div class=\"code-box\">\nSwithMail.exe \/XML &quot;C:\\Your-SwithMail-Settings.xml&quot;\n<\/div>\n<p>If you&#8217;re having trouble configuring SwithMail, add the \/Log parameter and a path to a log file. Check the file for error messages after a failed run.<\/p>\n<h2 class=\"blog-caption\">Solution #3: Update your Windows Service code to send email directly<\/h2>\n<p>If you have access to the service&#8217;s source code, your best option may be to include code to send email using the SMTP classes built into your programming language.<\/p>\n<p>For example, if you are using C#, we recommend incorporating the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.net.mail.mailmessage\" target=_blank\">System.Net.Mail.MailMessage<\/a> and <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.net.mail.smtpclient\" target=_blank\">System.Net.Mail.SmtpClient<\/a> classes. They are very easy to use.<\/p>\n<p>Here is some sample C# code (error handling omitted for clarity):<\/p>\n<div style=\"margin: 20px 0px 20px 0px;\">\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/ Compose the message.<\/span>\r\nMailMessage mailMessage = <span style=\"color: #008800; font-weight: bold\">new<\/span> MailMessage();\r\nmailMessage.From = <span style=\"color: #008800; font-weight: bold\">new<\/span> MailAddress(<span style=\"background-color: #fff0f0\">&quot;from@coretechnologies.com&quot;<\/span>);\r\nmailMessage.To.Add(<span style=\"color: #008800; font-weight: bold\">new<\/span> MailAddress(<span style=\"background-color: #fff0f0\">&quot;to@coretechnologies.com&quot;<\/span>));\r\nmailMessage.Subject = <span style=\"background-color: #fff0f0\">&quot;Server down&quot;<\/span>;\r\nmailMessage.Body = <span style=\"background-color: #fff0f0\">&quot;Server &lt;b&gt;FileServer1&lt;\/b&gt; is down!&quot;<\/span>;\r\nmailMessage.BodyFormat = MailFormat.Html;\r\n<span style=\"color: #888888\">\/\/ Construct the SMTP object that will send the message.<\/span>\r\nsmtpClient = <span style=\"color: #008800; font-weight: bold\">new<\/span> SmtpClient(<span style=\"background-color: #fff0f0\">&quot;smtp.gmail.com&quot;<\/span>, <span style=\"color: #6600EE; font-weight: bold\">587<\/span>);\r\nsmtpClient.EnableSsl = <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\nsmtpClient.Credentials = <span style=\"color: #008800; font-weight: bold\">new<\/span> System.Net.NetworkCredential(<span style=\"background-color: #fff0f0\">&quot;from@coretechnologies.com&quot;<\/span>, <span style=\"background-color: #fff0f0\">&quot;PWD8581JG$&quot;<\/span>);\r\n<span style=\"color: #888888\">\/\/ Send the message!<\/span>\r\nsmtpClient.Send(mailMessage);\r\n<\/pre>\n<\/div>\n<\/div>\n<h2 class=\"blog-caption\">Get in touch if you need help sending email from your Windows Service<\/h2>\n<p>Hopefully one of these three solutions, which don&#8217;t involve Outlook, will work from your service. If not &mdash; or if you have questions about the methods outlined above &mdash; please don&#8217;t hesitate to <a href=\"\/support\/\">reach out to our support team<\/a>. We&#8217;re here to help!<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/why-stop-button-disabled\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: Why is the Stop Button Disabled for my Windows Service?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/stop-button-disabled-150x150-1.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: Why is the Stop Button Disabled for my Windows Service?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/how-to-restart-at-specific-time\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: How do I Restart my Windows Service at a Specific Time every Week?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/restart-service-schedule-150x150-1.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: How do I Restart my Windows Service at a Specific Time every Week?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/net-fix-access-denied\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: NET STOP Fails with Access is Denied. How I Do Make it Work?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/access-denied-150-150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: NET STOP Fails with &quot;Access is Denied&quot;. How I Do Make it Work?<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>Not all Outlook functions work from a Windows Service Calling Outlook from a Windows Service can be problematic. Even though many operations work fine, Microsoft has issued some pointed advice for customers looking to run any Office application in the &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/send-email-without-outlook\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":9959,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[234,74,230,119,233,196],"class_list":["post-9943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows-services","tag-c","tag-email","tag-outlook","tag-powershell","tag-swithmail","tag-windows-services-2"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/9943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=9943"}],"version-history":[{"count":55,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/9943\/revisions"}],"predecessor-version":[{"id":11230,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/9943\/revisions\/11230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/9959"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=9943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=9943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=9943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}