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
Related
I am using Redemption and the RDO Objects. I want to be able to set the sender address so that outlook will show who the email is coming from. So I connect to Outlook and the inbox and create my msg object
app = new Application();
session = app.CreateObject("Redemption.RDOSession");
try
{
session.Logon(Program.outlookProfileName);
RDOFolder inbox = session.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);
msg = inbox.Items.Add();
}
I display an email form with the from address(prepopulated based on user), the TO, subject, and body
The user fills in the to box with the intended recipient(s), the subject and the body. They click the Send button. I do the following code
msg.Recipients.ResolveAll();
msg.SentOnBehalfOfEmailAddress = SenderTB.Text;
msg.Subject = SubjectTB.Text;
msg.Body = BodyTB.Text;
msg.Send();
But the sender address does not show properly in outlook. I want the sender address to show whatever was in the SenderTB.Text. How do I set the Sender for the msg object?
Is this for a delegate Exchange mailbox? Either set the SentOnBehalfOfName (not SentOnBehalfOfEmailAddress) or SentOnBehalfOf (RDOAddressEntry).
How do I select multiple items and open new mail form and add all selected items as attachments to the new email so I can send them together with the new email?
I guess, this could be broken into:
how to get multiple selected items in outlook (say user selects them all while holding Ctrl key to do multiple selection)?
How to open New Email form in Outlook?
How to attach selected item in 1 above and add them to the new email as attachments?
UPDATE:
So far I have managed to do following (Thanks to Dmitry's comment):
public void SendSelectedMailsAsAttachment()
{
try
{
Selection olSelection = HostAddIn.ActiveExplorer.Selection;
var count = olSelection.Count;
var items = new List<IItem>();
Microsoft.Office.Interop.Outlook.MailItem oMailItem = HostAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
foreach (var sel in olSelection)
{
oMailItem.Attachments.Add(sel);
}
oMailItem.Display(false);
}
catch (Exception ex)
{
//ex message
}
}
but with this code, following happens:
if I select single email and try to send it as attachment, the code above executes just fine and new email form opens but it will show no attachment.
if I select multiple emails and try to send them as attachment, the code above will throw exception when calling oMailItem.Attachments.Add(sel) for the 2nd time with exception "This operation is not supported until the entire message is downloaded. Download the message and try again."
Application.ActiveExplorer.Selection collection
Application.CreateItem / MailItem.Display
Loop through the items in the Application.ActiveExplorer.Selection collection and call MailItem.Attachments.Add for each item.
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?
I have a program that is using Outlook to send messages with attachments. It is working ok, sending emails with attachments but in outbox there is no attachment in the message. When somebody receive the message the attachment is visible but in outbox not.
Here is some code:
Outlook.MailItem mail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
mail.Attachments.Add(Application.StartupPath+"/"+attachment, iAttachType, null, attachment);
mail.To = email;
mail.Subject = "Something";
mail.Body = "Some body";
mail.Send();
Before this I use:
private Outlook.Application outlookApp;
private Outlook._NameSpace outlookNameSpace;
private Outlook.MAPIFolder outbox;
and
outlookApp = new Outlook.Application();
outlookNameSpace = outlookApp.GetNamespace("MAPI");
outlookNameSpace.Logon(null, null, false, false);
outbox = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
My outlook program is connected with Microsoft Exchange Serwer. When I was using an application written in C++ it saved attachment in messages in outbox.
Thx for help!
You could be working with an old version of the outlook item.
This can happen if you keep references to your mail items, rec-patterns, inspectors and some other types [that I now forgot] longer than you need them.
Your reference will often point to the old version of the item and keeping it can also prevent you from getting a reference to the updated one (the one with the attachment), even when events (Folder.BeforeItemMove) are triggered.
Also, have you tried if mail.Save() would do anything for you?
This is what I use as soon as I am done with an item.
public static void NullAndRelease(object o)
{
if (o == null) {
return;
}
try {
int releaseResult = 0;
do {
releaseResult = System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
} while (releaseResult >= 0);
} catch {
} finally {
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
The catch has no message and is not important in my case. It is there if someone would pass in a reference that leads to something other than a com object. You can also try FinalReleaseComObject(o).
In Outlook, I can set the subject for a new message (when composing a new mail message), but I want to prepend text. So I need to get the subject first, and then set it.
Outlook.Application application = Globals.ThisAddIn.Application;
Outlook.Inspector inspector = application.ActiveInspector();
Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;
if (myMailItem != null && !string.IsNullOrEmpty(myMailItem.Subject))
{
myMailItem.Subject = "Following up on your order";
}
This code works on replies, but not for new messages, because in that case, myMailItem is null.
This is what I was looking for:
if (thisMailItem != null)
{
thisMailItem.Save();
if (thisMailItem.EntryID != null)
{
thisMailItem.Subject = "prepended text: " + thisMailItem.Subject;
thisMailItem.Send();
}
}
The subject was null until the mail item had been saved, either because it was sent, or as a draft. We can save it programmatically and then get the subject.
One other note: if the subject is blank at the time of saving, it will still show as null.