Changing outlook mail sender in C# using Microsoft.Office.Interop.Outlook.Mailitem - c#

I programmed a wpf application that sends an outlook mail through the default logged in user.
This is my code:
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace FileOrganizer
{
class Program
{
private void CreateMailItem()
{
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
mailItem.To = "someone#example.com";
mailItem.Body = "This is the message.";
mailItem.Attachment.Add(logPath);//logPath is a string holding path to the log.txt file
mailItem.Importance = Microsoft.Office.Tools.Outlook.OlImportance.olImportanceHigh;
mailItem.Display(false);
}
}
}
I saw this post:
How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address
But i can't find the Outlook.Accounts option in my project. It just doesn't appear.
Does anyone have a solution for this problem?
My main goal of course is to send my mail using different user.

Related

Marshal.GetActiveObject("Outlook.Application") fails while Outlook has focus

I am trying to automate Outlook using COM, and we are having terrible problems with Outlook crashing at arbitrary points when trying to use MAPI methods (eg Outlook.Recipient r = MAPI.CreateRecipient("me#there.com")).
One solution that has been suggested is to use Marshal.GetActiveObject() instead of new Outlook.Application().
This appears to work fine, however I have run into a very strange issue - the code fires up a copy of outlook and tries to get the application object, but calls to Marshal.GetActiveObject throw a System.Runtime.InteropServices.COMException exception while Outlook is the active application (Has the focus).
If I run the below code, the try keeps failing over and over.
However, if I hit ALT-TAB or click anywhere such that Outlook is no longer the active application, the code INSTANTLY succeeds.
Any ideas why? Failing that, does anyone have code that will de-focus outlook, so the code succeeds?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Threading;
using System.Reflection;
using System.Runtime.InteropServices;
namespace MAPI_Repro
{
class FakeTest
{
public Outlook.Application oApp;
public Outlook.NameSpace MAPI;
public void DoTest(){
if (Process.GetProcessesByName("OUTLOOK").Count() == 0)
{
var process = Process.Start(new ProcessStartInfo("outlook.exe"));
}
while (Process.GetProcessesByName("OUTLOOK").Count() == 0)
{
Thread.Sleep(100);
}
bool success = false;
while (!success)
{
Debug.WriteLine("Waiting for Marshal.GetActiveObject");
try
{
// This FAILS while Outlook is the active application.
// As soon as you hit ALT-TAB or click another app, this succeeds.
oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
success = true;
Debug.WriteLine("SUCCESS");
}
catch (System.Runtime.InteropServices.COMException exp)
{
Debug.WriteLine("FAILED " + exp);
}
Thread.Sleep(100);
}
MAPI = oApp.GetNamespace("MAPI");
MAPI.Logon("", "", Missing.Value, Missing.Value);
}
}
}

Connecting Outlook 2010 Public Folders

I'm trying to connect to a Public Folder in Outlook 2010 with C# (Visual Studio 2010).
I copied following code from a Microsoft Website:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNS = olApp.GetNamespace("MAPI"); Outlook._Folders oFolders;
oFolders = olNS.Folders;
Outlook.MAPIFolder oPublicFolder = oFolders["Public Folders"];
oFolders = oPublicFolder.Folders;
Outlook.MAPIFolder oAllPFolder = oFolders["All Public Folders"];
oFolders = oAllPFolder.Folders;
Outlook.MAPIFolder oMyFolder = oFolders["My Public Folder"];
Console.Write(oMyFolder.Name);
}
}
}
My problem is that "ApplicationClass" is redlined and I don't know what I've forgotten or done wrong.
Here's a screenshot with the error message.
You need to use interface
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application()
or disable embedding of Interop types for this assembly (References -> Microsoft.Office.Interop.Outlook (right click) -> Properties -> Set 'Embed Interop Types' to False)
Change the line
Outlook._Application olApp = new Outlook.ApplicationClass();
to
Outlook._Application olApp = new Outlook._Application();

C# MailMessage Display email instead of Send

I have been following the example below to send email using C# code with success:
MSDN Mail Message
However, I would like the code to display the composed email message on user machine, so that user can have a final check before hitting send button on outlook.
In the VBA world, I can use mail.Display in place of mail.Send.
Can anyone provide some advice to achieve that in C#?
Thanks.
How about this...
private void btnEmail_Click(object sender, EventArgs e)
{
string command = "mailto:somebody#domain.com?subject=The Subject&bcc=another#codegaim.com&body=Hi,I found this website and thought you might like it http://www.geocities.com/wowhtml/";
Process.Start(command);
}
Found a great solution to my problem i.e. to use Microsoft Office Interop Outlook instead of System.Net.MailMessage
followed this:
How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address
//using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
using Outlook = Microsoft.Office.Interop.Outlook;
public static class Program
{
static void Main(string[] args)
{
SendUsingAccountExample();
}
private static void SendUsingAccountExample()
{
var application = new Application();
var mail = (_MailItem)application.CreateItem(OlItemType.olMailItem);
mail.Body = "Hello";
mail.Subject = "Good Bye";
mail.To = "hello#google.com";
// Next 2 lines are optional. if not specified, the default account will be used
Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
mail.SendUsingAccount = account;
mail.Display(false); // To Display
//mail.Send(); // To Send
}
}
}

Adding text to an email using an Outlook addin C# 2010

I want to add some text to the body of my email. But i keep getting this error:
Error 1 'EmailHelper.EmailHelperRibbon' does not contain a definition for 'Application' and no extension method 'Application' accepting a
first argument of type 'EmailHelper.EmailHelperRibbon' could be found
(are you missing a using directive or an assembly reference?)
My code looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
namespace EmailHelper
{
public partial class EmailHelperRibbon
{
private void EmailHelperRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Your Ribbon Works!");
Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)
this.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
{
mailItem.Subject = "This text was added by using code";
mailItem.Body = "This text was added by using code";
}
}
}
}
Try this one:
var inspector = this.Context as Outlook.Inspector;
var currentMailItem = inspector.CurrentItem as Outlook.MailItem;
currentMailItem.Body = "your text";
I am guessing this is a Ribbon based on the name, so you need to access the application off of the Globals class:
Globals.ThisAddIn.Application.CreateItem(...)

How to add login screen in my email application & login to my email account (windows phone 7)

I created a simple email application for Windows Phone 7, but I need to set-up my email account to send email from my application.
I want to add a login screen in my application so I can set-up my email account and when I put my email user id or password my application should take me to the compose mail screen.
I also want it to remember my user id or password until I logout.
This is my application code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace SendingMail
{
public partial class MainPage : PhoneApplicationPage
{
EmailAddressChooserTask emailAddresstask;// Constructor
public MainPage()
{
InitializeComponent();
this.emailAddresstask = new EmailAddressChooserTask();
this.emailAddresstask.Completed += new EventHandler<EmailResult>(emailAddresstask_Completed);
}
#region Events
//Open Contact button click
private void btnOpenContact_Click(object sender, RoutedEventArgs e)
{
emailAddresstask.Show();
}
//Email Address Chooser Task Completed
private void emailAddresstask_Completed(object sender, EmailResult e)
{
if (e.TaskResult == TaskResult.OK)
{
txtTo.Text = e.Email;
}
}
//Send mail button click
private void btnMail_Click(object sender, RoutedEventArgs e)
{
EmailComposeTask Myemail_Composetask = new EmailComposeTask();
Myemail_Composetask.To = txtTo.Text;
Myemail_Composetask.Cc = txtCC.Text;
Myemail_Composetask.Subject = txtSbj.Text;
Myemail_Composetask.Body = txtbd.Text;
Myemail_Composetask.Show();
}
#endregion
}
}
I guess you have mis-understood the how does Email Launcher task works. It does not have any login, or log out options. This Email Launchers task will be used to compose a email on configured email on outlook or other POP, IMAP email application.
Also you will be not able to test on emulator for this. You can only test on the device which has configured at least one email in it.
Thanks,
Kamal.

Categories