title: How to send email from a out of process background task
body: <p>I have a UWP out of process time triggered background task which successfully shows a Notification and gets data from an SQL Server database table.</p>
  <p>However, the backgound task will not send a simple SMTP email although the code works fine in foreground.</p>
  <p>This link says mail can be sent.</p>
  <p><a href="https://docs.microsoft.com/en-us/windows/uwp/launch-resume/support-your-app-with-background-tasks" rel="nofollow noreferrer">Microsoft background task</a></p>
  <p>Updated:</p>
  
      
  <pre><code>    &lt;Extension Category=&quot;windows.backgroundTasks&quot; EntryPoint=&quot;Tasks.ServicingComplete&quot;&gt;
            &lt;BackgroundTasks&gt;
              &lt;Task Type=&quot;systemEvent&quot;/&gt;
            &lt;/BackgroundTasks&gt;
          &lt;/Extension&gt;
          &lt;Extension Category=&quot;windows.backgroundTasks&quot; EntryPoint=&quot;Tasks.LookupBackgroundTask&quot;&gt;
            &lt;BackgroundTasks&gt;
              &lt;Task Type=&quot;systemEvent&quot;/&gt;
              &lt;Task Type=&quot;timer&quot;/&gt;
            &lt;/BackgroundTasks&gt;
          &lt;/Extension&gt;
        &lt;/Extensions&gt;
      &lt;/Application&gt;
    &lt;/Applications&gt;
  
    &lt;Capabilities&gt;
      &lt;Capability Name=&quot;internetClient&quot; /&gt;
      &lt;uap:Capability Name=&quot;enterpriseAuthentication&quot;/&gt;
    &lt;/Capabilities&gt;
  </code></pre>
  <p>I have Executable..., System Event and Timer declared in the manifest.
  Perghaps there is another declaration required or sending an email cannot be achieved?</p>
  <pre><code>using EASendMail;
  public static bool SendMail(EmailModel emailModel)
          {
              SmtpEmailModel emailConfig = EmailController.EmailConfiguration();
  
              try
              {
                  SmtpMail email = new SmtpMail(&quot;TryIt&quot;);
                  SmtpClient client = new SmtpClient();
  
                  email.From = new MailAddress(emailConfig.EmailAddress);
                  email.To.Add(new MailAddress(emailModel.ToAddress));
                
                  email.Subject = emailModel.Subject;
                  email.HtmlBody = emailModel.HtmlBody;
  
                  if (emailModel.Attachment != null)
                  {
                      Attachment attachment = await email.AddAttachmentAsync(emailModel.Attachment.Path);
                  }
  
                  SmtpServer server = new SmtpServer(emailConfig.EmailHost);
  
                  server.User = emailConfig.EmailAddress;
                  server.Password = emailConfig.EmailPassword;
  
                  server.Port = 587;
                  server.ConnectType = SmtpConnectType.ConnectSSLAuto;
  
                  client.SendMailAsync(server, email);
  
                  InAppNotificationModel inAppNotification = new InAppNotificationModel()
                  {
                      Header = &quot;EMAIL&quot;,
                      Message = &quot;Message Sent&quot;
                  };
  
                  InAppNotificationEventHandler.InAppNotification = inAppNotification;
  
                  return true;
              }
              catch (Exception ep)
              {
                  InAppNotificationModel inAppNotification = new InAppNotificationModel()
                  {
                      Header = &quot;Email not Sent&quot;,
                      Message = $&quot;{ep.Message}&quot;
                  };
  
                  InAppNotificationEventHandler.InAppNotification = inAppNotification;
  
                  return false;
              }
          }
  </code></pre>
  <p>For background working I also tested with the generic mail provider and exactly the same result. I usually use async operation but removed this for the background task. I tested in foregraound with no async and it worked fine for EASendMail and the generic mail. It's an office 365 account.</p>