code for outlook issue

Joseph Hedbon 161 Reputation points
2022-06-11T00:27:27.14+00:00

Hello,

i'm trying to take user input from a few text boxes and have it populate into an email. this is what i have so far:

Imports System
Imports System.IO
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class NewHireEmail

Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked  
    Process.Start("https://nipr.com/help/look-up-your-npn")  
End Sub  

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
    Dim oApp As Outlook.Application  
    oApp = New Outlook.Application  

    Dim oMsg As Outlook.MailItem  
    oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)  

    oMsg.Recipients.Add("test@test.com")  

    oMsg.Subject = "test"  
    oMsg.HTMLBody = "<HTML><BODY>test - test</BODY></HTML>"  
    oMsg.Display()  
End Sub  

End Class

i've only gone this far and tried testing it to see if outlook does indeed open a new message before putting in what i want in the body, etc..

thank you

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
4,870 questions
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2022-06-13T16:01:23.897+00:00

    Hi
    Thanks for re-posting code.
    Here is another possibility as you explicitly say 'populate an email'. This uses the default email client on the computer where it is being run - often a good idea as not everyone has all of the email clients installed. Anyway, this should open your Outlook with the details filled in ready to send. If not of any use, then there are other methods (NOT specific to Outlook) that may be of use. Give this example a try (in a new test project). If it does do what you want, then further clean up and exception handling may be required.

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
    		Dim address As String = "myemail@hotmail.com, Test@bt.com"  
    		Dim subject As String = "Test email to me Sending through default email client"  
    		Dim cc As String = "freddy@google.com, number2@msdn.com"  
    		Dim bcc As String = "freddybrother@googlemail.com, anyoneelse@nosyparker.com "  
    		Dim body As String = "Heaven saying bearing whose whales that. So every she'd fowl of  
     I. Set lesser itself meat seasons spirit were so male firmament gathered greater. Bring lesser gathered moveth dominion good two whales years yielding. Them tree great doesn't, day all, spirit third. Divided their greater set."  
      
      
    		Process.Start(String.Format("mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", address, subject, cc, bcc, body))  
      
    	End Sub  
    End Class  
    

1 additional answer

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-06-11T02:57:50.06+00:00

    Hi

    I on't have time at the moment to try out this code found on StackOverFlow, but here is the link that you can have a look. I don't have Office/Outlook installed here to test it. Maybe there are more recent items to be found, but this is a slightly older one.

    https://stackoverflow.com/questions/65412215/how-to-send-emails-in-vb-net-using-the-outlook-library