How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address

47,368

Solution 1

You are using outlook to send the mail. Since outlook must be configured to use the from address of your mail, you cannot provide the from address directly. However, you can select an account available on outlook. For example :

using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Accounts accounts = olkApp1.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
    // When the e-mail address matches, send the mail.
    if (account.SmtpAddress == "[email protected]")
    {
            olkMail1.SendUsingAccount = account;
            ((Outlook._MailItem)olkMail1).Send();
            break;
    }
}

Solution 2

The Send method sends the mail using the default account. To specify a different account to send the mail, set the SendUsingAccount property to the desired Account prior to calling the Send method.

var application = new Application();
var mail = (_MailItem) application.CreateItem(OlItemType.olMailItem);
mail.To = "[email protected]";
....
Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
mailItem.SendUsingAccount = account;
mail.Send();

More information can be found here:

http://msdn.microsoft.com/en-us/library/ff184652.aspx

Share:
47,368
Harikasai
Author by

Harikasai

Updated on May 10, 2020

Comments

  • Harikasai
    Harikasai almost 4 years

    I'm using Interop for sending e-mails via Outlook, but I am not able to specify the From e-mail address.

    I want to send mails to multiple users originating from the same sender (from). I need to mention the from e-mail address. However I cannot find a property using Intellisense that allows me to specify it.

    Please help.

    Microsoft.Office.Interop.Outlook.Application olkApp1 = 
        new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook.MailItem olkMail1 =
        (MailItem)olkApp1.CreateItem(OlItemType.olMailItem);
            olkMail1.To = txtpsnum.Text;
            olkMail1.CC = "";
            olkMail1.Subject = "Assignment note";
            olkMail1.Body = "Assignment note";
            olkMail1.Attachments.Add(AssignNoteFilePath, 
                Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, 
                    "Assignment_note");
    olkMail1.Save();
    //olkMail.Send();
    
  • provençal le breton
    provençal le breton almost 11 years
    How can I create my mail, and open it? (like a mailto?). Save put the mail in non send mail, but can we have a window with the new create mail?
  • Roman Boiko
    Roman Boiko over 10 years
    @Zaphod: use mail.Display(false) to display email as a non-modal window.
  • provençal le breton
    provençal le breton over 10 years
    @RomanD.Boiko :thanks for your comment, that's what i was searching for.
  • Eric
    Eric about 8 years
    Thank you for including the using portion of this. Took me forever to find that. I'm not sure how developers normally know what 'using' stuff they need to include?
  • j2associates
    j2associates about 6 years
    @Eric You can code the using {} block for any class that implements IDisposable. When the using block ends, the variable defined in it is automatically disposed.
  • Brendan Cain
    Brendan Cain about 6 years
    @j2associates I think they meant the namespace/alias "using"