Friday, January 18, 2019

Send mail using Office365 Auth from MAILx





Send mail using Office365 Auth from MAILx command,

Step-1
mkdir ~/.certs

Step-2
certutil -N -d ~/.certs

It will ask for password, enter password which will be used to encrypt your keys.
The password should be at least 8 characters long, and should contain at least one non-alphabetic character.

Step-3
echo -n | openssl s_client -starttls smtp -crlf -connect smtp.office365.com:587| sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/365.crt
certutil -A -n "Google Internet Authority" -t "C,," -d ~/.certs -i ~/.certs/365.crt

Step-4
vim  /mail-office365.sh
Now create a file with below content to send mail, Or add below code in your script

echo -e "<Body>" | mailx -v -s "<Subject>" -a "<Attachment>" \ 
-S smtp-auth=login \ 
-S smtp-use-starttls \ 
-S nss-config-dir=~/.certs \ # CERTIFICATE FILE PATH 
-S smtp=smtp.office365.com:587 \ # SMTP SERVER DETAILS PORT 
-S from="xxx@o365.com" \ # FROM MAIL ID 
-S smtp-auth-user=xxx@o365.com \ # SMTP USER ID 
-S smtp-auth-password="xxxxxxxx" \ # SMTP PASSWORD 
-S ssl-verify=ignore \
mmmm@gail.co.in # TO MAIL ID

Step-5
chmod +x /mail-office365.sh
sh /mail-office365.sh
Execute created script to send mail.

Sample Output :

Resolving host smtp.office365.com . . . done.
Connecting to 40.100.0.34 . . . connected.
220 HK2PR01CA0015.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 12 Apr 2016 12:34:02 +0000
>>> EHLO SL-APP-SRV001
250-HK2PR01CA0015.outlook.office365.com Hello [124.43.128.4]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250 CHUNKING
>>> STARTTLS
220 2.0.0 SMTP server ready
Error in certificate: Peer's certificate issuer is not recognized.
Comparing DNS name: "outlook.com"
Comparing DNS name: "*.outlook.com"
Comparing DNS name: "office365.com"
Comparing DNS name: "*.office365.com"
SSL parameters: cipher=AES-256, keysize=256, secretkeysize=256,
issuer=CN=Microsoft IT SSL SHA2,OU=Microsoft IT,O=Microsoft Corporation,L=Redmond,ST=Washington,C=US
subject=CN=outlook.com,OU=Microsoft Corporation,O=Microsoft Corporation,L=Redmond,ST=WA,C=US
>>> EHLO SL-APP-SRV001
250-HK2PR01CA0015.outlook.office365.com Hello [124.43.128.4]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-AUTH LOGIN
250-8BITMIME
250-BINARYMIME
250 CHUNKING
>>> AUTH LOGIN
334 VXNlcm5hbWU6
>>> amtzdG9ja0Bqa2ludHJhbmV0LmNvbQ==
334 UGFzc3dvcmQ6
>>> cGFzc0B3b3JkMw==
235 2.7.0 Authentication successful target host TY1PR04MB0717.apcprd04.prod.outlook.com
>>> MAIL FROM:<xxx@o365.com>
250 2.1.0 Sender OK
>>> RCPT TO:<mmmm@gail.co.in>
250 2.1.5 Recipient OK
>>> DATA
354 Start mail input; end with <CRLF>.<CRLF>
>>> .
250 2.6.0 <570cec0b.89+hyy6E/AJwNI8H%xxx@o365.com> [InternalId=38160784428382, Hostname=TY1PR04MB0717.apcprd04.prod.outlook.com] 2623 bytes in 0.471, 5.435 KB/sec Queued mail for delivery
>>> QUIT

221 2.0.0 Service closing transmission channel



ENJOY! and Stay connected.

1 comment :