Font of OOF Replies - c#

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>";

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.

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);

Read Text file and Email in Plain Text without affecting the original Text

I have a plain text file that I need to read in using C#, manipulate it a bit then I need to email it. That's easy enough, but it also has to stay in the same format as it's original state:
This is an excerpt from a sample file "mySample.txt":
*****************************NB!!!!**********************************
*Please view http://www.sdfsdf.comsdfsdfsdf . *
*********************************************************************
*** DO NOT DELETE or ALTER ANY OF THE FOLLOWING TEXT ***
Company X PTY.
Lorem Ipsum Office
Last Change - 01 February 2008
APPLICATION TO ESTABLISH A COMMUNITY WITHIN
THE RESTIN DISTRICT OF THE IPSUM.
===================================================================
1. COMMUNITY and ACTION
Give the name of the community. This is the name that will be
used in tables and lists associating the community with the name
district and community forum. The community names that are
delegated by Lorem are at the district level
The Action field specifies whether this is a 'N'ew application, an
'U'pdate or a 'R' removal.
1a. Complete community name:**{0}**
1b. Action - [N]ew, [U]pdate, or [R]emoval :**{1}**
As you can see I've got place holders {0} and {1} in the file which is to be replaced by an automated process.
In my C# I'm using a stream reader to read the entire file into a StringBuilder object then replacing the place holders using the StringBuilder.AppendFormat method.
The problem is when I add the text to a email message body and send it the format ends up looking different. It looks like a bunch of spaces or tabs get removed in the process.
private void Submit_Click(object sender, EventArgs e)
{
//create mail client
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(ConfigurationManager.AppSettings["SubmitToEmail"]);
message.Bcc.Add("xyz#test.com");
message.Subject = "Test Subject";
message.BodyEncoding = Encoding.ASCII;
message.IsBodyHtml = false;
message.Body = _PopulateForm(_GatherInput());//calls method to read the file and replace values
client.Send(message);
//cleanup
client = null;
message.Dispose();
message = null;
}
Anyone have any ideas on how to keep the formatting in tact?
Thanks,
Jacques
The problem you've got is that you're sending plain text as HTML. It's natural that the layout gets changed because HTML is displayed differently than text. Plain text has new lines (\n), tabs (\t), etc., while HTML has line breaks (<BR>) and different layout methods.
If I were you, I would 1st start out by replacing new lines with <BR> (there should be a replace function x.Replace("\n", "<BR>");)
As for the text items that are centered, wrap them in <p style="text-align:center" }>, </p>.
The answer seems to have been with .net and the Mail message object. Instead of adding the content to the Body property, you have to create an alternateView object and add it to that. That solved my problem.
Regards,
Jacques

Get Signatures from Outlook and Detecting Body format

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.

Categories

Resources