How to make a Form Application to Send Email via Outlook - vb.net

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.

Related

Verify if an Outlook Email Sent From VB.net was Actually Sent

I'm writing some code in VB.net which will zip some files with a password & email them to the recipient(s) & then send a second email to the recipient(s) with the password for the zip file. The email is sent through Outlook.
The people who would be using this also have some VBA code in the Application_ItemSend event procedure in Outlook which checks if there's attachments being sent to external email addresses & if so it gives the user the details & the option to Cancel sending the email - this is done using the Cancel property/argument (sorry I'm not good with the correct terminology for things in VBA/VB.net) of the Application_ItemSend event procedure.
If the user does cancel the first email then I'd like to stop the second email from going out aswell. I've tried checking (directly after invoking MailItem.Send()) MailItem.Sent.ToString but it bugs out on that line & gives this exception - System.Runtime.InteropServices.COMException: 'The item has been moved or deleted.
My first thought was that once MailItem.Send() was invoked that MailItem became nothing, but I've just put a watch for MailItem Is Nothing & it's returning False. I've tried searching but not been able to find anything.
I can work around by displaying the second email & leave it to the user to send or cancel but if possible I'd rather have the code take care of it.
Instead of sending the second email immediately, wait until Items.ItemAdd event fires on the Sent Items folder. Once the first message raises that event, you can create and send the second email.

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

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

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.

Open Outlook OFT template and populate recipient address

Working in a Windows Forms application and VB.NET.
I need to pop open an Office "New Message" window, populate the "To" address, and place an OFT template in the body of the message. I do not need to modify anything in the template.
To open the new message with the template, i could:
Diagnostics.Process.Start("MyFile.oft")
But this will not help with setting the TO address, or subject.
I also don't want to download any third party add-ons.
Any help??
You can use the Outlook Object Model and call Application.CreateItemFromTemplate. You can then set Subject/To/CC/BCC properties and the Recipients collection on the returned MailItem object.

Modifying email addresses in TO and CC using Outlook Add in

I am creating an outlook add in which needs to make modifications on the TO and CC fields before sending the message. I tried editing the TO property of the MailItem object but when I edit it, the mail doesn't get sent, it gets stuck in the Outbox. I also looked at the Recipients collection but it's read only so not of any use. Is there any mapping being maintained by Outlook between TO and Recipients collection which fails when I edit TO property?
What is the ideal way to make these changes and still have the mail be delivered properly?
Use yourMailItem.Recipients.Add("foo#bar.com") to add recipients.
To set them as CC set the recipient type to olCC (e.g. Recipients.Item(1).Type = olCC).