Get Signatures from Outlook and Detecting Body format - c#

I am using COM API to compose an e-mail.
objOutlook = new Microsoft.Office.Interop.Outlook.Application();
mic = (Microsoft.Office.Interop.Outlook.MailItem)(objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem));
mic.To = ccase.Email;
mic.CC = ccase.CC;
mic.Subject = ccase.Subject;
mic.Body = bodyBuffer.ToString();
// below line throws exception ?? Shouldn't it just use what is defined in outlook.
mic.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatUnspecified;
Question:
I want to define body to be plain Text, HTML, or Rich Text based upon what is defined in Outlook by the customer.
How can I retrieve the e-mail signature from Outlook and add it to the end of body ?
Any pointers will be appreciated.
Karephul

We decided not to do this as users might have configured multiple signatures. So, let users add signature in the end.

Related

Converting .MSG files to .TXT; Should I use Microsoft.Interop Outlook?

I'm trying to do a conversion from .msg files into .txt. I have two questions.
1)I've been investigating and found the Microsoft.Interop Outlook package and there is a way where I can extract the bodyHTML, To, Sent Date, and a few other properties but I feel as if this is a very manual process because I have to trim out all the html tags such as < br>, &nbsp, a href etc...
Here is my current code...
MailItem mailItem = outlookApp.Session.OpenSharedItem(item) as MailItem;
TextFile textFile = new TextFile(); //collection of properties I am interested in
textFile.To = mailItem.To;
textFile.Subject = mailItem.Subject;
textFile.Sent = mailItem.SentOn.ToString();
textFile.Name = Path.GetFileNameWithoutExtension(item);
var atttach = mailItem.Attachments; //Really just want the names
textFile.Body = RemoveStuff(mailItem.HTMLBody); //manually removing all html tags
textFiles.Add(textFile);
Marshal.ReleaseComObject(mailItem);
Does anyone know if there is a more effective way to do this in C# or a way using Interop that I am not aware of?
2)If I go the interop route, is there a way I can bypass the popup in Outlook asking if I can allow access to Outlook? Seems inefficient if my goal is to create a converter.
Any help is greatly appreciated.
Thanks!
Firstly, why are you using HTMLBody property instead of the plain text Body?
Secondly, you can use MailItem.SaveAs(..., olTxt) to save the message as a text file. Or do you mean something else by txt file?
The security prompt is raised by Outlook if your antivirus app is not up to date. If you cannot control the environment where your code runs, Extended MAPI (C++ or Delphi only) or a wrapper like Redemption (any language - I am its author) are pretty much your only option. See http://www.outlookcode.com/article.aspx?id=52 for more details.
In Redemption, you can have something like the following:
using Redemption;
...
RDOSession session = new RDOSession();
RDOMail msg = session.GetMessageFromMsgFile(TheFileName);
msg.SaveAs(TxtFileName, rdoSaveAsType.olTXT);

In EWS, how to get message's both plain-text and HTML bodies as strings keeping the original character encoding

I'm working with Exchange 2010 (not Exchange 2013 which lets the caller request both plain-text and HTML bodies directly).
To get HTML body, I'm using something like:
ExtendedPropertyDefinition PR_BODY_HTML = new ExtendedPropertyDefinition(0x1013, MapiPropertyType.Binary);
ExtendedPropertyDefinition PR_INTERNET_CPID = new ExtendedPropertyDefinition(0x3FDE, MapiPropertyType.Long);
PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties);
properties.RequestedBodyType = BodyType.Text;
properties.Add(EmailMessageSchema.Body);
properties.Add(PR_BODY_HTML);
properties.Add(PR_INTERNET_CPID);
...
byte[] htmlBodyBytes;
string htmlBody;
int iCP;
if (item.TryGetProperty<int>(PR_INTERNET_CPID, out iCP))
{
// The code never enters here
}
if (item.TryGetProperty<byte[]>(PR_BODY_HTML, out htmlBodyBytes))
{
htmlBody = Encoding.GetEncoding(65001).GetString(htmlBodyBytes);
}
string textBody = item.Body.Text;
For plain-text body, I get the correct string representation. But HTML body gives me just bytes and I don't know the codepage to pass to GetString. Currently, UTF-8's codepage is hardcoded but this won't work for production. I need to either find out the codepage of the HTML part or find another method of extracting it from the message. Of course, I could make a separate query to EWS setting RequestedBodyType = BodyType.HTML but I would better not make an additional query. I thought PR_INTERNET_CPID MAPI property (0x3FDE) will fit my needs but it's never populated (I double-checked that it exists on the mail server but I can't get it via EWS).
So I need to either convince Managed EWS library to return both HTML and plain-text as strings or get me PR_INTERNET_CPID value. What can I do for that?
OK, it turns out that PidTagInternetCodepage (PR_INTERNET_CPID) has type MapiPropertyType.Integer, not MapiPropertyType.Long (although MSDN says PT_LONG). After the adjustment, I can get the value in question just fine.

Font of OOF Replies

I am currently developing an Add-In for Outlook. This Add-In edits the OOF Settings of the logged in Outlook user. I have to take the text for the external message from a .oft File.
The Add-In is running fine and everything's working as intended. There is just this one little issue with the font. I would like to change the font of both replies to our company standard but i see no way of implementing that.
Is there a way of changing the font?
Body.Html allows you to specify a coercion type. Here you can specify HTML and add style to the text in the body.
For Example:
Office.context.mailbox.item.body.setAsync(
"<p style=\"font-family: 'Arial Black', 'Arial Bold', Gadget, sans-serif;\">text</p>",
{ coercionType:"html"},
function callback(result) {
// Process the result
});
For reference: https://dev.outlook.com/reference/add-ins/Body.html
You could Get the item you want to alter, and then Update it, as outlined here. What you will need to do is get the Body-property of the mail, and then alter the <body>-tag in this Body:
// As a best practice, limit the properties returned to only those that are required.
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Body);
// Bind to the existing item by using the ItemId.
// This method call results in a GetItem call to EWS.
Item item = Item.Bind(service, itemId, propSet);
// item.Body.value = "<html><body> Example body </body></html>"
// Update the style of the mail's body.
item.Body.value = "<html><body style='font-family: Arial'> Example body </body></html>"
// Save the updated email.
// This method call results in an UpdateItem call to EWS.
item.Update(ConflictResolutionMode.AlwaysOverwrite);
Thanks for your help!
The simplest way was to just add the html tags to the string which I use for the OofReply-object. Like so: string message = "<html><div style='font-size:11pt; font-family:Calibri;'>" + template.body + "</div></html>";

What component to use to send mail with any kind of body?

I made a mail sender in C# but I'm having trouble with the body of the mail. It only sends the texts without pictures, links and other elements. Not to mention, I used RichTextBox for that purpose.
So now my question is: what component to use to send mail with pictures, links and else?
I also enabled IsBodyHtml to true.
What I want to do is to copy the pictures, links and texts (with different colors and size) from Microsoft Word and paste it to control, and when user gets mail he gets the exact same body and layout as I send them.
You'll need to send it as html.
Save your word doc as html and use that.
For the images in your document you can either point to them via their absolute urls (publicly available via the internet).
Or you could use the LinkedResource class.
With the LinkedResource class your images have to specify a cid in the source.
var inlineLogo = new LinkedResource("path/myfile.png");
inlineLogo.ContentId = Guid.NewGuid().ToString();
var imageHtmlFragment = string.Format("<img alt='My Logo' src='cid:{0}' style='width: 250px;height: 60px;'/>",inlineLogo.ContentId);
var newMail = new MailMessage();
var view = AlternateView.CreateAlternateViewFromString(imageHtmlFragment, null, "text/html");
view.LinkedResources.Add(inlineLogo);
newMail.AlternateViews.Add(view);

Managing Lotus Notes Mail Format using C#

I am accessing mail body and fetching it in another mail.
But i am not getting original format of previous mail in new mail.
Problem i am facing in this situation are:
Not getting images in destination mail.
Font is also varying.
I am accessing mail body as follows:
NotesRichTextItem rtItem = (NotesRichTextItem)docInbox.GetFirstItem("Body");
String Body = rtItem.GetFormattedText(false , 0);
String bodyFormat = rtItem.type.ToString();
also tried this code:
NotesItem itemBody = docInbox.GetFirstItem("Body");
String bodyFormat = itemBody.type.ToString();
String Body = itemBody.Text;
But not getting solution in both case.
If you're trying to access rich text from Lotus Notes and put it into your own system, that will be very difficult and the NotesAPI is of little help to you. However, if you are trying to copy a rich text item from one NotesDocument to another, look at the AppendRTItem method of the NotesRichTextItem class.

Categories

Resources