FB_SmtpV2

FB_SmtpV2 1:

The block sends a byte stream to a remote ADS device via ADS. The TwinCAT ADS Smtp service must be running on the remote ADS device, so that the byte stream can be received and processed into an e-mail. Once the byte stream has been processed the e-mail is sent.

VAR_INPUT

VAR_INPUT
    sNetId      : T_AmsNetID;   (* AmsNetID *)
    sSmtpServer : T_MaxString;  (* Smtp Server addres ( IP or Name) *)
    sUsername   : T_MaxString;  (* Smtp Username *)
    sPassword   : T_MaxString;  (* Smtp Password *)
    nAuth       : UDINT;       (* Smtp Auth Type *)
    sFrom       : T_MaxString;  (* Sender stzring *)
    sTo         : T_MaxString;  (* To recipient string *)
    sCc         : T_MaxString;  (* Cc recipient string *)
    sBcc        : T_MaxString;  (* Bcc recipient string *)
    sSubject    : T_MaxString;  (* Subject string *)
    pMessage    : DWORD;       (* Pointer to the message *)
    cbMessage   : UDINT;       (* Messagelenght in byte to send *)
    bExecute    : BOOL;
    tTimeout    : TIME := T#20s;
END_VAR

sNetId: AmsNetID on which the TwinCAT Smtp server runs.

sSmtpServer: Name or IP of the Smtp server.

sUsername: Username for the Smtp Server.

sPassword: Password for the Smtp Server.

nAuth: Smtp Auth Type:
0 = AUTH NONE
1 = RESERVED
2 = AUTH LOGIN
3 = AUTH NTLM
4 = AUTH PLAIN

sFrom: A string containing the e-mail address of the sender. A sender must be specified. The string is limited to 255 characters.

sTo: A string containing the e-mail address of the recipient. Several addresses can be specified, separated by semicolon. At least one recipient has to be specified. The string is limited to 255 characters.

sCc: A string containing an e-mail address of a further recipient (cc=carbon copy). This string can also be empty. A copy of the e-mail is sent to this recipient. The e-mail address of this recipient is visible to other recipients. It is possible to enter multiple recipient addresses separated by semicolons. The string is limited to 255 characters.

sBcc: A string containing the e-mail address of a further recipient (Bcc = blind carbon copy). This string can also be empty. A copy of the e-mail is sent to this\these recipient\s. The e-mail address of this recipient is not visible to other recipients. It is possible to enter multiple recipient addresses separated by semicolons. The string is limited to 255 characters.

sSubject: A string containing the subject line for the e-mail. The e-mail may be sent without subject, in which case the name of the sending computer is automatically entered in the subject line (e.g. "Mail sent from: CX_00762C"). The string for the subject line is limited to 255 characters.

pMessage: The address (a pointer) to a null-terminated string containing the e-mail text. The e-mail may be sent without body text, in which case the date and time are entered automatically (e.g. "Mail sent at: Thu, 23 Mar 2006 02:31:44 -0800"). The address of the string can be determined with the ADR operator.

cbMessage: Length of the e-mail text. The length can be determined through the LEN operator.

bExecute: The function block is activated by a rising edge at this input.

tTimeout: Maximum time allowed for the execution of the command.

VAR_OUTPUT

VAR_OUTPUT
    bBusy   : BOOL;
    bError  : BOOL;
    nErrId  : UDINT;
END_VAR

bBusy: the output variable remains TRUE until the function block has executed a command, but only until tTimeOut has expired.

bError : the output variable is switched to TRUE as soon as an error occurs during the execution of the command. The command-specific error is contained in iErrorId.

nErrId: contains the command-specific error code of the most recently executed command (see table).

FB_SmtpV2 2:
  • Make sure that you do not use \o within the byte-arrays, otherwise the message will be stopped.
  • The maximum number of characters in a message is 510,725 - in total you have 1275 characters for From, To, Cc, Bcc and Subject.

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v2.8.0 and above

 

TcSmtp.lib

Sample in ST:

PROGRAM MAIN
VAR
    FB_SmtpV2   : FB_SmtpV2;
    bSend       : BOOL;
    bBusy       : BOOL;
    bError      : BOOL;
    nErrID      : UDINT;
    sServer     : STRING := 'smtpserver';
    sMsg        : STRING := 'TcSmtpSrv is working properly';
    sSubject    : STRING :=  'TcSmtp Service Test';
    sUser       : STRING := 'username';
    sPassword   : STRING := 'password';
    sFrom       : STRING := 'emailfrom';
    sTo         : STRING := 'emailto';
    nAuth       : INT := 2;
END_VAR
FB_SmtpV2 (
    sNetId:= '',
    sSmtpServer:= sServer,
    sUsername:= sUser,
    sPassword:= sPassword,
    nAuth:= nAuth,
    sFrom:= sFrom,
    sTo:= sTo,
    sSubject:= sSubject,
    pMessage:= ADR(sMsg) ,
    cbMessage:= LEN(sMsg)+1,
    bExecute:= bSend,
    tTimeout:= t#20s,
    bBusy=> bBusy,
    bError=> bError,
    nErrId=> nErrID);