How can  the contents of a textBox (or RichTextBox) be sent as email by using a button click-event in VB.NET

Posted on 2009-04-25
Last Modified: 2013-12-17
I am wirting an app which combines the inpiut from various sources into a textbox. I want to send selected text  from this textbox as email by clicking a button. Either the button's text property would be set to the email address, or this could be picked up from another textbox, or, alternatively it could be entered by the user. I think I need to use

Any hints would be appreciated.  
Question by:softspot
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    Use the standard Outlook automation code found here:
    And simply add in your Texbox reference as the Message Body.

    (I have not tesed this code specifically. MS has about three different versions of it, all similar.
    If you can't get this to work then I will post the code I use.)

    Something like this:

    Sub sbSendMessage(Optional AttachmentPath)
       Dim objOutlook As Outlook.Application
       Dim objOutlookMsg As Outlook.MailItem
       Dim objOutlookRecip As Outlook.Recipient
       Dim objOutlookAttach As Outlook.Attachment

       On Error GoTo ErrorMsgs

       ' Create the Outlook session.
       Set objOutlook = CreateObject("Outlook.Application")
       ' Create the message.
       Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
       With objOutlookMsg
          ' Add the To recipient(s) to the message. Substitute
          ' your names here.
         Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
          objOutlookRecip.Type = olTo
          ' Add the CC recipient(s) to the message.
          Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
          objOutlookRecip.Type = olCC
          ' Set the Subject, Body, and Importance of the message.
          .Subject = "This is an Automation test with Microsoft Outlook"
    .Body = YourTexBoxReference
          .Importance = olImportanceHigh  'High importance
          ' Add attachments to the message.
          If Not IsMissing(AttachmentPath) Then
             Set objOutlookAttach = .Attachments.Add(AttachmentPath)
          End If
          ' Resolve each Recipient's name.
          For Each objOutlookRecip In .Recipients
             If Not objOutlookRecip.Resolve Then
    .........End If
          End If
       End With
       Set objOutlookMsg = Nothing
       Set objOutlook = Nothing
       Set objOutlookRecip = Nothing
       Set objOutlookAttach = Nothing
       If Err.Number = "287" Then
          MsgBox "You clicked No to the Outlook security warning. " & _
          "Rerun the procedure and click Yes to access e-mail" & _
          "addresses to send your message. For more information, & _
          "see the document at" & _
          "/previous/outlook/downloads/security.asp. " "
          Msgbox Err.Number, Err.Description
       End If
    End Sub


    Author Comment

    Thanks for this. Still some quirks not solved. Would appreciate it if you would post your code as you suggest.

    LVL 74

    Accepted Solution

    Here is a fully functioning sample

    Author Closing Comment

    Thanks for your help. This has opened up other issues which I need to think about. Good luck!
    LVL 74

    Expert Comment

    by:Jeffrey Coachman

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    PDF to Word 25 78
    Disk Wipe Utility 6 63
    Best way to accomplish this task? 3 65
    Changing MX record and DNS cache 12 50
    Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
    Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
    This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.
    Viewers will learn how to use the Hootsuite Dashboard.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now