Peut seulement envoyer un email via Outlook si Outlook est ouvert
Que je veux utiliser pour envoyer des e-mails via Outlook comme décrit ici. Il fonctionne très bien tant que j'ai déjà ouvert des Perspectives. Donc par exemple si Outlook est réduite au minimum et j'exécute mon code, alors je peux envoyer un e-mail à l'amende juste. Mais si Outlook est fermé, puis-je obtenir une exception:
{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}
Voici le code:
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());
}
}
Pourquoi ne pas ce travail?
Edit: Voici la solution
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());
}
}
source d'informationauteur Kasper Hansen
Vous devez vous connecter pour publier un commentaire.
Le code suivant est fiable a travaillé pendant des mois pour moi:
si outlook est ouvert, il l'utilise, sinon sa a ouverte. Bien sûr, si votre outlook vous demande de vous connecter, votre code habitude. Certains systèmes, il devient difficile pour vous de connexion automatique.
Je n'aimais pas l'idée d'utiliser du Fil.Dormir pendant 5 secondes, j'ai donc trouvé une autre solution qui a fonctionné pour moi:
Tous vous avez besoin est d'obtenir Inspecteur de l'objet nouvellement créé
Réponse a été publiée dans Google groupes à l'origine pour Outlook 2007 (mais cela a fonctionné pour moi avec Outlook 2010)