23 September, 2023

[CSHARP] Email Meeting Calendar Reminder with meeting time reminder

Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẻ các bạn cách gởi email kèm theo lịch nhắc họp (Meeting Calendar Reminder) bằng ngôn ngữ C#, Winform.

[C#] How to Send email with Meeting Calendar Reminder Winform

[CSHARP] Email Meeting Calendar Reminder with meeting time reminder

Giao diện demo ứng dụng:
[CSHARP] Email Meeting Calendar Reminder with meeting time reminder
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 NOW

Chúc mọi người thành công.
Nguồn : LapTrinhVB.Net

Hướng dẫn bình luận

Mọi người để lại bình luận góp ý, nhận xét về những bài viết mà mình chia sẽ văn minh lịch sự hay kích động, Không spam, không chèn link quảng cáo, bán hàng, Không sử dụng từ ngữ thô tục, xúc phạm, kích động, Link chỉ được phép khi thực sự liên quan đến nội dung bài viết, Không mạo danh người khác hoặc sử dụng email giả, Bình luận vi phạm sẽ bị xóa không cần thông báo trước.
Mọi người lưu ý răng, nếu muốn chia sẽ code ở bình luần thì cần mã hóa code trước khi bỏ vào khung nhé. :)
⑴ Chèn ℂ𝕤𝕤 theo mẫu : [pre css] ℂ𝕤𝕤 [/pre]
⑵ Chèn ℍ𝕥𝕞𝕝 theo mẫu : [pre html] ℍ𝕥𝕞𝕝 [/pre]
⑶ Chèn 𝕁𝕒𝕧𝕒𝕤𝕔𝕣𝕚𝕡𝕥 theo mẫu : [pre js] 𝕁𝕒𝕧𝕒𝕤𝕔𝕣𝕚𝕡𝕥 [/pre]

🖼️ Chèn 𝕀𝕞𝕒𝕘𝕖 theo mẫu : [img] 𝕃𝕚𝕟𝕜 𝕀𝕞𝕒𝕘𝕖 [/img]
🎞️ Chèn Video 𝕐𝕠𝕦𝕥𝕦𝕓𝕖 theo mẫu : [youtube] 𝕃𝕚𝕟𝕜 𝕪𝕠𝕦𝕥𝕦𝕓𝕖 [/youtube]

Post a Comment

@Tags

.NET (84) ANDROID (7) API (11) ASP.NET (1) AUTOIT (4) BLOGGER (74) BOT (2) Cam-Xuc (5) CHROME (17) CPP (7) CSHARP (115) CSS (40) DEVEXPRESS (24) DOMAIN (3) DOWNLOAD (74) EXCEL (6) EXTENSION (6) FACEBOOK (26) FIREFOX (3) GOOGLE (25) HTML (48) IOS (3) JAVASCRIPT (52) MICROSOFT (16) NUGET (10) OPERA BROWSER (1) PDF (4) PHP (7) POWERSHELL (1) PROGRAM (33) PYTHON (13) SECURITY (2) SEO (10) SOFTWARE (21) TELEGRAM (1) TIPS (100) TOOLS (13) UPDATE (3) VISUAL STUDIO (16) VPN (1) WINDOWS (113) WORDPRESS (8) YOUTUBE (4) ZALO (5)