I use Outlook2013, which has several mailboxes from both exchange and pop servers. ( Rob@mydomain.com [default exchange], rob@somethingdifferent.com [POP], support@mydomain.com [exchange])
I am trying to use Outlook Automation to send email using the support@mydomain.com account .
The problem I ran into is the code below, which creates the mail item in the outbox for support, but the from field is rob@mydomain.com , not support@mydomain.com. This prevents it from being sent.
I would like to change the address from support@mydomain.com. I thought that setting the Sendusionaccount property did this.
Any help is greatly appreciated.
public static string Send_Email_Outlook(string _recipient, string _message, string _subject, string _cc, string _bcc, string accountname)
{
try
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon(Missing.Value, Missing.Value, true, true);
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.Subject = _subject;
oMsg.HTMLBody = _message;
oMsg.To = _recipient;
oMsg.CC = _cc;
oMsg.BCC = _bcc;
#region Send via another account
if (accountname.Trim().Length != 0)
{
Microsoft.Office.Interop.Outlook.Accounts accounts = oMsg.Session.Accounts;
for (int i = 1; i <= accounts.Count; i++)
{
string accountfound = accounts[i].DisplayName.ToLower();
if (accountname.ToLower() == accountfound)
{
oMsg.SendUsingAccount = accounts[i];
Microsoft.Office.Interop.Outlook.Recipient recipient = oMsg.Session.CreateRecipient(accountfound);
oMsg.Sender = recipient.AddressEntry;
break;
}
}
}
#endregion
(oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send();
oNS.Logoff();
oMsg = null;
oNS = null;
oApp = null;
}
catch (Exception e)
{
return e.Message;
}
return "";
}
source
share