Adding the sender address in Redemption - c#

I am using Redemption and the RDO Objects. I want to be able to set the sender address so that outlook will show who the email is coming from. So I connect to Outlook and the inbox and create my msg object
app = new Application();
session = app.CreateObject("Redemption.RDOSession");
try
{
session.Logon(Program.outlookProfileName);
RDOFolder inbox = session.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);
msg = inbox.Items.Add();
}
I display an email form with the from address(prepopulated based on user), the TO, subject, and body
The user fills in the to box with the intended recipient(s), the subject and the body. They click the Send button. I do the following code
msg.Recipients.ResolveAll();
msg.SentOnBehalfOfEmailAddress = SenderTB.Text;
msg.Subject = SubjectTB.Text;
msg.Body = BodyTB.Text;
msg.Send();
But the sender address does not show properly in outlook. I want the sender address to show whatever was in the SenderTB.Text. How do I set the Sender for the msg object?

Is this for a delegate Exchange mailbox? Either set the SentOnBehalfOfName (not SentOnBehalfOfEmailAddress) or SentOnBehalfOf (RDOAddressEntry).

Related

Moving Mail to Outlook Sent Folder. Mail is still editable

I am editing an email and send it to the recipient. But i also want to save the original mail in the sent folder. But if i move the mailobject to the folder the mail is still editable.
This is how i move the mail:
private void CopyMailToSent(Outlook.MailItem originalMail)
{
var folder = originalMail.SaveSentMessageFolder;
originalMail.Move(folder);
}
Can i set the mailobject to readonly or faking the send?
Firstly, Outlook Object Model would not let you set the MailItem.Sent property at all. On the MAPI level, the MSGFLAG_UNSENT bit in the PR_MESSAGE_FLAGS property can only be set before the message is saved for the very first time.
The only OOM workaround I am aware of is to create a post item (it is created in the sent state), set its message class to "IPM.Note", save it, release it, reopen by the entry id (it will be now MailItem in the sent state), reset the icon using PropertyAccessor, set some sender properties (OOM won't let you set all of them).
If using Redemption (I am its author) is an option, it will let you set the Sent property as well as the sender related properties, plus add recipients without having to resolve them.
Set MySession = CreateObject("Redemption.RDOSession")
MySession.MAPIOBJECT = Application.Session.MAPIOBJECT
Set folder = MySession.GetDefaultFolder(olFolderSentMail)
Set msg = folder.Items.Add("IPM.Note")
msg.Sent = True
msg.Recipients.AddEx "Joe The User", "joe#domain.demo", "SMTP", olTo
msg.Sender = MySession.CurrentUser
msg.SentOnBehalfOf = MySession.CurrentUser
msg.subject = "Test sent message"
msg.Body = "test body"
msg.UnRead = false
msg.SentOn = Now
msg.ReceivedTime = Now
msg.Save
I couldn't solve the problem but i did workaround which works fine for me.
I hope it's ok to post this here even it's not right the solution. If not, sorry i will delete it.
My workaround is saving the orignal mail as ".msg" file and then add it to the mail in the sent folder.
Then it looks like this:
This is the code:
private void SendMail(Outlook.Mailitem mail)
{
mail.SaveAs(tempDirectory + #"originalMail.msg");
var folder = mail.SaveSentMessageFolder;
ChangeMailSubject(mail);
ChangeMailText(mail);
mail.Send();
folder.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler((sender) => AttachOriginalMail(sender);
}
private void AttachOriginalMail(object sender)
{
var mail = (Outlook.MailItem) sender;
mail.Attachments.Add(tempDirectory + #"originalMail.msg");
mail.Save();
}

how to automatically trigger a link in outlook email using VSTO

I'm trying to programmatically click on a link which creates an email with predefined subject,to,cc,bcc and body content of the email.My requirement is, If I select an Outlook mail item and click on “Approve via mail” in my Addin, the code will search for hyperlink “Click here to Approve” in the mail body and Automatically click on the hyperlink.
The hyperlink “Click here to Approve” creates an email with predefined subject,to,cc,bcc and body content of the email.
I'm not sure how to do it with VSTO as all the other solutions suggest using JQuery and Javascript
Object selObject = this.Application.ActiveExplorer().Selection[1];
Outlook._MailItem eMail = (Outlook._MailItem)
this.Application.CreateItem(Outlook.OlItemType.olMailItem);
eMail = ((Outlook._MailItem)selObject);
if(eMail.HTMLBody.Contains("Approve"))
{
}
I'm not sure what I can write in the IF segment of the code.Please Suggest.
Outlook doesn't provide anything for opening hyperlinks. You can use the the following code (Process.Start) for opening them in the default web browser:
Process.Start("your_hyperlink");
Or just create a Mail item progrmmatically in Outlook based on the info where the Approve button was clicked.
Outlook.MailItem mail = null;
Outlook.Recipients mailRecipients = null;
Outlook.Recipient mailRecipient = null;
try
{
mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem)
as Outlook.MailItem;
mail.Subject = "A programatically generated e-mail";
mailRecipients = mail.Recipients;
mailRecipient = mailRecipients.Add("Eugene Astafiev");
mailRecipient.Resolve();
if (mailRecipient.Resolved)
{
mail.Send();
}
else
{
System.Windows.Forms.MessageBox.Show(
"There is no such record in your address book.");
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message,
"An exception is occured in the code of add-in.");
}
finally
{
if (mailRecipient != null) Marshal.ReleaseComObject(mailRecipient);
if (mailRecipients != null) Marshal.ReleaseComObject(mailRecipients);
if (mail != null) Marshal.ReleaseComObject(mail);
}
Take a look at the following articles for more information and samples:
How to create and show a new Outlook mail item programmatically: C#, VB.NET
How To: Create and send an Outlook message programmatically
How To: Fill TO,CC and BCC fields in Outlook programmatically
How To: Create a new Outlook message based on a template

Checking for an enabled outlook account

I just started to dabble into using Microsoft.Office.Interop.Outlook. I was able to successfully send an email using the bit of code below.
public void Send()
{
try
{
Outlook._Application _app = new Outlook.ApplicationClass();
var test = _app.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.MailItem mail = (Outlook.MailItem) _app.CreateItem(Outlook.OlItemType.olMailItem);
mail.To = "testemail#fakeaddress.com";
mail.Subject = "Test Outlook Subject";
mail.Body = "Test Outlook Body";
mail.Importance = Outlook.OlImportance.olImportanceNormal;
((Outlook.MailItem) mail).Send();
}
catch
{
Notification.Notice("Error");
}
}
I would like to have a Validate() function before the try/catch such that it'll check to see if there's a valid outlook account enabled. May I ask does anyone know how I can check if any outlook accounts are setup?
I tried this
public bool validate()
{
Outlook._Application _app = new Outlook.ApplicationClass();
Outlook.Accounts accounts = _app.Session.Accounts;
return accounts.Count > 0;
}
But accounts.Count returned 1 even after I removed my outlook account.
There will always be at least one account - the store. Otherwise Outlook won't run. But even if there are mail accounts, how would you know whether they are configured appropriately? Unless you take over the message submission, there is no way for you to know ahead of time.
UPDATE: Loop through the Namespace.Accounts collection and look for accounts with Account.AccountType == olExchange ,olImap,olPop3, olHttp. Keep in mind that OOM only list mail accounts, not store or address book.
If you were using Extended MAPI (C++ or Delphi), you could use IOlkAccountManager::EnumerateAccounts(CLSID_OlkMail, ...) (you can play with that interface in OutlookSpy (I am its author) - click IOlkAccountManager button). If Extended MAPI is not an option, Redemption (I am also its author) exposes the RDOAccounts object; its GetOrder(acMail) method will return all mail accounts. You'll just need to check if the returned collection has any elements.

VSTO Outlook Addin Email Sender Name

I have a VSTO Outlook 2013 addin that reads properties from CurrentItem when the read mail window is open. When I get the property Sender I always get system.__comobject Why does it keep returning this?
The Sender property returns an AddressEntry object that corresponds to the user of the account from which the MailItem is sent.
//if it is a regular mail or a Meeting mail
var senderName = mail.Sendername;
//if it is a task mail
var senderName = mail.Owner;
Wish can help you!
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oApp1 = oApp.GetNamespace("MAPI");
oApp1.CurrentUser.Name;

outlook contact can't get SMTP address, No MAPI properties on "exchange" contact list

is it a bug in outlook?
i've created a local Contact list card, and i gave him in the address field an exchange user address. (double click on that address, see that its exchange).
when i try to get the address using MAPI - i can't, the problem is this, when i check the AddressEntry object, i get the following:
Type = "EX"
Address = "/o=.../ou=Exchange..."/cn=Recipients/cn=Name
Class = olAddressEntry
AddressEntryUserType = olOutlookContactAddressEntry
when i checked in OutlookSpy - no MAPI properties, so i can't get PR_SMTP_ADDRESS nor PR_EMS_AB_PROXY_ADDRESSES, also, this is not SMTP so i have no valid address.
i checked other users and those are the properties (which it works):
Real exchange user recipient, same email address as the exchange one, but it was created without autocorrect to the exchange user, so it stays smtp:
Type = "SMTP"
Address = "Email#email.com"
Class = olAddressEntry
AddressEntryUserType = olExchangeUserAddressEntry
Regular address entry
Type = "EX"
Address = "/o=.../ou=Exchange..."/cn=Recipients/cn=Name
Class = olAddressEntry
AddressEntryUserType = olOutlookContactAddressEntry
if i double click on the "exchange" local contact, it opens exchange window of its properties, if i open the "regular one i created manually", it opens the "SMTP" address window.
any workaround i can do?
thanks.
It didn't work in way "Dmitry Streblechenko" suggested because for some reason
ContactItem.Email1EntryId, ContactItem.Email2EntryId and ContactItem.Email3EntryId contains not id but some wrong random data (even some html tags) - office 2016.
But it finally worked with following code
using (var pa = new InteropWrapper<Outlook.PropertyAccessor>(contact.innerObject.PropertyAccessor))
{
String EMAIL1_ENTRYID = "http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80850102";
string emailEntryID = pa.innerObject.BinaryToString(pa.innerObject.GetProperty(EMAIL1_ENTRYID));
using (var rs = new InteropWrapper<Outlook.NameSpace>(Globals.ThisAddIn.Application.Session))
{
rs.innerObject.Logon();
using (var addressEntry = new InteropWrapper<Outlook.AddressEntry>(rs.innerObject.GetAddressEntryFromID(emailEntryID)))
using (var exchangeUser = new InteropWrapper<Outlook.ExchangeUser>(addressEntry.innerObject.GetExchangeUser()))
{
return exchangeUser.innerObject.PrimarySmtpAddress;
}
}
}
where InteropWrapper<T> just IDisposable wrapper around com object - it does Marshal.ReleaseComObject(innerObject) on dispose. So you can do everything without it by using Marshal.ReleaseComObject() directly.
just in case if someone need email 2 and email 3 including them here
String EMAIL2_ENTRYID = "http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80950102";
String EMAIL3_ENTRYID = "http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80A50102";
Hope it will save someones time! I've spent like a day on it.
If you have an EX type contact, use the value of the ContactItem.Email1EntryId property to call Namespace,GetAddressEntryFromId, then read the AddressEntry.GetExchangeUser.PrimarySmtpAddress property.

Categories