Changing SenderName in a .msg file - outlook

How can I change the property SenderName and SenderEmailAddress in a .msg file, not from within Outlook?
I know that this is not possible with the Outlook COM, because it is a read only property.
I tried using the class Redemption.RDOSession and the method GetMessageFromMsgFile but all the properties were null.

Sure, it is very easy:
string rdoAddressID = rdoSession.CreateOneOffEntryID("Joe the User", "SMTP", "address#email.com", Type.Missing, Type.Missing);
Redemption.RDOAddressEntry entry = rdoSession.GetAddressEntryFromID(rdoAddressID, Type.Missing);
mail.Sender = entry;

Related

Not able to save a mailItem with protected attachment in outlook using c#

I'm trying to create a copy of a mailItem in my sent folder. Once I create it, I save the msg in the folder. It works for all mailItems , except when I try to save a mailItem with an attachment where I disallow the save attachment permission in outlook. Why does the mailItem.Save() not saving the mailItem only for this scenario?
In the code below, I'm using redemptions to create a copy in sent folder. msg.save() saves all mails but the one I mentioned above. Also I tried saving the mailItem before the creation, but it does not generate entryId.
static void CreateSentFolderMail(Redemption.SafeMailItem newSentMail, string nvdID, Outlook.MailItem mailItem, Redemption.SafeMailItem safeMailItem)
{
RDOFolder folder = Globals.ThisAddIn.session.GetDefaultFolder(rdoDefaultFolders.olFolderSentMail);
RDOMail msg = (RDOMail)folder.Items.Add(newSentMail);
RDOMail originalmsg = Globals.ThisAddIn.session.GetMessageFromID(mailItem.EntryID);
msg.Sent = true;
msg.SentOn = DateTime.Now;
msg.ReceivedTime =msg.CreationTime;
msg.Subject = safeMailItem.Item.Subject;
msg.To = safeMailItem.Item.To;
msg.BCC = safeMailItem.Item.BCC;
msg.Body = safeMailItem.Item.Body;
msg.Recipients = originalmsg.Recipients;
msg.Sender = Globals.ThisAddIn.session.CurrentUser;
msg.SentOnBehalfOf = Globals.ThisAddIn.session.CurrentUser;
msg.SetProps(NVDMailHeaderUtils.PS_INTERNET_HEADERS + NVDMailHeaderUtils.NVD_HEADER_ID, nvdID);
msg.Save();
}
I had used session.GetRDOObjectFromOutlookObject before calling this method to get a RDOAttachment object. But after using this: session.GetRDOObjectFromOutlookObject I was not able to the save the mailitem. Save was not getting executed and hence the EntryId was not getting generated. Due to this issue I was getting an error here : RDOMail originalmsg = Globals.ThisAddIn.session.GetMessageFromID(mailItem.EntryID); saying "invalid entry Id". I installed the new version of redemptions that solved this problem.

EWS Managed API 2.2 Read\Write extended properties of attachments

Currently I'm working on migration of one big project from MAPI CDO to EWS (Managed API 2.2) to support Ex2016. All things were migrated well except one: I can't find the way how to read\write Attachments Extended Properties. Does anybody know how to do that or may be some workaround? This is very critical for me and I would be very appreciate for any help.
---Update:
Also tried to use native EWS to get property of attachment but without success too:
var ret = esb.GetAttachment(new GetAttachmentType()
{
AttachmentIds = new []{new AttachmentIdType()
{
Id = "AAMkADVhNjUzMzMyLTRiMDYtNDc4OS1hYjJjLWI1ZDA4ZWFhYTJkZQBGAAAAAADqFaOFYZSeQI5UObwGbjIJBwAOgaos6ORVS5+o5bQovn/kAAAAeN2cAAAOgaos6ORVS5+o5bQovn/kAAAeCoIuAAABEgAQAJPAuRg2gipPmEKfgW26mFU=",
}},
AttachmentShape = new AttachmentResponseShapeType()
{
BodyType = BodyTypeResponseType.Best,
BodyTypeSpecified = true,
IncludeMimeContent = false,
IncludeMimeContentSpecified = true,
AdditionalProperties = new []
{
new PathToExtendedFieldType() { PropertyType = MapiPropertyTypeType.Integer, PropertyTag = "0x3705"},
new PathToExtendedFieldType() { PropertyType = MapiPropertyTypeType.Integer, PropertyTag = "0x0E21"},
}
}
});
The response doesn't contain any of requested properties.
--- Update 2:
In project we use next properties of attachments:
PR_RECORD_KEY, PR_DISPLAY_NAME, PR_RENDERING_POSITION
PR_ATTACH_ENCODING, PR_ATTACH_NUM, PR_ATTACH_METHOD, PR_ATTACH_LONG_FILENAME, PR_ATTACHMENT_HIDDEN, PR_ATTACH_CONTENT_ID, PR_ATTACH_FLAGS, PR_ATTACH_MIME_TAG, PR_ATTACH_CONTENT_LOCATION, PR_ATTACH_SIZE
Also we create a couple of Custom Extended Properties with custom Property Set and tag some attachments with that props.
Some of Properties can be found in object model of EWS/ManagedApi like PR_ATTACH_SIZE but the problem with others and with custom props.
So we need to read/write standard attachment properties as well as custom.
In project we mark attachment itself, not embedded Item.
You can't access extended properties on Attachments or the Recipients collection in EWS outside of those properties that are made accessible as strongly typed properties by the API. The only place you can use Extended properties are at the message level.
That said can you explain more as to how your using Extended properties eg are these extended properties on embeded items. If that is the case then you could access those extended properties via the Item Attachment.
Looking at your code 0x3705 is the PR_ATTACH_METHOD property on an attachment there is no equivalent to this in EWS, rather EWS will return a different Attachment Class based on the Attachment type. Eg ItemAttachment, FileAttachment or ReferanceAttachment (eg for OneDrive Attachments). 0x0E21 is that Attachment number EWS is going to return the Attachments in the order of that number in GetItem request so you can compute that yourself. But the property is useless in EWS because to get an Attachment you need the EWSId unlike MAPI.
Cheers
Glen

EWS: Add userproperties to an EML when saving it?

In some of my mails, I have stored some userproperties via Outlook...
item.UserProperties.Add("CustID", Outlook.OlUserPropertyType.olText)
item.UserProperties("CustID").Value = custID.ToString
On a Pc (server) without Outlook installed, I want to download mail as EML and store the userproperties (like 'CustID' etc.).
My code for saving the EML:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim service As New ExchangeService(ExchangeVersion.Exchange2013)
service.Credentials = New WebCredentials("my#email.com", "mypsw")
service.Url = New System.Uri("https://outlook.office365.com/EWS/Exchange.asmx")
Dim entryID As String = "00000000C802F40668D27342B788D508E9210A67005DF9A7DEF7194A418C8515031D4262280000E00005DF9A7DEF7194A418C8515031D4262280002A9D18B320000"
Dim id As New ItemId(Convert(service, "my#email.com", entryID))
Dim email As EmailMessage = EmailMessage.Bind(service, id)
Dim ps As PropertySet = New PropertySet(ItemSchema.MimeContent)
email.Load(ps)
Using fileStream = New IO.FileStream("D:\sandbox\_email_test.eml", IO.FileMode.Create)
fileStream.Write(email.MimeContent.Content, 0, email.MimeContent.Content.Length)
End Using
End Sub
Private Function Convert(service As ExchangeService, mailbox As String, entryID As String) As String
Dim originalID As New AlternateId()
originalID.Format = IdFormat.HexEntryId
originalID.Mailbox = mailbox
originalID.UniqueId = entryID 'EntryId retrieved from email in Outlook
Dim altBase As AlternateIdBase = service.ConvertId(originalID, IdFormat.EwsId)
Dim convertedID As AlternateId = DirectCast(altBase, AlternateId)
Dim strConvertedID As String = convertedID.UniqueId
Return strConvertedID
End Function
Is there a way I can store userproperties in an EML? If yes, if I double click the EML to open it in Outlook, can I read back the userproperties from the EML?
Thanks!
User Properties are Mapi/Extended properties which aren't include in the MimeStream of a Message. If you add the properties as X-headers instead then they would be available in the MimeSteam otherwise you would better to export the Message as a MSG file which will give you full fidelity on all MAPI properties. To export as a MSG you will need to use Outlook and maybe look at something like redemption http://www.dimastr.com/redemption/rdo_introduction.htm if you need automation.
Cheers
Glen

Attaching Word Document as an Attachment - Path Specification ASP.NET MVC 3

I need to attach a word document to an email.
The document is being stored in the solution, in a folder called "Attachments"
Question
I was wondering what path i need to use in order to attach a Word document to an email and also would like to know if I am attaching it correctly.
Here is how I am doing it:
string fileName = "~/Attachments/worddocument.doc";
MailMessage mail = new MailMessage
{
Sender = new MailAddress(this.SenderAddress, this.SenderName),
From = new MailAddress(this.FromAddress, this.FromName),
ReplyToList = { new MailAddress(this.ReplyToAddress, this.ReplyToName) },
IsBodyHtml = this.isBodyHtml,
Subject = this.UserSubject,
Attachments.Add(new Attachment(fileName, MediaTypeNames.Application.Octet));
};
How does that look? Did I specify the path correctly?
Thanks
Attachment expects an absolute path.
You can convert your virtual path to an absolute path with
var absolutePath = Server.MapPath("~/Attachments/worddocument.doc")
And attach it with
Attachments.Add(new Attachment(absolutePath, MediaTypeNames.Application.Octet));
If you want to check if the file in the virtual directory exists, use
if (File.Exists(absolutePath))
...

Reading ALL custom properties on Exchange Server with EWS

I would like to read all "Custom properties" from EWS-Items. I do have the names of the properties (e.g. "duration" oder "distance") but I did not create them (my clients did).
I guess I will have to use "ExtendedProperties" of the "Item" class. But when I use Item.Bind() with a Property-set I need to know a GUID which I don't have! Microsoft says (http://msdn.microsoft.com/en-us/library/exchange/dd633697%28v=exchg.80%29.aspx):
"Create a GUID identifier from the GUID that was used when the extended property was created."
I don't have these GUIDs because I did not create the properties.
I guess the only chance is to use Item.Bind() without a specific property set. Would that slow down the process (I need to call this for every item in the mailbox)?
I basically would like to iterate with a clause like this:
if (extendedProperty.PropertyDefinition.Name == "duration")
Any ideas how to do this?
Thanks
Martin
try to recover it by name using the PropertyDefinition and property type (MapiPropertyType):
//Search for "duration" property
Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition extendedPropertyDefinition =
new Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition
(DefaultExtendedPropertySet.Common, "duration", MapiPropertyType.String);
EmailMessage msgMod = EmailMessage.Bind(service, msgComplete.Id,
new PropertySet(extendedPropertyDefinition));
ExtendedPropertyCollection colProp = msgMod.ExtendedProperties;
foreach (ExtendedProperty prop in colProp)
{
//Iterate properties
}

Resources