In this tutorial I will show you how to use Beckhoff TwinCAT6350 to send an email to Outlook server.
Installation
Please access this link and Download the install packages,
Next>.
Accept the license and Next>.
Enter your User information and Next>.
Choose Complete and Next>.
Next>.
Press Install to start your installation.
Please wait a mins..
Done!
You can see a folder named “TF6350-SMS-SMTP” in TwinCAT/Functions.
System Requirment
Configure Your Email
Outlook
Outlook is used here.Login to your Outlook account and press the Gear icon.
Press the Button that Red Rectangle is pointed to.
Go to Settings>Main>Sync email.
Choose Pop options to Yes and you will see the SMTP Settings Field in the bottom.
Here is the setting of the connection to SMTP Server.
Please reference this link to get more information for the setting.
https://www.gmass.co/blog/outlook-smtp/
Why not Use Gmail?
From 2022-5-30, GMAIL is not allowed to use Less secure apps to connect to their Mail Server.
https://support.google.com/accounts/answer/6010255?hl=en
Configuration
Here is the Configuration in this tutorial. We are just using TwinCAT3 TF6350 to send an email from our Email account to another email account.
Function Block
I will only explain the Function Block that is used in this tutorial.
FB_SmtpV3
In this Tutorial, FB_SMTPV3 is used.
VAR_INPUT
sNetId | T_AmsNetID | TwinCAT SMTP Server’s AmsNetID |
sSmtpServer | T_MaxString | SMTP Server/IP |
sUsername | T_MaxString | SMTP Server’s UserNameIn this case, it is your user name of the outlook account.e |
sPassword | T_MaxString | SMTP Server’s PasswordIn this case, it is your password of the outlook account.e |
nEncryption | UDINT | 0=NONE, 1=STARTTLS, 2=SSL |
sFrom | T_MaxString | The Sender Email Address,In this case, it is your Outlook address. |
sTo | T_MaxString | The Receiver Email Address,You need to configure at least one Receiver.. |
sCc | T_MaxString | The CC Receiver Email Address |
sBcc | T_MaxString | The Bcc Receiver Email Address |
sSubject | T_MaxString | The Subject of your mail |
pMessage | POINTER TO STRING | The Memory address of your Mail Content VariableExampleMail send at Thu, 23 Mar 200602:31:44 -0800 |
cbMessage | UDINT | The Size of the your mail Content, Count by bytes |
bExecute | BOOL | Execute by Rise Edge |
tTimeout | TIME := T#20s | Timeout 設定 |
VAR_OUTPUT
bBusy | BOOL | True=Executing |
bError | BOOL | True=Function Block Error |
nErrId | UDINT | Error Information |
Implementation
Now we will start to make a simple program.
VAR
Variables used in our program are defined in the VAR area.
Message is the content of your email.
PROGRAM MAIN VAR SmtpV3:FB_SmtpV3; Message:STRING; bSend:BOOL; END_VAR |
PROGRAM
Please set the nEncryption=1.It is because STARTTLS is used in the Outlook SMTP Setting.
For the sFrom parameter,you only need to enter your outlook address.and the Function Block will execute while bSend is True.
Message:=’Hello! this is a test!’; SmtpV3( sSmtpServer:=’smtp.office365.com’ ,sUsername:=’YourOutlookAccount_Username’ ,sPassword:=’YourOutlookAccount_Password’ ,nEncryption:=1 ,sFrom:=’YourOutlook_Email’ ,sTo:=’The receiver’ ,sSubject:=’Test subject’ ,pMessage:=ADR(Message) ,cbMessage:=SIZEOF(Message) ,bExecute:=bSend ); IF bSend AND NOT SmtpV3.bBusy THEN bSend:=FALSE; END_IF |
Result
You can receive the email 🙂
Sample Code
Summary
Finally, when I was looking for information to send to Gmail or Outlook on the Internet, there are so many websites that are roughly similar. But these sites haven’t touched any settings on the Gmail side or the Outlook side. I felt a little irresponsible there. If it’s a memo blog, it’s still good, but if it’s obviously a tutorial-based site, It should be more detailed.
My goal is to make it easy for all readers.