0

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.

Community
  • 1
  • 1
user3731023
  • 65
  • 1
  • 7
  • What version of Outlook are you using? Do you see the MailItem.SendUsingAccount property? – Dmitry Streblechenko Jul 31 '14 at 13:29
  • @DmitryStreblechenko 2010, I cant see the property. – user3731023 Jul 31 '14 at 14:05
  • Then I am not sure what the problem is - can you not access Namespace.Accounts collection? Are you saying you get a compiler error if you declare an instance of the Microsoft.Office.Interop.Outlook.Accounts object? – Dmitry Streblechenko Jul 31 '14 at 16:45
  • @DmitryStreblechenko Exactly. Maybe i need to reference a newer Office Object? I used 12 and 14 and it didn't help. – user3731023 Jul 31 '14 at 18:40
  • Accounts support was added in Outlook 2007 (12). Are you referencing the right assembly? If you see SendUsingAccount, Accounts collection will also be there. Do you see that collection in OutlookSpy if you click the Namespace button? – Dmitry Streblechenko Jul 31 '14 at 20:05
  • @DmitryStreblechenko I can't really use OutlookSpy in my organization.I can't see SendUsingAccount, which is the main thing i need and the reason why i wrote this post. Can you find me a different solution? – user3731023 Aug 01 '14 at 09:53
  • That means you are using an old version of the interop. Remove the Outlook reference and add it again. – Dmitry Streblechenko Aug 01 '14 at 18:20
  • @DmitryStreblechenko It worked! Now i am getting a different error after writing this: `Outlook.Account account = Application.Session.Accounts["MyOtherAccount"]; mailItem.SendUsingAccount = account;` The Error: Sorry,something went wrong, please try again. Do you know what's the problem? – user3731023 Aug 04 '14 at 11:00
  • Does that account actually exist? What do you see for Account.DisplayName if you loop through all accounts in the Namesapce.Accounts? You can see the account names in OutlookSpy - click Namespace button, select the Accounts property, click Browse, go to the IEnumVariant tab. – Dmitry Streblechenko Aug 04 '14 at 14:00
  • @DmitryStreblechenko When i do this, i'm getting only "Microsoft Exchange Server". What does it mean? *btw, i cant use OutlookSpy in my organization. I will be very glad if you'll find me a solution without using different programs(which i can't use). thx! – user3731023 Aug 05 '14 at 12:25
  • @DmitryStreblechenko Oh and the account defined as public. – user3731023 Aug 05 '14 at 13:01
  • I am not sure what you mean by "account defined as public". Defined by whom? Did you add it as a separate account? If you are sending as another Exchange user, set the MailItem.SentOnBehalfOfName property instead. – Dmitry Streblechenko Aug 05 '14 at 13:23

0 Answers0