I am currently developing an add-in for Microsoft Outlook that adds an image right before the tag of an html body. This image is located on a remote server so the source of the image tag is http://someserver.com/image.jpg
This works exactly as expected on a fresh e-mail ( aka a new e-mail )
However when a user clicks reply, or forward for some reason the image source gets changed to cid:image001.jpg and the actual image source gets put in the alt tag.
I am altering the body on the send event as I want the image to be added after the e-mail is finished being written.
The code that is run at the send event
void OutlookApplication_ItemSend(object Item, ref bool Cancel)
{
if (Item is Outlook.MailItem)
{
Outlook.MailItem mailItem = (Outlook.MailItem)Item;
string image = "<img src='http://someserver.com/attach.jpg' width=\"100\" height=\"225\" alt=\"\" />";
string body = mailItem.HTMLBody;
body = body.Replace("</body>", image + "</body>");
mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mailItem.HTMLBody = body;
}
}
So I found a way to do it that works. What I ended up having to do was create a new mailItem, copy the existing mailitem into it, modify and send that item and cancel the original. The following code shows how I did it:
void OutlookApplication_ItemSend(object Item, ref bool Cancel)
{
if (Item is Outlook.MailItem)
{
Outlook.Inspector currInspector;
currInspector = outlookApplication.ActiveInspector();
Outlook.MailItem oldMailItem = (Outlook.MailItem)Item;
Outlook.MailItem mailItem = oldMailItem.Copy();
string image = "<img src='http://someserver.com/attach.jpg' width=\"1\" height=\"1\" alt=\"\" />";
string body = mailItem.HTMLBody;
body = body.Replace("</body>", image+"</body>");
mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mailItem.HTMLBody = body;
mailItem.Send();
Cancel = true;
currInspector.Close(Outlook.OlInspectorClose.olDiscard);
}
}
I'd recommend adding images inside the ... elements. You can read more about HTML rendering capabilities in the following series of articles:
Word 2007 HTML and CSS Rendering Capabilities in Outlook 2007 (Part 1 of 2)
Word 2007 HTML and CSS Rendering Capabilities in Outlook 2007 (Part 2 of 2)
Word is used as an email editor in latest Outlook versions.
Do you get any issue with the ItemSend event handler? Does it work?
Related
Am trying to display HTML content inside the Outlook Appointment body but am not able to (i tried for both body and FTFbody option). Anyone Please help me . Below is my code .
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Application application = Globals.ThisAddIn.Application;
Outlook.Inspector inspector = application.ActiveInspector();
Outlook.AppointmentItem AppointmentItem = inspector.CurrentItem as Outlook.AppointmentItem;
if (AppointmentItem != null)
{
if (AppointmentItem.EntryID == null)
{
string messageBody = string.Empty;
string defaultMessageBody = "Click <a href='https://www.google.com/' > here </a>";
AppointmentItem.Subject = "This text was added by using code";
AppointmentItem.Recipients.Add("testuser1#gmail.com");
AppointmentItem.Location = "INDIA";
//AppointmentItem.RTFBody = System.Text.Encoding.Default.GetBytes(defaultMessageBody);
AppointmentItem.Body = defaultMessageBody;
AppointmentItem.Save();
}
}
}
Firstly, you should not be using Outlook (or any other Office app) under a service (such as ASP.Net).
Secondly, Outlook 2016 and higher supports HTML in appointments, but that support has not been exposed through the Outlook Object Model. You can attempt to set the PR_HTML property (DASL name http://schemas.microsoft.com/mapi/proptag/0x10130102) using AppointmentItem.PropertyAccessor.SetProperty, but Outlook won't see your change until you completely dereference and reopen the item.
We need to convert the HTML content to the byte array and then display it in the HTML body then we can able to get the full html body . Until unless byte conversion we are not able to get the html content in the appointment plug-in . please vote if it solve your problem. Thank you :)
I want to send a Mail via Outlook and C# but I have a problem with the placement of my Attachments. I have the following code:
if (strBody.StartsWith(#"{\rtf"))
{
mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatRichText;
mailItem.RTFBody = Encoding.UTF8.GetBytes(strBody);
mailItem.Attachments.Add(strAttachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, int.MaxValue, null);
}
else
{
mailItem.Body = strBody;
mailItem.Attachments.Add(strAttachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, null);
}
My strBody has the following Value:
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Arial;}}
{\colortbl ;\red255\green0\blue128;\red0\green128\blue255;}
\viewkind4\uc1\pard\fs20 Sehr geehrte \cf1 Damen\cf0 und \cf2 Herren\cf0 ,\par
\par
hier ihre AB\fs20\par
}
But my Mail looks like this:
Now my Question is,
Can the Attachments be displayed as an extra Row like when the Mail is not RTF formatted?
If not 1., then how can I get my Attachments to be displayed at the End?
Well you did everything right. Every value > 1 will place the attachment at the end of your mail. After "hier ihre AB" it is placed. Looks stupid but well...
As a little workaround, I used it like that too, place some new lines. As much as it takes to place the Attachment under your last sentence.
Or you write the Mail as a HTML Type. Less problems.
EDIT:
As you can see, the file is placed at the end of the mail.
EDIT II:
Here is a example for a method to send your E-Mail as HTML with the attachment in the attachment row:
static void Main(string[] args)
{
Outlook.Application tmpOutlookApp = new Outlook.Application();
Outlook.MailItem tmpMessage = (Outlook.MailItem)tmpOutlookApp.CreateItem(Outlook.OlItemType.olMailItem);
tmpMessage.HTMLBody = "Test";
String sDisplayName = "Test";
int iPosition = (int)tmpMessage.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = tmpMessage.Attachments.Add(#"C:\Test.txt", iAttachType, iPosition, sDisplayName);
tmpMessage.Subject = "Your Subject will go here.";
Outlook.Recipients oRecips = (Outlook.Recipients)tmpMessage.Recipients;
Outlook.Recipient tmpRecipient = (Outlook.Recipient)oRecips.Add("EMail");
tmpRecipient.Resolve();
tmpMessage.Send();
}
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
I'm trying to use the attachments included in calendar items pulled progmatically.
I have a list of chosen calendar subject lines from a previous dialog box, and while the subject is transferring properly, the body isn't working well (another question altogether) but the attachments aren't working whatsoever.
Here's my foreach loop where the attachments are being placed into an Attachments array for use later:
string[] subjects = new string[dialog.chosen.Count];
string[] bodies = new string[dialog.chosen.Count];
Attachments[] attach = new Attachments[dialog.chosen.Count];
foreach (Outlook.AppointmentItem appt in rangeAppts)
{
foreach (string text in dialog.chosen)
{
if (text == appt.Subject)
{
subjects[i] = appt.Subject;
bodies[i] = Convert.ToString(appt.Body);
attach[i] = appt.Attachments;
i = i + 1;
}
}
}
And then here's where I actually call the method:
sendEmailTemplate(bodies[i], subject, to, "", attachment: attach[i]);
And then the method itself:
public void sendEmailTemplate(string body, string subject, string to, string cc , Attachments attachment = null)
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMailItem.HTMLBody = body;
oMailItem.Subject = subject;
try
{
oMailItem.Attachments.Add(attachment);
}
catch {}
oMailItem.To = to;
oMailItem.CC = cc;
oMailItem.Display(false);
oMailItem.Application.ActiveInspector().WindowState = Microsoft.Office.Interop.Outlook.OlWindowState.olNormalWindow;
}
I've tried several things, however when I actually go to send the e-mail, I end up getting:
Exception: Member not found. HRESULT: 0x80020003
And then I haven't been able to get anything else to work. The try/catch loop on the method is to prevent the above exception as I was getting that exception regardless of whether or not an attachment was present, and now attachments just aren't being added.
I'm using Interop that comes with Office along with C#. Winforms if that makes a difference.
MailItem.Attachments takes either a string (fully qualified file name), or another Outlook item (MailItem, ContactItem, etc.).
You cannot pass Attachments object as an argument. If you need to copy the attachments, loop through all attachments in the Attachments collection, call Attachment.SaveAsFile for each attachment, pass the file name to MailItem.Attachments.Add, delete thee temporary file.
I'm having a slight problem with adding text to forwarding emails. This is my current code:
private void ForwardFunction(Outlook.MailItem email)
{
Outlook.MailItem forwardEmail = ((Outlook._MailItem)email).Forward();
Outlook.Inspector forwardInsp = forwardEmail.GetInspector;
Word.Document forwardDoc = forwardInsp.WordEditor;
Word.Range forwardRange = forwardDoc.Range(0,1);
string forwardText = "This is some text";
forwardRange.Text = forwardText + forwardRange.text
newEmail.Recipients.Add("myemail");
forwardEmail.Save();
((Outlook._MailItem)forwardEmail).Send();
}
I've gone through it and it does add the text to the range, but when I receive the forwarded email it doesn't contain any of the additional text. I've used similar code to edit current emails that the user is editing (New, Replies/Forwards, InlineResponses) with success, but the email being passed to the function is the currently selected email in the inbox. Not sure if this matters, maybe because it's not being edited by the user.
I couldn't find a specific way to add new text to a programmatically forwarded email.
For anyone else interested in this, I ended up using the .HTMLBody. It seems that using the .GetInspector either gets the inspector of the _email instead of fEmail and you can't edit it or it gets the correct inspector but it can't edit it. Either way, using the .HTMLBody seems to get around it.
Outlook.MailItem fEmail = ((Outlook._MailItem)_email).Forward();
string forwardText;
forwardText = "class=MsoNormal>";
forwardText += "This is my forwarded message";
forwardText += "Bonus hyper link <a href='" + hyperlinkText + "'>" + hyperlinkText + "</a>";
var regex = new Regex(Regex.Escape("class=MsoNormal>"));
fEmail.HTMLBody = regex.Replace(fEmail.HTMLBody, forwardText, 1);
fEmail.Save();
fEmail.AutoForwarded = true;
((Outlook._MailItem)fEmail).Send();