Outllook VBA: Get Header Info from attached Email to an Email - vba

Challenge description
I'd like to extract the header information from emails in an Outlook folder.
This works so far.
But there are Emails which where scanned by Spamassassin and found as being SPAM. So the SPAM Mail is attached to a new mail as Mail-Attachment.
Now i'd like to extract the header information from the original email-header.
What I already have
I am getting the header information from the 'normal'- mail and can access the Outlook-mail-item and also found the attachement.
What I look for
The easiest way to get the attachment as Outlook-mail-item so that I can perform the getHeader-operation operation. And, if possible without the need to open the attached mail.
Is there a way from the olmailItem to the attached mail (.msg-file) without opeining the attachment?
(Manually - with opening the mail - this can be done by opening the attached mail and look at the message options.)

Outlook does not let you directly access embedded message attachments. The best you can do is call Attachment.SaveAsFile to save the embedded message attachment as an MSG file, then open it using Application.Session.OpenSharedItem.
If you using Redemption is an option (I am its author), it exposes EmbeddedMsg property on both the Attachment object (returned by the SafeMailItem object) and RDOAttachment object (returned by the RDOMail object).

Related

Add a hidden tag to outgoing mail

I create a mail by VBA when sending invoices and automated newsletters by Outlook (2019).
For reimporting the mails from the Sent folder into the database it would be convenient to read a hidden tag including the customer number which I would like to embed into the mail.
You can add a custom property either using MailItem.UserProperties.Add or by setting your custom property directly using MailItem.PropertyAccessor.SetProperty.
Keep in mind adding a user property can force Outlook to send in the RTF format (the infamous winmail.dat attachment), so the latter solution is preferable. You just need to make sure you get the DASL property name (to be used with MailItem.PropertyAccessor.SetProperty) right. You can use the former approach (MailItem.UserProperties.Add), take a look at the message in the Sent Items folder with OutlookSpy (I am its author - click IMessage button), then use the DASL property name replacing MailItem.UserProperties.Add with MailItem.PropertyAccessor.SetProperty

How to Send All Emails From Outlook Outbox in VBA

I'm using Excel VBA to stage emails in Outlook and it is working well.
Dim template As Outlook.MailItem, tomerge As Outlook.MailItem
' Create E-mail
tomerge.Close olSave
The e-mails can then be manually moved to the Drafts folder and sent using this Sub.
'Loop through items in Drafts folder
objDrafts.Item(i).Send
However, many users have a bunch of extra drafts in their Drafts folder that they don't want sent.
If I replace "olFolderDrafts" with "olFolderOutbox" and try to send from their Outbox. The first message sends and then I get a "Run-time error" "Outlook has already begun transmitting this message".
Is there some way to send all from the Outbox or even better is there someway to stage and send from a newly created folder?
You need to create a folder for your unsent items and process them separately. As a rule the Outbox folder contains already submitted items. So, that's not a right place for your items.
The Outlook Object Model provides the Add function of the Folders class. You can get an instance of the Folders class using the Folders property of the Folder class in Outlook. You can read more about that in the How To: Create a new folder in Outlook article.
This answer was inspired by Nagarajan's comment above, but there are quite a few changes necessary from the answer in Send/Receive in Outlook Via Code. The main problem is using olSave doesn't put the messages in a "ready to send" state in Outlook so starting a sync using syc.Start from the answer above does nothing.
Instead, we found the following process to be straight forward:
Put Outlook in offline mode using "Send/Recieve" -> "Work Offline"
Use Excel VBA to stage the e-mails and instead of saving just .Send each email. As Outlook is offline they will be staged and ready to be sent but not actually sent.
The e-mails should now be staged in Outlook's Outbox folder and can be reviewed just be sure to press the "send" button after reviewing/editing messages otherwise the e-mail you modified will be removed from the queue.
When the messages are ready to be sent put Outlook back in online mode and they will be sent automatically.

Macro/Script - Set Forward Blue Line as Reference to delete previous emails when forwarding without manually deleting the previous emails

Seeking your insight on this process:
I want to forward the latest email received only not including the entire thread of the emails. Can I forward that email without manually deleting the previous emails on the thread?
I want to automatically send the NEWEST/LATEST emails received to a specific email address without the previous emails on the thread.
What I'm thinking is, I will set the "BLUE LINE" as reference, for starting position and ending position. Because as we all know, whenever we forward a HTML email, there's a blue line separating emails received.
Is it possible to do that? Set blue line as reference, then delete everything not inside the lines.
You can edit the message body at runtime using VBA macros. The Outlook object model provides three main ways for working item bodies:
Body - a string representing the clear-text body of the Outlook item.
HTMLBody - a string representing the HTML body of the specified item.
Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to set up the message body.
You can read more about all these ways in the Chapter 17: Working with Item Bodies. It us up to you which way is to choose to customize the message body.
If you are new to VBA macros, see Getting Started with VBA in Outlook 2010.

How to change salutation for multiple recipients in Outlook with c#

I want to code an outlook addin and try to replace a placeholder in an outlook mail with the salutation of the recipient. I get the salutation from AD. I tried this in ItemSend, but the body for different recipients contains always the last replacement.
Then i generated a new mail per recipient. That seems to be the right way, but there is the next problem. The mailitem.htmlbody contains not the same content like shown in the outlook message window. All styles of the original message are lost and for instance my signature is bad formatted.
Has anyone an idea to solve my problem?
First of all, if want to send an individual "salutation" for each recipient you need to send separate emails. For example, you can check out the list of recipients in the ItemSend event and if it contacts more than one entry in the collection you can cancel the send operation setting the Cancel parameter to true and prepare an individual email to each recipient from the collection.
You may consider using the Word object model instead of modifying the raw HTML markup of the body. The WordEditor of the Inspector class returns an instance of the Document class from the Word object model. The Chapter 17: Working with Item Bodies describes all possible ways of working with item bodies.

How to make a Form Application to Send Email via Outlook

I tried to search across on StackOverflow and Google, but had no success.
Am creating a form application to accept some information for the body of an email, create a HTML email and send via Outlook.
Everywhere I looked and found sending via GMail. But I want to be able to send via outlook without user interruption.
Could someone help me with a code to call outlook, frame the message and send automatically. Also should be able to enter some extra recipients via their username on the domain and it should automatically resolve and pickup the email and send to them when sending via outlook.
The message contents may have fields like Name, Email Address, Phone Number, Address. This should all sit inside a HTML email in a table.
Whilst i am puzzled you cant find what you are looking for i am going to provide an answer as the title is very clear so it could simplify searching for others in the future.
Dim Outlook As New Microsoft.Office.Interop.Outlook.Application
Dim MailItem As Microsoft.Office.Interop.Outlook.MailItem
MailItem = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
With MailItem
.HTMLBody = "put the body of your email here as a string"
.Subject = "Subject Line Info"
'use the below to change from the default email account
.SentOnBehalfOfName = "YourEmail#yourdomain.you"
'you can add multiple recipients using .Add()
.Recipients.Add("Recipient#theirdomain.them")
'examples of other optional arguments that can be included
.Attachments.Add([file])
.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh
.Display() 'opens the email for checking prior to sending or use .Send()
End With
As per the comment from Rahul below you will also need to add a Reference to the Microsoft Office 14.0 Object Library.