สร้างระบบ notify service ด้วย c#.net class sendmail
มาสร้างการ Notify Message ให้กับหลายๆ Application ของเรากันเถอะ เพื่อนๆคงอยากให้โปรแกรมของเราที่ทำงานอยู่ทุกวัน หรือมีการตั้ง Scheduler รันแบบอัตโนมัติเป็นแบบ Daily, Weekly, Monthly สามารถ Alert message หรือ Notify message เพื่อแจ้งให้เราทราบสถานะการทำงาน หรือสรุปการทำงานของโปรแกรมโดยที่เราเขียน Notify Service ขึ้นมาด้วยเทคโนโลยีที่เรียกว่า Web service ซึ่งจะทำให้โปรแกรมสามารถเรียกใช้งานร่วมกันได้ทั้งองค์กร
- สร้าง Class Send Mail ด้วย Web service ดังนี้
1.1 สร้าง Class Status เพื่อใช้เป็น Object ในการเก็บค่า Status และ Description web service ชื่อไฟล์ Status.csusing System; using System.Collections.Generic; using System.Web; using System.Xml.Serialization; public class Status { private bool m_isSuccess; private string m_description; #region properties public bool IsSuccess { get { return m_isSuccess; } set { m_isSuccess = value; } } [XmlElement(IsNullable = true)] public string Description { get { return m_description; } set { m_description = value; } } #endregion public enum StatusType { OK = 0, RecordNotFound, UnableToInsert, UnableToUpdate, UnableToDelete }; public Status() { } public Status(bool isSuccess, string description) { m_isSuccess = isSuccess; m_description = description; } }
1.2 สร้าง Class SendMailResponse เพื่อใช้เป็น Object ในการ Return ค่าสถานะในการทำงานของ web service ชื่อไฟล์ SendMailResponse.cs
using System; using System.Collections.Generic; using System.Web; public class SendMailResponse { public SendMailResponse() { } public Status OperationStatus; public SendMailResponse(Status status) { OperationStatus = status; } }
1.3 สร้าง Class SendMailService จุดที่น่าสังเกต BodyFormat เซตเป็น MailFormat.Html เพื่อรับและส่งเมลย์เป็น Htmlและค่าใน Mail Body ก็ทำให้เป็น Text Html เมลย์เราก็จะแสดงผลตาม Html ที่เราออกแบบอย่างสวยงาม ^_^
using System; using System.Collections.Generic; using System.Web; using System.Net.Mail; using System.Net.Mime; using System.Net; using System.Web.Mail; public class SendMailService { public SendMailService() { } public SendMailResponse SendEmail(string MailServer, string From, string MailTo, string Cc, string subject, string Body) { SendMailResponse resp = new SendMailResponse(); Status result = new Status(); try { System.Web.Mail.MailMessage MyMail = new System.Web.Mail.MailMessage(); MyMail.From = From; MyMail.To = MailTo; MyMail.Subject = subject; MyMail.Body = getTemplate(subject, Body) ; MyMail.Cc = Cc; MyMail.BodyFormat = MailFormat.Html; MyMail.Priority = System.Web.Mail.MailPriority.High; SmtpMail.SmtpServer = MailServer; SmtpMail.Send(MyMail); result.IsSuccess = true; result.Description = "Success"; } catch (Exception ex) { result.IsSuccess = false; result.Description = ex.Message; } resp = new SendMailResponse(result); return resp; }
1.4 หน้าตา Web Service ที่เราได้จะเป็นดังรูป
- วิธีการเรียกใช้งาน NotifyServices ที่เราสร้างขึ้นมาสามารถทำได้ดังนี้
- สมมุติผมสร้าง APP1 ขึ้นมาแล้วทำการ Add Service Reference เจ้าตัว NotifyServices จาก url ตามรูปหากเรามี web server ก็ Add จาก url server ที่เราเอา webservice ไปวางแล้วทำการคลิก OK ดังรูป
- ทดลองเขียนโปรแกรมเรียกใช้ Notify Service ของเราได้ตามโค้ดด้านล่าง
ServiceNotify.SendMailResponse objResp = new ServiceNotify.SendMailResponse(); ServiceNotify.notifierSoapClient service = new ServiceNotify.notifierSoapClient(); objResp = service.SendMailCommand("MailServer", "source@gmail.com", "destination@gmail.com", "", "Subject", "TextBody");
ข้อดีของการทำเป็น web service คือเราเขียนขึ้นมาครั้งเดียวสามารถเรียกใช้ได้จากหลากหลาย application หรือจากหลากหลาย platfrom สามารถดาวน์โหลดโค้ดตามลิ้ง https://autosoft.in.th/sourcecode/NotifyServices.zip
- สมมุติผมสร้าง APP1 ขึ้นมาแล้วทำการ Add Service Reference เจ้าตัว NotifyServices จาก url ตามรูปหากเรามี web server ก็ Add จาก url server ที่เราเอา webservice ไปวางแล้วทำการคลิก OK ดังรูป