How To Send SMTP Mail Using PowerShell And Office 365

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys welcome to the channel uh today i'm going to show you how you can send email using microsoft powershot or windows powershot okay so from this lesson uh you will be able to send emr from any smtp server that you have so for example if you want to use gmail smtp server or you want to use your own personal mailing server as long as it supports smtp you will be able to send mail from it okay but for this uh tutorial we are going to use office 365 smtp server and as well for the receiving email will be office 365 so to begin and let me just show you so this is the turn on and this is the account that i'm going to use to test the email so for sending and as well for receiving okay so we can begin coding the code so the editor that i'm using uh is uh microsoft visual studio so you can use any editor that you want okay so we can begin uh scripting so the first variable that i'm going to define is for password i'm gonna call it pwd run and here we're going to store uh our password so this password is for the account that we're going to send the email from so i'm gonna specify a comment right here and call it password so the next thing will be to convert our plain password into a secure password to do that i'm going just to name it pwd and then call it 2 and then i'm going to call command line converts converts to secure string and then i'm going to pass in pwd 1 okay that is the password in plain text pw1 is the plain uh text and then we're going to use this command let to convert it into a secure string the parameter for this command let me as plain text and then enforce the change okay we have already defined uh the password uh for the email account that we're going to send email from so now we need another variable to host now the email account itself i'm going to start with the comment and call it email account to send email from variable i'm going to call it send mbx here the account i'm going to use that one so this is the account this one here is the mailbox i'm going to use to send email from and as well to send to myself okay so i'm going to define it as that you can add your email account there the next variable will be now the email account that we want to send to so i'm going to use same email account so for the common email account to receive email the variable name i'm going to call it receive mbx here i'm going to use the same email address so i'm going to send send to myself the email if you want to send to multiple users uh here on the receiving email account you can just add comma and then now you add the next the the other email account that you want to send an email to okay for my case i'm going to use one email account moving to next we need to define the body of our email so body variable should be now body and then i'm going just to type this is a test email from office 365 and just email from windows powershot partial application okay i'm going to call that the next thing now to define is the properties of the email itself so i'm going to call the variable as so let me start with the command and call it properties of the email okay so the first thing that we need to define right here is props that is the format of the properties of our email so the first pro is the smtp server smtp server for my case i'm using office 365 so it should be smtp.office365.com the next variable is from where we send the email from this i'm going to heard now from so it should be the send mbx variable we pass in that variable just like that the next is to where we're sending email to so they receive mbx variable we're going to pass it that way the next is subject of our email and here i just need to define the subject they are like testing email from passion that is the subject of our email the next property is part that we're going to use and this is five eighty-seven you can use two five as well a587 doesn't work the next property now is the credential credential will host uh the username and password to login to the account that we want to send email from so the way we define this is new new object and then we call system should be system.management that automates and if i'm not wrong dot ps ps credential we should have it there ps credential right there you should be into bracket and then here we can pass in the email address that we want to send email from and as well the password or the account that we want to send email from so here the variable should be send mbx comma and then the password and now for the password we need to specify the secure string password so it should be pw id underscore 2. like that now once you define your email properties the next thing is to call send message should be sent message command let and then from here we can pass in our call our property our email properties is alas shouldn't uh the r and m props this this one so we're not going to pass with the dollar we're going to pass in with earth and then props the name of the variable itself and since we didn't add our body within the probes which we can as well add i'm going to pass in the variable body because it's outside the properties block right there so here i'm just going to write body okay so with that now we can test uh script so one more thing i need to add my password here to login to this account unattended so you can add your password there once you add your password now you can copy your script and paste it in partial so i'm going to copy the entire script ctrl control c and then open posture window right here and then press the enter code and i will press enter okay we have an error and as you can see error in processing response a number is start tls is required to send email and i know why because we didn't office 365 always accepts email from a secure channel so we need to specify ssl as true so you know back in our variable in our code we can add another variable just here and call it use as a cell and then is to return it true now we can copy our code again control air control c go to partial let me clear the screen try to zoom out a little bit and paste the code then i press enter and then wait and as you can see it has completed successfully and now i can im open my mailbox go to inbox and as you can see i have received an email the subject is testing email from partial and we have the body this is a test email from windows partial application just as we had defined okay so you can see the email is coming from this account just as we had defined it and that's all sending to ourselves so if i can just go back it's the same so as you can see now our script is working so that's so simple you can implement uh a script that can be able to send email using any smtp server within powershot so if you want to use gmail you can define your smtp right here and call it smtp gmail.com so you just need to change the value of smtp server uh attribute okay so guys i hope you learned something from this tutorial today and thank you for watching bye bye
Info
Channel: Bernard Mwanza
Views: 12,502
Rating: undefined out of 5
Keywords:
Id: g_4KEtxpqEw
Channel Id: undefined
Length: 14min 50sec (890 seconds)
Published: Sat Jun 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.