So, this may be a silly question (or something that's impossible), but I just wanted to ask in case anyone knows something. I'm trying to open Outlook (either through Office 365- Browser or through the Outlook Desktop Application) to compose an email. I got that working fine. What I want to know is, if there's a way that I can capture what was composed (like, Body, To, Subject, Attachments) in my WPF application so that I can update it on my end. Do you guys think if this is possible?
Here's the sample code I have: (For opening this in the browser)
string To = "abc#ftr.com";
string subject = "Test Email";
string body = "This is a test email, Please ignore";
string url = #"https://outlook.office.com/?path=/mail/action/compose&to=" + To + "&subject=" + subject + "&body=" + body;
System.Diagnostics.Process.Start(url);
And here's the code for opening it in Outlook Desktop App:
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.Subject = "subject something";
oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
oMsg.HTMLBody = "Test Email";
oMsg.Attachments.Add("c:/temp/test.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
oMsg.Display(true);
Thank you!
You can hook the ItemSend event, which will give you a reference to the MailItem object that is about to be send. Here is some sample code I copied from the Microsoft Community Forums
public void SendEnMail(Office.IRibbonControl control) //OnAction Function
{
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem myMail = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
myMail.Display(true);
Outlook.Application application = Globals.ThisAddIn.Application;
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
System.Windows.Forms.MessageBox.Show(a);
Cancel = true;
}
Related
I'm working on a Selenium test which generates a report and then sends the report to an email address when it's finished. The script works flawlessly up until I have to send the email report.
The weird thing is that if I run the script from inside Visual Studio, the email sends fine, but when I build the solution and then set the script to run automatically from the task schedule it fails. The rest of the script runs fine, the report gets generated, it just doesnt send the email.
I'm not a Visual Studio expert, so I'm thinking it may possibly be something in my settings.
Here's the code to send the email using Outlook:
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Please find the attached Contract Remaining Hours report for the week of " + DateTime.Today.ToString("D");
String sDisplayName = fileName;
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = oMsg.Attachments.Add
(pathToFile + fileName, iAttachType, iPosition, sDisplayName);
oMsg.Subject = "Contract Remaining Hours Report " + DateTime.Today.ToString("D");
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("tom.depiera#tbs.toshiba.com");
oRecip.Resolve();
oMsg.Send();
I tried to open outlook window with body to send a message.
When I works with my local it's opening perfectly. But when I work with IIS the outlook window is not opening.
public static void SendMailInOutlook(Page CurrentPage, Type ScriptType,
string Subject, string Recipients, Dictionary<string, string> emailInputs,
string emailTemplatePath)
{
static Microsoft.Office.Interop.Outlook.Application outlookApp;
static Microsoft.Office.Interop.Outlook._MailItem mailItem;
outlookApp = new Microsoft.Office.Interop.Outlook.Application();
mailItem = (Microsoft.Office.Interop.Outlook._MailItem)outlookApp.CreateItem(
Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailItem.Subject = Subject;
mailItem.To = Recipients;
mailItem.HTMLBody = SendMail.GetEmailContent(emailInputs, emailTemplatePath);
Thread newThread = new Thread(SendEmailToOutLook);
newThread.Start();
}
static void SendEmailToOutLook()
{
string sMsg = "Email sent successfully.";
try
{
mailItem.Display(true);
}
catch (Exception ex)
{
sMsg = ex.Message;
}
}
And it won't.
Your codebehind is executed on the web server, NOT on the client machine. So when you develop locally, and run the web app locally, that code is executed on the same machine, hence "working".
But once you deploy, the "Outlook code" is executed on a different machine than the one the browser is on. It cannot work.
The most common approach to this requirement is a "mailto link", the likes of <a href='mailto:a#b.com'...
I am looking to generate an Outlook message from within my program, I am able to build and send from within the program or build and save, what I would like is to build then display to allow the user to manually select recipients from the AD listings... The code below is a mixup of samples here and other tutorial sites however none I can find just build then "display" the email without saving a draft or sending it from within the program...
also I am looking to find a way i can create a UNC link inside of an email IE: write out a path to the users folder \\unc\path\%USERNAME% or the likes
private void sendEmailOutlook(string savedLocation, string packageName)
{
try
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Attached is the required setup files for your <i><b>soemthing</i></b> deployment package.";
oMsg.HTMLBody += "\nPlease save this file to your network user folder located.<br /><br/>\\\\UNC\\data\\users\\%USER%\\";
oMsg.HTMLBody += "\nOnce saved please boot your Virtual machine, locate and execute the file at <br /> <br />\\\\UNC\\users\\%USER%\\";
int pos = (int)oMsg.Body.Length +1;
int attachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add(savedLocation, attachType, pos, packageName);
oMsg.Subject = "something deployment package instructions";
oMsg.Save();
}
catch(Exception ex)
{
Console.WriteLine("Email Failed", ex.Message);
}
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.Subject = "something deployment package instructions";
oMsg.BodyFormat = OlBodyFormat.olFormatHTML;
oMsg.HTMLBody = //Here comes your body;
oMsg.Display(false); //In order to display it in modal inspector change the argument to true
Regarding the link to the folder you should be able to use(in case that you know User Name):
Link
A lot of companies have their employees user names attached to address entries (looks something like "John Doe(Jdoe)" where Jdoe is a username).
when your user select a recipients or tries to send the email you could catch those event, and do something like
foreach (Outlook.Recipient r in oMsg.Recipients)
{
string username = getUserName(r.Name);// or r.AddressEntry.Name instead of r.Name
oMsg.HTMLBody += "<a href='C:\\Users\\" + username + "'>Link</a>"
}
oMsg.Save();
oMsg.Send();
where getUserName() is a method that extracts only the userName (Could use substring or RegEx).
Make sure that mail's body is a valid HTML
/n won't give you a new line you should use <br> insted.
i have written a code that will read all unread mails from Outlook 2010 and write them in a file. After that i want to change the status of the mails as Read in outlook.
How do i do it?
I am using Interop for accessing the mails.
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.Items unreadItems = null;
app = new Microsoft.Office.Interop.Outlook.Application();//.CreateItem(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
ns = app.GetNamespace("MAPI");
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
unreadItems = inboxFolder.Items.Restrict("[Unread]=true");
Here are some link that could help you:
http://msdn.microsoft.com/en-us/library/aa289167%28VS.71%29.aspx (it is for Outlook 2003 but it still contains very usefull info)
http://msdn.microsoft.com/en-us/library/ms268731%28VS.80%29.aspx
I've a code sample that could help you:
OutLook.Application oApp;
OutLook._NameSpace oNS;
OutLook.MAPIFolder oFolder;
OutLook._Explorer oExp;
oApp = new OutLook.Application();
oNS = (OutLook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
OutLook.Items items = oFolder.Items;
foreach (OutLook.MailItem mail in items)
{
if (mail.UnRead)
{
mail.UnRead = false;
mail.Save();
}
Marshal.ReleaseCOMObject(mail);
}
Marshal.ReleaseCOMObject(items);
// Dont forget to free all other object, using Marshal.ReleaseCOMObject then close oApp
Please note I've not tested if it works or even compile.
On of general rule with outlook dev is that you need to release ALL com object otherwise you can have strange behavior (save popup when closing outlook app, or even the outlook never close etc.)
EDIT:
I would advice to you indeed use the Restrict method to get only unred mail, because my snippet above will loop in all emails which could be unnecessary and not performant.
Set the OutLook.MailItem's UnRead property to false
I have a WinForms app that at the click of a button automatically produces an Outlook mail as follows:
public static void CreateOutlookEmail(string pFileName, string pCaseFolder, string pEmail, string pSubject, string pMessage)
{
try
{
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = pSubject;
mailItem.To = pEmail;
mailItem.Body = pMessage;
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
mailItem.Display(false);
string fileDetails = pCaseFolder + "\\" + pFileName + #".eml";
mailItem.SaveAs(fileDetails);
}
catch (Exception eX)
{
throw new Exception("cDocument: Error occurred trying to Create an Outlook Email"
+ Environment.NewLine + eX.Message);
}
}
The code succesfully opens a new Outlook email, and populates it with the details sent into the method e.g. email address, subject and the body of the message.
Also when I locate the folder (sent in as a paramater) I can see the email document has been saved.
The issue is, that when I open the email from the folder, the email document is totaly blank ii.e. no email address, subject or message.
What am I doing wrong?
Your code is fine. Just use extension ".msg" instead of ".eml". Also the eml format does not exist under Outlook.OlSaveAsType