MailItem.Reply() doesn't reply inline - c#

I want to be able to reply to an email inline, from within my program, but calling the reply function seems to only fill in the To: and Subject: field for me. I can't seem to find anything online about it and it doesn't seem to have an overload that turns on inline replies.
I found that each MailItem has a Conversation so I'm wondering if I have to manually do the inline reply? (I would really like to avoid that, it would be a pain to iterate through a conversation, some of ours can be hundreds of emails long).

I found that this code helped make this happen;
Document oldDoc = itemToReplyTo.GetInspector.WordEditor;
Document newDoc = newMailItem.GetInspector.WordEditor;
if (oldDoc != null)
{
Microsoft.Office.Interop.Word.Selection oldSelect = oldDoc.Windows[1].Selection;
Microsoft.Office.Interop.Word.Selection newSelect = newDoc.Windows[1].Selection;
oldSelect.Find.Execute("From:");
oldSelect.Collapse(WdCollapseDirection.wdCollapseStart);
oldSelect.MoveEnd(WdUnits.wdStory, 1);
oldSelect.Copy();
newSelect.Move(WdUnits.wdStory, 1);
newSelect.InlineShapes.AddHorizontalLineStandard();
newSelect.Paste();
newSelect.Move(WdUnits.wdStory, -1);
newSelect.InsertAfter("Reply Text here");
newSelect.Find.ClearFormatting();
newSelect.Find.Execute(mailItem.SenderEmailAddress);
}

I tried the below and it worked for me using Outlook 2010.
Outlook.MailItem reply1 = mailItem.Reply();
//prepend text to email
reply1.HTMLBody = "<html><body><strong>Test Body</strong><p><p></body></html>" + reply1.HTMLBody;
reply1.Display(false); //display message or .Send() to send

Related

Get Email Message using Exchange webservices without attachment, using C# (in UiPath)

I am getting emails from a mailbox using exchange webservices using a custom code block in C# (I'm not versed in C# at all so forgive code quality!) in UiPath. I am passing the exchange service and folder ID as arguments to the code block. I noticed that when there was a large attachment on the email it took significantly longer. I am not interested in the attachment I just want to be able to access some information about the email. This was my initial code:
//Search for oldest email
ItemView objView = new ItemView(1);
objView.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
FindItemsResults<Item> lstItems = objServer.FindItems(in_FolderID, objView);
//Bind to email from result
if(lstItems.Count() == 0)
{
Console.WriteLine("Inbox appears to be empty");
out_ExchangeMessage = null;
out_InternetMessageID = null;
}
else
{
Item objItem = lstItems.ElementAt(0);
Console.WriteLine("Retrieving email: " + objItem.Subject);
PropertySet objPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent, EmailMessageSchema.IsRead,ItemSchema.Attachments,ItemSchema.TextBody);
out_ExchangeMessage = EmailMessage.Bind(objServer,objItem.Id, objPropertySet);
out_InternetMessageID = out_ExchangeMessage.InternetMessageId;
Console.WriteLine("Message Retrieved: " + out_ExchangeMessage.InternetMessageId);
}
I tried removing ItemSchema.Attachments so this line reads as follows. But the email still takes significantly longer to download
PropertySet objPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent, EmailMessageSchema.IsRead,ItemSchema.TextBody);
Is there a way to speed up the retrieving of emails with large attachments?
Because your including ItemSchema.MimeContent in your propertyset that is going to give you the whole MimeStream of the Message including attachments. If you don't require the MimeStream don't request it. Eg you should be able to get all the other properties of the Message eg body,subject,headers etc from other properties so it would only be required if you wanted to save the message.

C# and EWS How to Save Message as an Email Instead of a Draft

What I have done so far is to connect to EWS, access my inbox, create an item (email) with some info in Body, Subject, From, and To, save it to the Draft folder, and finally move it to my inbox. It works, however, I get a draft in the inbox instead of an email.
Is it possible to get the message as an email with the above scenario and how can I achieve that?
Below is my code. Any input would be very appreciated.
try {
message.Save();
}
catch(Exception e21) {;
}
message.Load(PS);
message.From = new EmailAddress("someone#abc.com");
message.ToRecipients.Add("me#abc.com");
message.Body = "This is A test take 1";
message.Subject = "Testing to send as someone else...";
// add in the attachments......
message.Update(ConflictResolutionMode.AlwaysOverwrite); // require this here why????
message.Copy(theTempFolder.Id); // get the item as a draft in my mailbox instead of an email
}
catch(Exception e99) {
Console.WriteLine("Exception fail to connect to office 365 on the cloud: " + e99.Message);
}
You need to set the MessageFlags property https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagmessageflags-canonical-property to 1 (before you call update) eg
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
message.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
Which will then make the message look like it was received. The other way is just import an EML like https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-import-items-by-using-ews-in-exchange
Have you tried to send it to yourself with either message.Send() or message.SendAndSaveCopy() ? see more here

Reading the Mail Body content on Outlook Addin VSTO

I am developing an Outlook add-in using VSTO that checking spelling of the mail content while composing.
In Reply mails, How can I check only the reply content by excluding the old conversation ?
This is what I am doing now.But I need to know whether there is any proper object or method to get current reply content.
Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem;
string temp = mailItem.Body;
int target= temp.IndexOf("\r\nFrom:");
string contentToCheck= temp.Substring(0, target);
There is no built in property that signifies the end of the new message and the start of a reply in body text.
What can be done is store common text that marks the end of the new message and check for that when reading the body. Things like ' FROM:', 'Regards,' 'Thanks,'...
Something similar to:
while ((line = bodytext.ReadLine()) != null)
{
foreach(string ending in MYLISTOFCOMMONENDINGS)
{
if (line.StartsWith(ending))
return sb.ToString(); //we are done
//here sb is a string builder consuming new lines
}
//read the line and check spelling
}
Look for the bookmark named "_MailOriginal". The script below inserts text just before the original message begins:
set objDoc = Application.ActiveInspector.WordEditor
If objDoc.Bookmarks.Exists("_MailOriginal") Then
' is there the original email? (_MailOriginal)
set objBkm = objDoc.Bookmarks("_MailOriginal")
Set objSel = objDoc.Application.Selection
objSel.Start = objBkm.Start-2 'give room for the line break before. It includes the line
objSel.End = objSel.Start
objSel.Text = "test"
End If

Get Url of attachment (IMessage.Attachments)

I'm trying to code a chat-log function into my bot. I know of IMessage.Attachments, but I don't know how to actually use it.
I don't have my exact code on hand (for now), but it looks like this:
_client.MessageReceived += async (message) =>
{
var attach = message.Attachments.Url as IMessage; //get Url of attachment
if ('message contains attachment (?)' && !message.Author.IsBot)
await message.Channel.SendMessageAsync("Attachment: " + attach); //send attachment
};
Of course, that doesn't work. The documentation didn't really help me either. It would be nice if someone could explain this to me.
Got it to work with this:
message.Attachments.FirstOrDefault().Url

Create/Open existing msg from path to new Outlook.MailItem in c#

Hello I'd like to create a Outlook.MailItem ( I believe ) from an existing one located on disk. I have the path stored in a string, and would like to access to save the body and attachments from it.
I can't seem to figure out how to open it in c# and access it.
currently I have something along the lines of
where fl evaluates out to something like "C:\users\msgs\email.msg"
Thanks for the time
Outlook.Application app = new Outlook.Application();
try
{
foreach (String fl in Directory.GetFiles(docInfo.LocalPath + _preprocessorDirectory))
{
if (Regex.IsMatch(fl.Trim(), _regex, RegexOptions.IgnoreCase))
{
Outlook.MailItem email = new Outlook.MailItem(fl);
SaveAttachments(email);
SaveBody(email);
}
}
}
catch (Exception ex)
{
logger.Error("Error in Process for document " + docInfo.OriginalPath, ex);
callback.Invoke(docInfo, false);
}
return false;
To open an item in outlook try:
var email = (Outlook.MailItem)app.Session.OpenSharedItem(fl)
From there, you can access the Attachments property and Body property as well.
Also, as I mentioned in my comment if the Regex.IsMatch is to determing the file extension, use Path.GetExtension() instead
I used this NuGet package: https://www.nuget.org/packages/MSGReader/
Seems to work fine. I prefer it to the MS OutlookApi library because it doesn't require Outlook to be installed.
I appreciate that it won't create instances of MailItem, as you have asked for in your question - but it will enable you to extract save the individual attachments and the body...

Categories

Resources