BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[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

Đăng nhận xét

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...