[C#] How to Send email with Meeting Calendar Reminder Winform
Giao diện demo ứng dụng:
Khi gởi email, chúng ta tích hợp lịch họp sẵn luôn vào Outlook mail.
FULL CODE:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net.Mail; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; using System.IO; namespace SendEmailWithMettingTime { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSendEmail_Click(object sender, EventArgs e) { DateTime start = Convert.ToDateTime(txt_start.Value); DateTime end = Convert.ToDateTime(txt_end.Value); var userName = "youremail@laptrinhvb.net"; var passWord = "BanAnhThao"; var isSuccess = SendEmailMetting(userName, passWord, txtToEmail.Text, "Demo Send Test Email", "Send email with event Metting", "Phòng họp văn phòng công ty", start, end); if (isSuccess) { MessageBox.Show("send mail is success"); } else { MessageBox.Show("send mail is fail"); } } private static string MeetingRequestString(string from, List<string> toUsers, string subject, string desc, string location, DateTime startTime, DateTime endTime, int? eventID = null, bool isCancel = false) { StringBuilder str = new StringBuilder(); str.AppendLine("BEGIN:VCALENDAR"); str.AppendLine("PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN"); str.AppendLine("VERSION:2.0"); str.AppendLine(string.Format("METHOD:{0}", (isCancel ? "CANCEL" : "REQUEST"))); str.AppendLine("BEGIN:VEVENT"); str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime.ToUniversalTime())); str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmss}", DateTime.Now)); str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime.ToUniversalTime())); str.AppendLine(string.Format("LOCATION: {0}", location)); str.AppendLine(string.Format("UID:{0}", (eventID.HasValue ? "laptrinhvb_" + eventID : Guid.NewGuid().ToString()))); str.AppendLine(string.Format("DESCRIPTION:{0}", desc.Replace("\n", "<br>"))); str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", desc.Replace("\n", "<br>"))); str.AppendLine(string.Format("SUMMARY:{0}", subject)); str.AppendLine(string.Format("ORGANIZER;CN=\"{0}\":MAILTO:{1}", from, from)); str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", string.Join(",", toUsers), string.Join(",", toUsers))); str.AppendLine("BEGIN:VALARM"); str.AppendLine("TRIGGER:-PT20M"); str.AppendLine("ACTION:DISPLAY"); str.AppendLine("DESCRIPTION:Reminder"); str.AppendLine("END:VALARM"); str.AppendLine("END:VEVENT"); str.AppendLine("END:VCALENDAR"); return str.ToString(); } public static bool SendEmailMetting( string fromEmail, string password, string toEmail, string Sub, string body, string location, DateTime start, DateTime end) { string CalendarContent = MeetingRequestString(fromEmail, new List<string>() { toEmail }, Sub , body, location,start, end); MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(fromEmail); mailMessage.Subject = Sub; mailMessage.Body = body; mailMessage.IsBodyHtml = true; mailMessage.To.Add(new MailAddress(toEmail)); AlternateView calendarView = AlternateView.CreateAlternateViewFromString(CalendarContent, new System.Net.Mime.ContentType("text/calendar")); calendarView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit; mailMessage.AlternateViews.Add(calendarView); try { var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = false; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromEmail, password); smtp.Timeout = 20000; smtp.Send(mailMessage); } return true; } catch (Exception ex) { Console.WriteLine(ex.Message); } return false; } private void Form1_Load(object sender, EventArgs e) { txt_start.Format = DateTimePickerFormat.Custom; txt_start.CustomFormat = "dd-MM-yyyy HH:mm"; txt_end.Format = DateTimePickerFormat.Custom; txt_end.CustomFormat = "dd-MM-yyyy HH:mm"; } } }
DOWNLOAD PROGRAMMER.
Nguồn : LapTrinhVB.Net