question

JosephHedbon-9458 avatar image
0 Votes"
JosephHedbon-9458 asked LesHay-2099 commented

code for outlook issue

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

office-outlook-itprodotnet-visual-basic
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

i have imported the reference object for outlook into my project as well

0 Votes 0 ·

Hi

I can't find a question in your post. Also, what was the result when you tested the code?

0 Votes 0 ·

sorry about that. my question is nothing happens when i click the button. i was hoping an outlook email message window would pop up with the pre populated data so the user just hits send. the error i get is: " System.Runtime.InteropServices.COMException
HResult=0x80080005
Message=Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE))."

i copied the whole error but it exceeds the character limit. if you need more i can post in seperate comment.

this is my first go around with trying to use outlook within VB.net

Thanks



0 Votes 0 ·
LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered LesHay-2099 commented

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



· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so very much this worked perfectly. such a relief of a headache. didn't even think to just use the default mail client.

0 Votes 0 ·
LesHay-2099 avatar image LesHay-2099 JosephHedbon-9458 ·

Hi
Glad it helped.

0 Votes 0 ·

Hello, the code is working perfect now i'm trying to figure out formating with in the email client it self. i've done some looking around and playing with code. what i'm trying to do is have multiple lines in the body of the email itself with the generated text i'm doing and can't seem to get it working. i've tried using the stringbuilder class with appendliine, as well as using<br> with in the string for the body of the email. below is the code. i'm not sure if i need to add a reference or not.

Dim body As String
Dim subject As String
Dim address As String




     If RadioButton1.Checked = True Then
         Call NewAgent()
         Call NPNNumber()
         Call ManagerHierarchy()


         address = "test@test.com"
         subject = "New Hire " & firstlastname
         body = "hello <br> hello <br> hello"



         Process.Start(String.Format("mailto:{0}?subject={1}&body={2}", address, subject, body))

     End If

Thank you once again


0 Votes 0 ·
Show more comments
LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered JosephHedbon-9458 commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for this, i've been trying all weekend with a few different things but cant seem to get it to work. 210951-code.png



Here is what i've changed and tried but still can't get outlook to open a new message window when the user clicks the button. i've added the outlook view control reference as well. any other help would be great.

0 Votes 0 ·
code.png (148.6 KiB)
LesHay-2099 avatar image LesHay-2099 JosephHedbon-9458 ·

Hi

Please do not post code as an image. I had one idea for you to test but as you posted an image which I cannot copy/paste (and some hidden). I will wait for our code in a code block instead of an image.

0 Votes 0 ·

Sorry about the pic. here is the code:
Imports System.Runtime.InteropServices
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 OutlookMessage As Outlook.MailItem
     Dim AppOutlook As New Outlook.Application

     Dim objNS As Outlook._NameSpace = AppOutlook.Session
     Dim objFolder As Outlook.MAPIFolder
     objFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts)

     Try
         OutlookMessage = AppOutlook.CreateItem(Outlook.OlItemType.olMailItem)
         Dim Recipents As Outlook.Recipients = OutlookMessage.Recipients
         Recipents.Add("myemail@hotmail.com")
         OutlookMessage.Subject = "Sending through Outlook"
         OutlookMessage.Body = "Testing outlook Mail"
         OutlookMessage.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
         OutlookMessage.Save()
         OutlookMessage.Move(objFolder)
         OutlookMessage.Display(True)
     Catch ex As Exception
         MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line    

     End Try
 End Sub




End Class

Thank you for all your help.

0 Votes 0 ·