Use IIS to relay SMTP email within a VB Script

Hello

We currently have a VB Script (attached) that runs against certain clients & checks servers & services. It then sends an email via the client's Exchange server.

Recently one of our client's has started to use Office365, so they no longer need Exchange. Therefore, I need to amend the script to send the email via SMTP and IIS.

I have configured IIS & SMTP to send externally - this works with a backup application that sends Job alerts, so I just need to amend the script accordingly.

Thanking you in advance.
BCC.txt
tfindingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I used to have a client that used office365. I seem to remember that you first have to log in and allow rely on whatever mail account you are using.

Make sure you are using the correct the correct smtp server. Instead of the ip, strSMTPServer should be set to smtp.office365.com port 587 and you will need authentication

https://support.office.com/en-au/article/Settings-for-POP-and-IMAP-access-for-Office-365-for-business-or-Microsoft-Exchange-accounts-7fc677eb-2491-4cbc-8153-8e7113525f6c

https://social.msdn.microsoft.com/Forums/en-US/71d50521-3e19-4ae3-a455-ebbf5375be35/sending-mails-thru-microsoft-office-365?forum=visualfoxprogeneral
0
tfindingAuthor Commented:
Hi Scott

Many thanks for your reply. I can send/relay email via smtp.office365.com through IIS, but it doesn't work if I run the script with the same settings.

I've added authentication (sendusername and sendpassword) to the script, but I get;

  -  The transport failed to connect to the server.

Will carry on testing next week.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
What does your updated code look like?  Looking at what you posted, I don't see authentication or the right port.  Make sure the "from" is set up in office365 to allow a relay.

    With objMsg.Configuration.Fields
        sch = "http://schemas.microsoft.com/cdo/configuration/"
        .Item (sch & "sendusing") = 2
        .Item (sch & "smtpserver") = strSMTPServer
        .Item (sch & "smtpserverport") = 26
        .Update
    End With
    
    '
    ' Set the importance if we have any errors
    '
    With objMsg.Fields  

        if intFailed > 0 then      
            .Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
            .Item("urn:schemas:mailheader:X-Priority") = 2
            .Item("urn:schemas:httpmail:importance") = 2
            .Update
        end if
    End With

 

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

tfindingAuthor Commented:
Hi Scott - sorry for the delay

I changed it thus;

strSMTPServer       = "smtp.office365.com"
strFrom             = "Client Name <administrator@client-name.onmicrosoft.com>"

With objMsg.Configuration.Fields
        sch = "http://schemas.microsoft.com/cdo/configuration/"
        .Item (sch & "sendusing") = 2
        .Item (sch & "smtpserver") = strSMTPServer
        .Item (sch & "smtpserverport") = 587
      .Item (sch & "sendusername") = "administrator@client-name.onmicrosoft.com"
      .Item (sch & "sendpassword") = "password"
      .Item (sch & "sendusessl") = 1
      .Item (sch & "sendauthenticate") = 1
        .Update
    End With

It ends with a Windows Script Host error on the line which states : objMsg.Send;

Error : The transport failed to connect to the server
Code : 80040213
Source : CDO.Message.1

Many thanks again
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Are you sure you have authorized remote for this account?
0
tfindingAuthor Commented:
Hi Scott

Yes, because I am sending the backup job alerts through smtp.office365.com.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I had a client that  used to use office365 and I  had this working with cdo.  I will hunt around to see if I can find the code I used.
0
tfindingAuthor Commented:
Many thanks.

As an aside, if I create a .txt file with a recipient/sender address into the Inetpub\mailroot\pickup folder, this is delivered to the recipient without issue.

Therefore, the SMTP virtual server within IIS 6.0 is ok. Again, it's using smtp.office365.com as the smart host for the routed domain.
0
tfindingAuthor Commented:
Ok, got this sorted after many '1 change at a time' attempts;

Script has the following;

I changed it thus;

 strSMTPServer       = "%Local Server%"
 strFrom             = "Client Name <administrator@client-domain.com>"

 With objMsg.Configuration.Fields
         sch = "http://schemas.microsoft.com/cdo/configuration/"
         .Item (sch & "sendusing") = 2
         .Item (sch & "smtpserver") = strSMTPServer
         .Item (sch & "smtpserverport") = 587
             .Update
     End With

SMTP Virtual Server

Properties > Delivery > Outbound Security

  -  Basic Auth = username@client-domain.onmicrosoft.com
  -  TLS encryption = ticked

Properties > Delivery > Outbound connections

  -  TCP port = 587

Properties > Delivery > Advanced Delivery

  -  FQDN = local-server.client-domain.local
  -  Smart host = smtp.office365.com

Just in case anyone else has similar problems.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tfindingAuthor Commented:
Had to play around with the script & the SMTP Virtual server to get this working - all contributions allowed me to pursue & check all settings.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.