3

I found this code snippet from "http://www.codeproject.com/Tips/165548/C-Code-snippet-to-send-an-Email-with-attachment-fr"

// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();

// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

// Set HTMLBody. 
oMsg.HTMLBody = "Test"

//Subject line
oMsg.Subject = "Test Sub";

// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip;

oRecip = (Outlook.Recipient)oRecips.Add(UserID);
oRecip.Resolve();

oRecip = (Outlook.Recipient)oRecips.Add(Recipients[i]);
oRecip.Resolve();

// Send.
oMsg.Send();

}

I need to know how to send the mail from a specific Outlook Profile if I have a couple of profiles configured in Outlook.

Thanks in Advance, Avirup.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Avirup Das
  • 189
  • 1
  • 3
  • 15

2 Answers2

2

I actually found the answer from another post. We need to have a profile name for the different accounts and the following code will do the work:

Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];

Community
  • 1
  • 1
Avirup Das
  • 189
  • 1
  • 3
  • 15
  • And you can enumerate the accounts with something like this: https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb207787(v=office.12) – TripleAntigen Aug 31 '18 at 03:33
0

I tried for a while with this problem and in the end I'm just using the email address to send from in a Template .msg file. Obviously the template used can be dynamic (for us, based on language) -

Outlook.MailItem oMsg = (Outlook.MailItem)outlookApp.CreateItemFromTemplate("C:\\mail_templates\\template_"+lang+".msg");

I realise this doesn't answer the question as asked, but this is the workaround that we settled with - it may or may not be appropriate for your case but I hope it helps.

aland
  • 1,824
  • 2
  • 26
  • 43
  • I was just wondering, if the use of template can specify the outlook profile to be used for sending mail. – Avirup Das Apr 16 '15 at 11:51
  • @AvirupDas you can specify an email address to send from in a template, so effectively yes. – aland Apr 16 '15 at 20:09
  • Ahh yes... ofcourse. The *.msg file can hold the recipient address as well. But I am actually using an *.html template for the Mail body. I guess, I need to have the *.msg file composed by that HTML template to suite both the desired requirements. – Avirup Das Apr 20 '15 at 16:55