-Begin-
preface
C # is an object-oriented general programming language launched by Microsoft. In addition to developing PC software, website (with the help of ASP.NET) and APP (based on Windows Phone), it can also be used as game script and write game logic. SMTP is a protocol that provides reliable and effective e-mail transmission. It is a mail service based on FTP file transmission service. It is mainly used for mail information transmission between systems and provides notification of letters. Today, I'd like to share with you how to realize alarm notification through c# combined with SMTP.
thinking
C# combined with SMTP to realize email alarm notification. After analysis, we need to solve the following two problems:
The first problem is how to detect the alarm, and the second problem is how to send the mail. Next, we mainly analyze these two points in detail.
Alarm detection
1. In practical application, there are two kinds of alarms: discrete alarm (i.e. switching value alarm) (data acquisition plus VX: xiketang777) and conditional alarm (i.e. data alarm). For switching value alarm, it is divided into rising edge detection and falling edge detection; For conditional alarms, there are generally high limit alarm detection, high high alarm detection, low limit alarm detection and low low alarm detection.
2. For different alarms, the detection methods will be different, but the detection principle is the same. It is mainly to capture the moment when the alarm occurs, such as the rising edge of discrete alarm. In fact, it is necessary to detect the jump of the variable value from False to True. For the upper limit of conditional alarm, it is to capture that the actual value of the variable is greater than (or equal to) Alarm high limit this state. Therefore, the alarm detection needs to open a separate thread for real-time detection, so that the alarm information will not be missed. The accuracy of alarm detection is determined by the amount of alarm detection and the computer performance itself.
3. Since the alarm needs to be realized in combination with configuration information, it can not be displayed directly in code. The whole process is combed in combination with rising edge alarm and high limit alarm, as shown in the following figure:
Mail class writing
For C#, it is very convenient to implement email notification. You can directly call the relevant class library to implement it. Here, we mainly encapsulate the class library in the form of an EmailHelper class. Later, you only need to call this EmailHelper. In the EmailHelper class, you first create some attributes that will be used for email sending (get information plus VX: xiketang777) Including sender, recipient, title, content, sender password and other information, as shown in the figure below:
/// <summary> /// sender /// </summary> public string mailFrom { get; set; } /// <summary> /// addressee /// </summary> public string[] mailToArray { get; set; } /// <summary> /// CC /// </summary> public string[] mailCcArray { get; set; } /// <summary> /// title /// </summary> public string mailSubject { get; set; } /// <summary> /// text /// </summary> public string mailBody { get; set; } /// <summary> /// Sender password /// </summary> public string mailPwd { get; set; } /// <summary> /// SMTP mail server /// </summary> public string host { get; set; } /// <summary> /// Whether the body is in html format /// </summary> public bool isbodyHtml { get; set; } /// <summary> /// enclosure /// </summary> public string[] attachmentsPath { get; set; }
After creating the attribute, write a method to send mail. The logic of the mail sending method is consistent with that of sending mail normally. The steps are as follows:
1. Initializes the MailAddress instance with the specified mail address
2. Adds a mail address to the recipient address collection
3. Add a mail address to the CC recipient address collection
4. Set sender information
5. Add attachments with attachments
6. Specify the sender's email address and password to verify the sender's identity
7. Set up SMTP mail server
8. Send mail to SMTP mail server
The specific code is as follows:
/// <summary> /// Send mail /// </summary> /// <returns></returns> public bool Send() { //Initializes the MailAddress instance with the specified mail address MailAddress maddr = new MailAddress(mailFrom); //Initialize MailMessage instance MailMessage myMail = new MailMessage(); //Adds a mail address to the recipient address collection if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //Add a mail address to the CC recipient address collection if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } //sender address myMail.From = maddr; //Email title myMail.Subject = mailSubject; //The encoding used for the subject content of the email myMail.SubjectEncoding = Encoding.UTF8; //Email body myMail.Body = mailBody; //Encoding of e-mail body myMail.BodyEncoding = Encoding.Default; myMail.Priority = MailPriority.High; myMail.IsBodyHtml = isbodyHtml; //Add attachments with attachments try { if (attachmentsPath != null && attachmentsPath.Length > 0) { Attachment attachFile = null; foreach (string path in attachmentsPath) { attachFile = new Attachment(path); myMail.Attachments.Add(attachFile); } } } catch (Exception err) { throw new Exception("There was an error adding an attachment:" + err); } SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; //Specify the sender's email address and password to verify the sender's identity smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd); //Set up SMTP mail server smtp.Host = host; try { //Send mail to SMTP mail server smtp.Send(myMail); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } }
Mail sending
1. Here, you should first set up the mailbox, open the following services according to the prompts, and a password will be provided after opening. Therefore, the password used for sending mail is not the mailbox login password, but a password generated after opening the following services.
2. In order to facilitate the construction of the overall environment, I use the Xinge education host computer configuration software (CMSPro) (data acquisition plus VX: xiketang777) combined with Siemens S7-1200 PLC for function test. First, configure some variables through CMSPro, as shown in the figure below:
3. Alarm configuration is performed for each variable. Take the rising edge of M8.0 configuration as an example:
4. In the same way, configure other variables for corresponding alarms. After configuration, click save and run:
5. It can be seen from the above figure that the current M8.0 is False. Set M8.0 manually, or set M8.0 through TIA botu software. At this time, an alarm email will be received in the mailbox, and then reset M8.0. An alarm elimination email will be received in the mailbox again, as shown in the following figure:
-END-