- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
- [C#] Thiết kế ứng dụng Console đẹp với thư viện Spectre.Console
- [C#] Thiết kế ứng dụng Single Instance và đưa ứng dụng lên trước nếu kiểm tra ứng dụng đang chạy
- [C#] Giới thiệu JSON Web Token và cách đọc chuỗi token
- [C#] Cách tăng giảm font chữ tất cả các control trên winform
- [DEVEXPRESS] Tích hợp chức năng Tìm kiếm Search vào CheckedComboboxEdit
- [C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
- [C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không
- [C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui
- [C#] Hướng dẫn tạo mã VietQR Payment API Winform
- [C#] Sử dụng thư viện BenchmarkDotNet đo hiệu năng của hảm Method
- [DEVEXPRESS] Tìm kiếm không dấu tô màu highlight có dấu trên C# Winform
[C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
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
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.
Source code C#:
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";
}
}
}
Thanks for watching!