- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [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
[C#] Hướng dẫn sử dụng thư viện Outlook Interop để gởi và nhận email
Xin chào các bạn đọc, bạn viết hôm nay, mình sẽ tiếp tục hướng dẫn các bạn cách gởi và nhận email sử dụng thư viện Outlook C# trong bộ office của microsoft bằng ngôn ngữ lập trình C#.
[C#] Send and Receive Email with Outlook Library
Bài viết hướng dẫn này, yêu cầu các bạn đã cài sẵn Microsoft Outlook trên máy tính và đã cấu hình các thông số email sender của bạn vào ứng dụng trước.
Ví dụ: các thông tin cấu hình email, mật khẩu, địa chỉ pop server, smtp server và các cổng, các bạn phải cấu hình trên ứng dụng Outlook.
Dưới đây là giao diện demo ứng dụng gởi và nhận email C#:
- Đầu tiên, các bạn cần import thư viện Microsoft.Office.Interop.Outlook
Các bạn, có thể import thư viện như hình bên dưới:
Sau khi, import thư viện vào xong, chúng ta sẽ code import thư viện vào project của mình.
using outlook = Microsoft.Office.Interop.Outlook;
1. Viết code gởi email
private void btnSend_Click(object sender, EventArgs e)
{
outlook.Application app = new outlook.Application();
outlook.MailItem mail = (outlook.MailItem)app.CreateItem(outlook.OlItemType.olMailItem);
mail.To = txt_to.Text;
mail.Subject = txt_subject.Text;
mail.Body = txt_message.Text;
mail.Importance = outlook.OlImportance.olImportanceHigh;
((outlook.MailItem)mail).Send();
MessageBox.Show("Đã gởi Email đến " + txt_to.Text + " thành công!");
}
2. Viết code nhận email vào datagridview C#
private void btnReceive_Click(object sender, EventArgs e)
{
try
{
outlook.Application app = new outlook.Application();
outlook.NameSpace ns = app.GetNamespace("MAPI");
outlook.MAPIFolder inbox = ns.GetDefaultFolder(outlook.OlDefaultFolders.olFolderInbox);
ns.SendAndReceive(true);
table = new DataTable();
table.Columns.Add("Subject", typeof(string));
table.Columns.Add("Sender", typeof(string));
table.Columns.Add("Body", typeof(string));
table.Columns.Add("Date", typeof(string));
dataGrid.DataSource = table;
foreach (outlook.MailItem item in inbox.Items)
{
table.Rows.Add(new object[] { item.Subject, item.SenderName, item.HTMLBody, item.SentOn.ToLongDateString() + " " + item.SentOn.ToLongTimeString() });
}
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
}
3. Viết sự kiện khi click vào từng dòng trên DataGridview thì hiển thị nội dung của email
private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < table.Rows.Count && e.RowIndex >= 0)
{
webBrowser1.DocumentText = table.Rows[e.RowIndex]["Body"].ToString();
}
}
Chúc các bạn thành công, nếu thấy hay nhớ like và share nhé các bạn.
Các bạn có thể download source ở link bên dưới.