简体   繁体   中英

How to send an email from office365 account using domain windows authentication

I'm developing a console app which will be executed from a windows service that needs to send emails using the domain account associated to the domain account running the windows service.

In my development machine I'm logged with a domain account that belongs to the same domain that will run the windows service but I'm not able to get it working properly.

For this development I'm using .NET 4.6.1 and the nuget package FluentEmail.Smtp

My code looks like this:

Email.DefaultSender = new SmtpSender(new SmtpClient
{
    UseDefaultCredentials = true,
    EnableSsl = true,
    Host = "smtp.office365.com",
    TargetName = "STARTTLS/smtp.office365.com",
    Port = 587,
    DeliveryMethod = SmtpDeliveryMethod.Network
});

await Email.From("myname@mycompanydomain.com", "Some tittle")
     .To(emailListObject)
     .Subject("Some subject")
     .Body("Some body", true)
     .SendAsync();

With this code I'm getting the following exception:

Unable to connect to the remote server

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 52.96.9.178:587

   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)
   at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)
   at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)

Note: From my machine I'm able to ping the IP mentioned in the exception

I'll appreciate your assistance

For starters you might want to take a look at the official document - direct send . You will note that there are quite a few issues (such as TLS security) alongside proper configuration of your Exchange server.

Without more information in your question it is rather limited as to what can be answered, however as an alternate solution, maybe take a look at direct send. (which is much less effort).

Settings for direct send

Enter the following settings on the device or in the application directly.

  • Server/smart host. Use your MX endpoint, for example, contoso-com.mail.protection.outlook.com
  • Port. Use port 25
  • TLS/StartTLS. Enable this.
  • Email address. Use any email address for one of your Office 365 accepted domains. This email address does not need to have a mailbox.

Microsoft recommends adding an SPF record to avoid having messages flagged as spam. If you are sending from a static IP address, add it to your SPF record in your domain registrar's DNS settings as follows:

DNS entry   Value
SPF         v=spf1 ip4:<Static IP Address> include:spf.protection.outlook.com ~all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM