0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Rahul
  • 51
  • 1
  • 9
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 22 '13 at 21:56
  • 1
    Have you tried anything yet? You seriously haven't been able to find a single example of using the Outlook API? – John Saunders Dec 22 '13 at 21:56
  • 1
    A quick search of the [tag:outlook] tag found http://stackoverflow.com/questions/248569/starting-outlook-and-having-an-email-pre-populated-from-command-line, http://stackoverflow.com/questions/6938002/c-sharp-outlook-interoperability, and http://stackoverflow.com/questions/301882/reading-e-mail-without-outlook-app-open. – John Saunders Dec 22 '13 at 22:05
  • I tried to do the SMTP code but that one works with GMail and other service provider types. But wanted to be able to send via Outlook. Not, i was not able to find any Outlook related, even though I found those were for C# and really dint prove good to me. These were the links i remember reading and tried previously and dint help. – Rahul Dec 22 '13 at 22:22
  • Show us what you tried. Are you saying that you totally can't read C# and translate to VB.NET? – John Saunders Dec 22 '13 at 22:24
  • Very frankly, am new to VB.net coding and C#. I tried to run those code that I got from StackOverFlow and MSDN and none of them worked. That put me into frustration and I deleted the code, but I still cant live without it because i want to learn it and do it. I know it sounds strange, but this is the truth. – Rahul Dec 22 '13 at 22:34

1 Answers1

2

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.

OSKM
  • 728
  • 14
  • 25
  • Thank you so much OSKM. This has helped me well. Need to do some extensive testing to get this fine tuned. This frame was what i was looking for. – Rahul Dec 25 '13 at 12:31
  • Thank you OSKM, this worked like a charm, just that i had to enter a few references for the project. ProjectProperties > References tab > Add Reference > "Microsoft Office 14.0 Object Library" – Rahul Dec 25 '13 at 22:25
  • One more issue that am hitting on is the ".Importance". If am using the ".Display()" it works fine, but if I use "Send()" it gives an error "COMException was unhandled. Item has been moved or deleted" – Rahul Dec 25 '13 at 22:28
  • 1
    if you are using `.Send()` then it has to be the last command line as amended above (not critical in using the `.Display()`) – OSKM Dec 27 '13 at 17:53
  • Thanks for the update on this OSKM. this did the trick of adding the `.Importance` before `.Send()`. I had another issue that cropped up which am not able to resolve. If we are using usernames to resolve and if there more than 1 usernames with similar usernames it fails and gives an error that it was not able to resolve it. Any code that can resolve it..? – Rahul Dec 27 '13 at 22:12