मैं here वर्णित Outlook के माध्यम से ईमेल भेजना चाहता हूं। यह तब तक ठीक काम करता है जब तक कि मैंने पहले ही Outlook खोला है। तो उदाहरण के लिए यदि Outlook कम हो गया है और मैं अपना कोड निष्पादित करता हूं, तो मैं एक ईमेल भेज सकता हूं। लेकिन अगर आउटलुक बंद कर दिया है, तो मैं एक अपवाद प्राप्त करें:यदि Outlook खुला है तो Outlook के माध्यम से केवल ईमेल भेज सकता है
{System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
at Microsoft.Office.Interop.Outlook._MailItem.get_Recipients()
at OutlookExample.Form1.btnSendEmail_Click(Object sender, EventArgs e) in C:\Users\abc\Documents\Visual Studio 2008\Projects\OutlookExample\OutlookExample\Form1.cs:line 28}
यहाँ कोड है:
using Outlook = Microsoft.Office.Interop.Outlook;
...
private void btnSendEmail_Click(object sender, EventArgs e)
{
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Hello, here is your message!";
oMsg.Subject = "This is a test message";
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]");
oRecip.Resolve();
oMsg.Send();
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
क्यों नहीं करता है यह काम करता है?
संपादित करें:
app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace ns = app.GetNamespace("MAPI");
f = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
Thread.Sleep(5000); // a bit of startup grace time.
अगर दृष्टिकोण खुला था यह इसे इस्तेमाल करता है, अगर नहीं है अपने इसे खोला: यहाँ समाधान
using Outlook = Microsoft.Office.Interop.Outlook;
...
private void btnSendEmail_Click(object sender, EventArgs e)
{
try
{
Outlook.Application oApp = new Outlook.Application();
// These 3 lines solved the problem
Outlook.NameSpace ns = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder f = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
System.Threading.Thread.Sleep(5000); // test
Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Hello, here is your message!";
oMsg.Subject = "This is a test message";
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]");
oRecip.Resolve();
oMsg.Send();
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Outlook का उपयोग न करें। इसके बजाय, System.Net.Mail का उपयोग करें। – SLaks
अच्छा सवाल। निश्चित रूप से यह अभी तक लॉग इन नहीं है? – BugFinder
स्लाक्स, मेरी इच्छा है। दुर्भाग्य से मैं वीबी 6 कोड बनाए रख रहा हूं और सी # में समस्या को दोहराया। –