2

I'm working in console Application and I'm using the below c# code to send mail automatically on a button click event,

    public void SendMail()
    {
        //// Create the Outlook application by using inline initialization.
        Outlook.Application oApp = new Outlook.Application();

        ////Create the new message by using the simplest approach.
        Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

        //Add a recipient.
        // TODO: Change the following recipient where appropriate.
        Outlook.Recipients oRecips = oMsg.Recipients;
        List<string> oTORecip = new List<string>();
        List<string> oCCRecip = new List<string>();

        oTORecip.Add("example@test.com");
        oCCRecip.Add("example@test.com");
        foreach (string t in oTORecip)
        {
            Outlook.Recipient oTORecipt = oRecips.Add(t);
            oTORecipt.Type = (int)Outlook.OlMailRecipientType.olTo;
            oTORecipt.Resolve();
        }

        foreach (string t in oCCRecip)
        {
            Outlook.Recipient oCCRecipt = oRecips.Add(t);
            oCCRecipt.Type = (int)Outlook.OlMailRecipientType.olCC;
            oCCRecipt.Resolve();
        }

        //Set the basic properties.
        oMsg.Subject = "TestMail- " + DateTime.Today.ToString("MM/dd/yyyy");
        oMsg.HTMLBody = "<html>" +
            "<head>" +
            "<title>TestMail</title>" +
            "</head>" +
            "<body style='background-color:#E6E6E6;'>" +
            "<div style='font-family: Georgia, Arial; font-size:14px; '>Hi,<br /><br />" +
            "PFA<br />" +
            "This is Test Mail.Please Ignore.<br /><br /><br /><br /><br />" +
            "Thanks & Regards<br />" +
            "test"+
            "</div>" +
            "</body>" +
            "</html>";
        string date = DateTime.Today.ToString("MM-dd-yyyy");

        //Add an attachment.
        // TODO: change file path where appropriate
        String sSource = "D:\\Test\\test_" + date + ".xlsx";
        String sDisplayName = "MyFirstAttachment";
        int iPosition = (int)oMsg.Body.Length + 1;
        int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
        Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);

        // If you want to, display the message.
        // oMsg.Display(true);  //modal

        //Send the message.
        oMsg.Save();
        oMsg.Send();

        //Explicitly release objects.
        oTORecip = null;
        oCCRecip = null;
        oAttach = null;
        oMsg = null;
        oApp = null;
    }

this code is working fine.I want to send the mail using another account.HOw to give the From address in the Above code? Is it possible to do so?what is the c# code i need to use to send the mail on behalf of someother account using c# code?

user2514925
  • 931
  • 8
  • 33
  • 56

1 Answers1

4

Set MailItem.SendUsingAccount property on oMsg

http://msdn.microsoft.com/en-us/library/office/ff869311%28v=office.14%29.aspx

you can select only from Application.Session.Accounts collection.

or possibly check

MailItem.SentOnBehalfOfName http://msdn.microsoft.com/en-us/library/office/ff862145%28v=office.14%29.aspx

(for exchange)

There are some similar posts:

Outlook automation - Change Sender Account

Sending defer message delivery and change default account using Powershell

EDIT

Following code is ok with intellisense, check your DLL version. Checked with v 14 and v 12 of Outlook PIA.

Package downloaded from http://www.microsoft.com/en-us/download/details.aspx?id=43664

using Microsoft.Office.Interop.Outlook;
//C:\Program Files\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Outlook.dll
//14.0.0.0 or 12.0.0.0 - it works from Outlook 2007 and higher

namespace ConsoleApplication1
{
    class Program
    {
        public void SendMail()
        {
            //// Create the Outlook application by using inline initialization.
            Application oApp = new Application();
            ////Create the new message by using the simplest approach.
            MailItem oMsg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
            oMsg.SendUsingAccount = oApp.Session.Accounts[2]; // it starts at 1

...

It seems you are using some other assembly DLL because for me the statement "using Microsoft.Office.Interop;" does not work at all.

Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
  • Thanks a lot for the response.can you give a code example.in C# intellisense not listing out MailItem.SendUsingAccount property – user2514925 Jul 18 '14 at 05:42
  • as VDohnal said, you have better use v14 or 12 of Outlook PIA; older Outlook and their interop COM lib might to support such function, or have this function visible to .NET. – ZZZ Jul 18 '14 at 06:32
  • I have a send as access for a common mailbox.it shows the count as 2 but when i give Accounts[1] and Accounts[2]. both sends mail from my account only – user2514925 Jul 18 '14 at 06:43
  • I have tested the code now on my machine and Outlook 2007 and it is working for me, did you setup accounts correctly within Outlook? Did you debug and look at what App.Session.Accounts collection actually contain? – Vojtěch Dohnal Jul 18 '14 at 07:01
  • There was a mistake: the first index is 1 not 0 within Accounts collection. – Vojtěch Dohnal Jul 18 '14 at 07:02
  • how to check the Accounts which i have acces in my outlook – user2514925 Jul 18 '14 at 07:21
  • Is it enough if i have send as access for an account to send mail from that account? – user2514925 Jul 18 '14 at 07:31
  • You have to configure e-mail accounts like this http://office.microsoft.com/en-us/outlook-help/add-or-remove-an-email-account-HA010354414.aspx If you use exchange it would be different, you should specify that. – Vojtěch Dohnal Jul 18 '14 at 07:57
  • 1
    MailItem.SentOnBehalfOfName saved my life! Cheers Thanks! – Eqzt111 Nov 24 '20 at 22:02