- [DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF
- [C#] Thêm ứng dụng vào Taskbar sử dụng SharpShell DeskBand
- [C#] Hướng dẫn thêm text vào hình ảnh icon winform
- [C#] Chia sẽ tổng hợp source code đồ án về Csharp
- [C#] Hướng dẫn viết ứng dụng quay màn hình video winform, Screen Recorder
- [C#] Hướng dẫn sử dụng thư viện Input Simulator để làm việc với Keyboard, Mouse Virtual
- [DEVEXPRESS] Hướng dẫn tích Filter Contain khi click chuột phải vào cell selection trên Gridview
- [C#] Tra cứu mã số thuế cá nhân bằng CMND hoặc CCCD
- [C#] Convert hình ảnh image thành Blurhash sử dụng trong loading image winform
- [POWERSHELL] Script sao lưu backup và nén database sqlserver
- [C#] Giới thiệu thư viện Autofac Dependency Injection
- [C#] Hướng dẫn tạo Windows Services đơn giản Winform
- [C#] Một click chuột điều khiển máy tính từ xa sử dụng Ultraviewer
- Hướng dẫn đóng gói phần mềm sử dụng Powershell biên dịch script thành file exe
- [C#] Hướng dẫn sử dụng Task Dialog trên NET 5
- [C#] Hướng dẫn xem lịch sử các trang web đã truy cập trên Chrome Browser
- [C#] Hướng dẫn lấy thông tin Your ID và Password của Ultraviewer Winform
- [C#] Hướng dẫn lấy thông tin Your ID và Password của Teamviewer Winform
- [C#] Hướng dẫn build code động xuất file exe trên Winform
- [C#] Điều khiển ứng dụng từ xa và rất xa với Telegram Bot Winform
[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.