Email script to use Office365 with Port 587

Tek Info
Tek Info used Ask the Experts™
on
I would like a script that I can modify to be able to the following:

Use Office365 smtp
Port 587
TLS
to accept username and password
have a pdf attachment to it called 1.pdf
Send to a distribution list from. EmailList.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Isn't this a duplicate of https://www.experts-exchange.com/questions/29164501/Modify-script-to-use-Office365-and-port-587.html or am I missing a deeper purpose here?

Author

Commented:
This is not a duplicate.
Do not delete.
The is a separate question.
The script here is not defined whether it is Powershell, VB, batch.. it's an open question and deserves and answer.
This should cover several of the other ways to send an e-mail scripted:

[VB, VBA - Send Email over TLS]    
'Connect server over TLS by STARTTLS command
Dim oSmtp As EASendMailObjLib.Mail
Set oSmtp = new EASendMailObjLib.Mail 

oSmtp.LicenseCode = "TryIt" 

oSmtp.ServerAddr = "mail.adminsystem.com" 

' Set User Authentication
oSmtp.UserName = "test@adminsystem.com"
oSmtp.Password = "test"

' Enable TLS Connection
oSmtp.SSL_init
oSmtp.SSL_starttls = 1
oSmtp.Subject = "test for ssl"
oSmtp.BodyText = "test body"
 
oSmtp.FromAddr = "test@adminsystem.com"
oSmtp.AddRecipient "Support Team", "Support@adminsystem.com", 0 
 
If oSmtp.SendMail() = 0 Then
  MsgBox "Message delivered"
Else
  MsgBox oSmtp.GetLastErrDescription()
End If
 
oSmtp.SSL_uninit
 


[VB, VBA - Send Email over SSL]
'Connect server over SSL by direct SSL
Dim oSmtp As EASendMailObjLib.Mail
Set oSmtp = new EASendMailObjLib.Mail 
oSmtp.LicenseCode = "TryIt" 
 
oSmtp.ServerAddr = "mail.adminsystem.com"

' Set User Authentication
oSmtp.UserName = "test@adminsystem.com"
oSmtp.Password = "test"

' Enable SSL Connection
oSmtp.SSL_init
oSmtp.SSL_starttls = 0
'SMTP server usually uses 465 as the alone SSL port
oSmtp.ServerPort = 465 

oSmtp.Subject = "test for ssl"
oSmtp.BodyText = "test body"
 
oSmtp.FromAddr = "test@adminsystem.com"
oSmtp.AddRecipient "Support Team", "Support@adminsystem.com", 0 
 
If oSmtp.SendMail() = 0 Then
  MsgBox "Message delivered"
Else
  MsgBox oSmtp.GetLastErrDescription()
End If
 
oSmtp.SSL_uninit



[VBScript, ASP - Send Email over TLS]    
'Connect server over TLS by STARTTLS command
Dim oSmtp
Set oSmtp = Server.CreateObject("EASendMailObj.Mail") 

oSmtp.LicenseCode = "TryIt" 

oSmtp.ServerAddr = "mail.adminsystem.com" 

' Set User Authentication
oSmtp.UserName = "test@adminsystem.com"
oSmtp.Password = "test"

' Enable TLS Connection
oSmtp.SSL_init
oSmtp.SSL_starttls = 1
oSmtp.Subject = "test for ssl"
oSmtp.BodyText = "test body"
 
oSmtp.FromAddr = "test@adminsystem.com"
oSmtp.AddRecipient "Support Team", "Support@adminsystem.com", 0 
 
If oSmtp.SendMail() = 0 Then
  Response.Write "Message delivered"
Else
  Response.Write oSmtp.GetLastErrDescription()
End If
 
oSmtp.SSL_uninit
 
[VBScript, ASP - Send Email over SSL]
'Connect server over SSL by direct SSL
Dim oSmtp
Set oSmtp = Server.CreateObject("EASendMailObj.Mail")
oSmtp.LicenseCode = "TryIt" 
 
oSmtp.ServerAddr = "mail.adminsystem.com"
' Set User Authentication
oSmtp.UserName = "test@adminsystem.com"
oSmtp.Password = "test"

' Enable SSL Connection
oSmtp.SSL_init
oSmtp.SSL_starttls = 0
'SMTP server usually uses 465 as the alone SSL port
oSmtp.ServerPort = 465 
oSmtp.Subject = "test for ssl"
oSmtp.BodyText = "test body"
 
oSmtp.FromAddr = "test@adminsystem.com"
oSmtp.AddRecipient "Support Team", "Support@adminsystem.com", 0 
 
If oSmtp.SendMail() = 0 Then
  Response.Write "Message delivered"
Else
  Response.Write oSmtp.GetLastErrDescription()
End If
 
oSmtp.SSL_uninit



[Visual C++ - Send Email over TLS]
#include "stdafx.h"
#include <comdef.h>
#include <iostream>

#include "easendmailobj.tlh"
using namespace EASendMailObjLib;

using namespace std;
 
void SendEmail()
{
  ::CoInitialize(NULL);
  IMailPtr oSmtp = NULL;
  oSmtp.CreateInstance(__uuidof(EASendMailObjLib::Mail));
	
  //The license code for EASendMail ActiveX Object,
  //for evaluation usage, please use "TryIt" as the license code.
  oSmtp->LicenseCode = _T("TryIt");
	
  oSmtp->ServerAddr = _T("mail.adminsystem.com");
  
  // Set user authentication
  oSmtp->UserName = _T("test@adminsystem.com"); 
  oSmtp->Password = _T("test");
  
  //Enable TLS connection
  oSmtp->SSL_init();
  oSmtp->SSL_starttls = 1;  
  
  oSmtp->FromAddr = _T("test@adminsystem.com");
  oSmtp->AddRecipient(_T("Support Team"), 
                          _T("support@adminsystem.com"), 0);
 
  oSmtp->Subject = _T("Test");
  oSmtp->BodyText = _T("Hello, this is a test email from VC++ ....");
  
  if(oSmtp->SendMail() == 0)
    cout << _T("Message delivered!") << endl;
  else
    cout << (const TCHAR*)(oSmtp->GetLastErrDescription()) << endl;
}



[Visual C++ - Send Email over SSL]
#include "stdafx.h"
#include <comdef.h>
#include <iostream>

#include "easendmailobj.tlh"
using namespace EASendMailObjLib;

using namespace std;
 
void SendEmail()
{
  ::CoInitialize(NULL);
  IMailPtr oSmtp = NULL;
  oSmtp.CreateInstance(__uuidof(EASendMailObjLib::Mail));
	
  //The license code for EASendMail ActiveX Object,
  //for evaluation usage, please use "TryIt" as the license code.
  oSmtp->LicenseCode = _T("TryIt");
	
  oSmtp->ServerAddr = _T("mail.adminsystem.com");
  
  // Set user authentication
  oSmtp->UserName = _T("test@adminsystem.com"); 
  oSmtp->Password = _T("test");
  
  //Enable SSL connection
  oSmtp->SSL_init();
  oSmtp->SSL_starttls = 0; 
  oSmtp->ServerPort = 465;
  
  oSmtp->FromAddr = _T("test@adminsystem.com");
  oSmtp->AddRecipient(_T("Support Team"), 
                          _T("support@adminsystem.com"), 0);
 
  oSmtp->Subject = _T("Test");
  oSmtp->BodyText = _T("Hello, this is a test email from VC++ ....");
  
  if(oSmtp->SendMail() == 0)
    cout << _T("Message delivered!") << endl;
  else
    cout << (const TCHAR*)(oSmtp->GetLastErrDescription()) << endl;
}



[Delphi - Send Email over TLS]
unit Unit1;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail unit

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  oSmtp : TMail;
begin
  oSmtp := TMail.Create(Application);
  oSmtp.LicenseCode := 'TryIt';

  // Set your sender email address
  oSmtp.FromAddr := 'test@emailarchitect.net';

  // Add recipient email address
  oSmtp.AddRecipientEx('support@emailarchitect.net', 0);

  // Set email subject
  oSmtp.Subject := 'simple email from Delphi project';

  // Set email body
  oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply';

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net';

  // User and password for ESMTP authentication, if your server doesn't require
  // user authentication, please remove the following codes
  oSmtp.UserName := 'test@emailarchitect.net';
  oSmtp.Password := 'testpassword';

  // Enable TLS connection
  oSmtp.SSL_init();
  oSmtp.SSL_starttls := 1;  

  ShowMessage('start to send email ...');

  if oSmtp.SendMail() = 0 then
    ShowMessage('email was sent successfully!')
  else
    ShowMessage('failed to send email with the following error: '
    + oSmtp.GetLastErrDescription());

end;

end.



[Delphi - Send Email over SSL]
unit Unit1;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail unit

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  oSmtp : TMail;
begin
  oSmtp := TMail.Create(Application);
  oSmtp.LicenseCode := 'TryIt';

  // Set your sender email address
  oSmtp.FromAddr := 'test@emailarchitect.net';

  // Add recipient email address
  oSmtp.AddRecipientEx('support@emailarchitect.net', 0);

  // Set email subject
  oSmtp.Subject := 'simple email from Delphi project';

  // Set email body
  oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply';

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net';

  // User and password for ESMTP authentication, if your server doesn't require
  // user authentication, please remove the following codes
  oSmtp.UserName := 'test@emailarchitect.net';
  oSmtp.Password := 'testpassword';

  // Enable SSL connection
  oSmtp.SSL_init();
  oSmtp.SSL_starttls := 0; 
  oSmtp.ServerPort := 465;        

  ShowMessage('start to send email ...');

  if oSmtp.SendMail() = 0 then
    ShowMessage('email was sent successfully!')
  else
    ShowMessage('failed to send email with the following error: '
    + oSmtp.GetLastErrDescription());

end;

end.

Open in new window


reference: https://www.emailarchitect.net/easendmail/sdk/html/SSL_init.htm

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial