using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void btnSend_Click(object sender, EventArgs e)
{
try
{
Outlook._Application _app = new Outlook.Application();
Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
mail.To = TextTo.Text;
mail.Subject = TextSubjet.Text;
mail.Body = TextMessage.Text;
mail.Importance = Outlook.OlImportance.olImportanceNormal;
((Outlook._MailItem)mail).Send();
MessageBox.Show("Sucesso","Message",MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
DataTable dt;
private void btnRecieve_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);
dt = new DataTable("Inbox");
dt.Columns.Add("Subject", typeof(string));
dt.Columns.Add("Sender", typeof(string));
dt.Columns.Add("Body", typeof(string));
dt.Columns.Add("Date", typeof(string));
dataGridView.DataSource = dt;
foreach (Outlook.MailItem item in inbox.Items)
dt.Rows.Add(new object[] { item.Subject, item.SenderName, item.HTMLBody, item.SentOn.ToLongDateString() + " " + item.SentOn.ToLongTimeString() });
}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < dt.Rows.Count && e.RowIndex >= 0)
webBrowser.DocumentText = dt.Rows[e.RowIndex]["Body"].ToString();
}
}
}
Erro:
Não é possível associar o objecto COM do tipo 'System.__ComObject' ao tipo de interface 'Microsoft.Office.Interop.Outlook.MailItem'. Esta operação falhou porque a chamada QueryInterface no componente COM para a interface com o IID '{00063034-0000-0000-C000-000000000046}'
falhou com o seguinte erro: Esta interface não é suportada (Excepção de HRESULT: 0x80004002 (E_NOINTERFACE)).