Use this, it works. Its a whole form, create a new form and replace its code
contents with this.
Option Explicit On
Option Strict On
Option Compare Binary
Imports System
Imports System.Text
Imports System.Web.Mail
Imports System.Windows.Forms
' <remarks>
' Hauptformular der Anwendung.
' </remarks>
Public Class MainForm
Inherits System.Windows.Forms.Form
#Region " Designer Code"
Public Sub New()
MyBase.New()
InitializeComponent()
' Eigener Initialisierungscode.
Me.txtFrom.Text = "fr**@anonymous.com"
Me.txtTo.Text = "to@anonymous.com"
Me.txtSubject.Text = "Enter title here..."
Me.txtBody.Text = "Write your message here..."
Me.cboPriority.SelectedIndex = 1
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents btnSend As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents txtFrom As System.Windows.Forms.TextBox
Friend WithEvents txtTo As System.Windows.Forms.TextBox
Friend WithEvents txtCC As System.Windows.Forms.TextBox
Friend WithEvents txtBCC As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents txtSubject As System.Windows.Forms.TextBox
Friend WithEvents txtBody As System.Windows.Forms.TextBox
Friend WithEvents lstAttachments As System.Windows.Forms.ListBox
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents cboPriority As System.Windows.Forms.ComboBox
Friend WithEvents btnRemoveAttachment As System.Windows.Forms.Button
Friend WithEvents btnAddAttachment As System.Windows.Forms.Button
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents Label8 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnSend = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.txtFrom = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.txtTo = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.txtCC = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.txtBCC = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.txtSubject = New System.Windows.Forms.TextBox
Me.txtBody = New System.Windows.Forms.TextBox
Me.lstAttachments = New System.Windows.Forms.ListBox
Me.Label6 = New System.Windows.Forms.Label
Me.cboPriority = New System.Windows.Forms.ComboBox
Me.btnRemoveAttachment = New System.Windows.Forms.Button
Me.btnAddAttachment = New System.Windows.Forms.Button
Me.Label7 = New System.Windows.Forms.Label
Me.Label8 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'btnSend
'
Me.btnSend.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnSend.Location = New System.Drawing.Point(187, 320)
Me.btnSend.Name = "btnSend"
Me.btnSend.TabIndex = 18
Me.btnSend.Text = "&Send"
'
'Label1
'
Me.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label1.Location = New System.Drawing.Point(16, 18)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "&From:"
'
'txtFrom
'
Me.txtFrom.Location = New System.Drawing.Point(64, 16)
Me.txtFrom.Name = "txtFrom"
Me.txtFrom.Size = New System.Drawing.Size(192, 20)
Me.txtFrom.TabIndex = 1
Me.txtFrom.Text = ""
'
'Label2
'
Me.Label2.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label2.Location = New System.Drawing.Point(16, 42)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(48, 16)
Me.Label2.TabIndex = 2
Me.Label2.Text = "&To:"
'
'txtTo
'
Me.txtTo.Location = New System.Drawing.Point(64, 40)
Me.txtTo.Name = "txtTo"
Me.txtTo.Size = New System.Drawing.Size(192, 20)
Me.txtTo.TabIndex = 3
Me.txtTo.Text = ""
'
'Label3
'
Me.Label3.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label3.Location = New System.Drawing.Point(16, 66)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(48, 16)
Me.Label3.TabIndex = 4
Me.Label3.Text = "&CC:"
'
'txtCC
'
Me.txtCC.Location = New System.Drawing.Point(64, 64)
Me.txtCC.Name = "txtCC"
Me.txtCC.Size = New System.Drawing.Size(192, 20)
Me.txtCC.TabIndex = 5
Me.txtCC.Text = ""
'
'Label4
'
Me.Label4.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label4.Location = New System.Drawing.Point(16, 90)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(48, 16)
Me.Label4.TabIndex = 6
Me.Label4.Text = "&BCC:"
'
'txtBCC
'
Me.txtBCC.Location = New System.Drawing.Point(64, 88)
Me.txtBCC.Name = "txtBCC"
Me.txtBCC.Size = New System.Drawing.Size(192, 20)
Me.txtBCC.TabIndex = 7
Me.txtBCC.Text = ""
'
'Label5
'
Me.Label5.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label5.Location = New System.Drawing.Point(16, 114)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(48, 16)
Me.Label5.TabIndex = 8
Me.Label5.Text = "S&ubject:"
'
'txtSubject
'
Me.txtSubject.Location = New System.Drawing.Point(64, 112)
Me.txtSubject.Name = "txtSubject"
Me.txtSubject.Size = New System.Drawing.Size(192, 20)
Me.txtSubject.TabIndex = 9
Me.txtSubject.Text = ""
'
'txtBody
'
Me.txtBody.Location = New System.Drawing.Point(16, 168)
Me.txtBody.Multiline = True
Me.txtBody.Name = "txtBody"
Me.txtBody.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtBody.Size = New System.Drawing.Size(416, 144)
Me.txtBody.TabIndex = 13
Me.txtBody.Text = ""
'
'lstAttachments
'
Me.lstAttachments.Location = New System.Drawing.Point(264, 32)
Me.lstAttachments.Name = "lstAttachments"
Me.lstAttachments.Size = New System.Drawing.Size(168, 95)
Me.lstAttachments.TabIndex = 15
'
'Label6
'
Me.Label6.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label6.Location = New System.Drawing.Point(264, 16)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(64, 16)
Me.Label6.TabIndex = 14
Me.Label6.Text = "&Attachments:"
'
'cboPriority
'
Me.cboPriority.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboPriority.Items.AddRange(New Object() {"Low", "Normal", "High"})
Me.cboPriority.Location = New System.Drawing.Point(168, 136)
Me.cboPriority.Name = "cboPriority"
Me.cboPriority.Size = New System.Drawing.Size(88, 21)
Me.cboPriority.TabIndex = 11
'
'btnRemoveAttachment
'
Me.btnRemoveAttachment.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnRemoveAttachment.Location = New System.Drawing.Point(352, 134)
Me.btnRemoveAttachment.Name = "btnRemoveAttachment"
Me.btnRemoveAttachment.Size = New System.Drawing.Size(56, 24)
Me.btnRemoveAttachment.TabIndex = 17
Me.btnRemoveAttachment.Text = "&Remove"
'
'btnAddAttachment
'
Me.btnAddAttachment.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnAddAttachment.Location = New System.Drawing.Point(288, 134)
Me.btnAddAttachment.Name = "btnAddAttachment"
Me.btnAddAttachment.Size = New System.Drawing.Size(56, 24)
Me.btnAddAttachment.TabIndex = 16
Me.btnAddAttachment.Text = "A&dd..."
'
'Label7
'
Me.Label7.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label7.Location = New System.Drawing.Point(120, 138)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(48, 16)
Me.Label7.TabIndex = 10
Me.Label7.Text = "&Priotity:"
'
'Label8
'
Me.Label8.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Label8.Location = New System.Drawing.Point(16, 152)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing.Size(48, 16)
Me.Label8.TabIndex = 12
Me.Label8.Text = "&Message:"
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(448, 360)
Me.Controls.Add(Me.Label8)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.btnAddAttachment)
Me.Controls.Add(Me.btnRemoveAttachment)
Me.Controls.Add(Me.cboPriority)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.lstAttachments)
Me.Controls.Add(Me.txtBody)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.txtSubject)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.txtBCC)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.txtCC)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txtTo)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtFrom)
Me.Controls.Add(Me.btnSend)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "MainForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n
Me.Text = "SendMail"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
Dim mailmsg As New MailMessage
With mailmsg
..From = Me.txtFrom.Text.Trim()
..To = Me.txtTo.Text.Trim()
..Cc = Me.txtCC.Text.Trim()
..Bcc = Me.txtBCC.Text.Trim()
..Subject = Me.txtSubject.Text.Trim()
..Body = Me.txtBody.Text.Trim()
Select Case Me.cboPriority.SelectedIndex
Case 0
..Priority = MailPriority.Low
Case 1
..Priority = MailPriority.Normal
Case 2
..Priority = MailPriority.High
End Select
Dim s As String
For Each s In Me.lstAttachments.Items
..Attachments.Add(New MailAttachment(s))
Next s
End With
Try
SmtpMail.Send(mailmsg)
MessageBox.Show("Your mail has been successfully sent!")
Catch ex As Exception
MessageBox.Show("The following problem occurred when attempting to send your
mail: " & ex.Message)
End Try
End Sub
Private Sub btnAddAttachment_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddAttachment.Click
Dim ofdlg As New OpenFileDialog
ofdlg.CheckFileExists = True
ofdlg.CheckPathExists = True
If ofdlg.ShowDialog(Me) = DialogResult.OK Then
Me.lstAttachments.Items.Add(ofdlg.FileName)
End If
End Sub
Private Sub btnRemoveAttachment_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnRemoveAttachment.Click
If Me.lstAttachments.SelectedIndex >= 0 Then
Me.lstAttachments.Items.Remove(Me.lstAttachments.S electedItem)
End If
End Sub
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
End Class
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
Time flies when you don't know what you're doing
"Rob" <rw*********@rtt.uk.com> wrote in message
news:38**************************@posting.google.c om...
Hi all,
I am having trouble converting the code below (found on
http://vbnet.mvps.org/index.html?cod...endmessage.htm) into a
format that will work using vb .NET.
Can anyone have a look at it and let me know what I need to change. I
have tried changing the "hwnd" type into intptr's but there seem to be
other problems too, like it won't allow "lParam As Any" to be
declared.
Many thanks,
Rob
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2004 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const WM_SETTEXT = &HC
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function SetWindowText Lib "user32" _
Alias "SetWindowTextA" _
(ByVal hwnd As Long, ByVal _
lpString As String) As Long
Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Sub Command1_Click()
Dim hWndApp As Long
'call the wrapper function to start shell,
'and return the handle to the shelled app.
hWndApp = StartShell("notepad.exe")
'if found, prove it!
If hWndApp Then
'change its caption
Call SetWindowText(hWndApp, "The handle to Notepad is " &
CStr(hWndApp))
'bring it to the top
Call BringWindowToTop(hWndApp)
'and add some text to its edit window
Call PutTextIntoNotepad(hWndApp)
End If
End Sub
Private Function GethWndFromProcessID(hProcessIDToFind As Long) As
Long
Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProcessID As Long
On Local Error GoTo GethWndFromProcessID_Error
'get the handle to the desktop
hWndDesktop = GetDesktopWindow()
'get the first child under the desktop
hWndChild = GetWindow(hWndDesktop, GW_CHILD)
'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0
'get the ThreadProcessID of the window
Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)
'if it matches the target, exit returning that value
If hWndChildProcessID = hProcessIDToFind Then
GethWndFromProcessID = hWndChild
Exit Do
End If
'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
Loop
Exit Function
GethWndFromProcessID_Error:
GethWndFromProcessID = 0
Exit Function
End Function
Private Function StartShell(sApplication As String) As Long
Dim hProcessID As Long
'start using shell, and get the process ID
hProcessID = Shell(sApplication, vbNormalFocus)
'return the hwnd to the shelled process
StartShell = GethWndFromProcessID(hProcessID)
End Function
Private Sub PutTextIntoNotepad(hWndApp As Long)
'This adds text to notepad, by locating its
''Edit' class window. This is similar to the
'method used to locate the hwnd itself, but
'here we know the parent (hWndApp) so only
'have to search its child windows.
Dim hWndChild As Long
Dim sMsg As String
Dim sBuffer As String * 32
Dim nSize As Long
'this string is split only to fit the browser window
sMsg = "This method demonstrates using Shell() "
sMsg = sMsg & "to launch notepad, obtain its hwnd using "
sMsg = sMsg & "GetWindowThreadProcessId, and then use that"
sMsg = sMsg & "hwnd to change its caption and place text into "
sMsg = sMsg & "its Edit window using SetWindowText and
SendMessage."
'get the first child window in Notepad
hWndChild = GetWindow(hWndApp, GW_CHILD)
'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0
'get the Class Name of the window
nSize = GetClassName(hWndChild, sBuffer, 32)
'if nSize > 0, it contains the length
'of the class name retrieved
If nSize Then
'if the class name is "Edit",
'set some text and exit
If Left$(sBuffer, nSize) = "Edit" Then
Call SendMessage(hWndChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub
End If
End If
'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
Loop
End Sub