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
Medium Priority
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 System.net.mail?

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

Expert Comment

by:Jeffrey Coachman
ID: 24247605
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 http://www.microsoft.com/office" & _
      "/previous/outlook/downloads/security.asp. " "
      Msgbox Err.Number, Err.Description
   End If
End Sub


Author Comment

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

LVL 74

Accepted Solution

Jeffrey Coachman earned 2000 total points
ID: 24299894
Here is a fully functioning sample

Author Closing Comment

ID: 31574498
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
ID: 24350540

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
While Plesk offers many potential benefits to website administrators, including compatibility with Windows Server and other leading technologies, the company has also been working to differentiate it from other control panels for content management…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

864 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