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()
InitializeCompo nent()
' Eigener Initialisierung scode.
Me.txtFrom.Text = "fr**@anonymous .com"
Me.txtTo.Text = "to@anonymous.c om"
Me.txtSubject.T ext = "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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
Private components As System.Componen tModel.IContain er
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 btnRemoveAttach ment As System.Windows. Forms.Button
Friend WithEvents btnAddAttachmen t As System.Windows. Forms.Button
Friend WithEvents Label7 As System.Windows. Forms.Label
Friend WithEvents Label8 As System.Windows. Forms.Label
<System.Diagnos tics.DebuggerSt epThrough()> _
Private Sub InitializeCompo nent()
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.lstAttachmen ts = New System.Windows. Forms.ListBox
Me.Label6 = New System.Windows. Forms.Label
Me.cboPriority = New System.Windows. Forms.ComboBox
Me.btnRemoveAtt achment = New System.Windows. Forms.Button
Me.btnAddAttach ment = New System.Windows. Forms.Button
Me.Label7 = New System.Windows. Forms.Label
Me.Label8 = New System.Windows. Forms.Label
Me.SuspendLayou t()
'
'btnSend
'
Me.btnSend.Flat Style = System.Windows. Forms.FlatStyle .System
Me.btnSend.Loca tion = New System.Drawing. Point(187, 320)
Me.btnSend.Name = "btnSend"
Me.btnSend.TabI ndex = 18
Me.btnSend.Text = "&Send"
'
'Label1
'
Me.Label1.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label1.Locat ion = New System.Drawing. Point(16, 18)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing. Size(48, 16)
Me.Label1.TabIn dex = 0
Me.Label1.Text = "&From:"
'
'txtFrom
'
Me.txtFrom.Loca tion = New System.Drawing. Point(64, 16)
Me.txtFrom.Name = "txtFrom"
Me.txtFrom.Size = New System.Drawing. Size(192, 20)
Me.txtFrom.TabI ndex = 1
Me.txtFrom.Text = ""
'
'Label2
'
Me.Label2.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label2.Locat ion = New System.Drawing. Point(16, 42)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing. Size(48, 16)
Me.Label2.TabIn dex = 2
Me.Label2.Text = "&To:"
'
'txtTo
'
Me.txtTo.Locati on = New System.Drawing. Point(64, 40)
Me.txtTo.Name = "txtTo"
Me.txtTo.Size = New System.Drawing. Size(192, 20)
Me.txtTo.TabInd ex = 3
Me.txtTo.Text = ""
'
'Label3
'
Me.Label3.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label3.Locat ion = New System.Drawing. Point(16, 66)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing. Size(48, 16)
Me.Label3.TabIn dex = 4
Me.Label3.Text = "&CC:"
'
'txtCC
'
Me.txtCC.Locati on = New System.Drawing. Point(64, 64)
Me.txtCC.Name = "txtCC"
Me.txtCC.Size = New System.Drawing. Size(192, 20)
Me.txtCC.TabInd ex = 5
Me.txtCC.Text = ""
'
'Label4
'
Me.Label4.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label4.Locat ion = New System.Drawing. Point(16, 90)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing. Size(48, 16)
Me.Label4.TabIn dex = 6
Me.Label4.Text = "&BCC:"
'
'txtBCC
'
Me.txtBCC.Locat ion = New System.Drawing. Point(64, 88)
Me.txtBCC.Name = "txtBCC"
Me.txtBCC.Size = New System.Drawing. Size(192, 20)
Me.txtBCC.TabIn dex = 7
Me.txtBCC.Text = ""
'
'Label5
'
Me.Label5.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label5.Locat ion = New System.Drawing. Point(16, 114)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing. Size(48, 16)
Me.Label5.TabIn dex = 8
Me.Label5.Text = "S&ubject:"
'
'txtSubject
'
Me.txtSubject.L ocation = New System.Drawing. Point(64, 112)
Me.txtSubject.N ame = "txtSubject "
Me.txtSubject.S ize = New System.Drawing. Size(192, 20)
Me.txtSubject.T abIndex = 9
Me.txtSubject.T ext = ""
'
'txtBody
'
Me.txtBody.Loca tion = New System.Drawing. Point(16, 168)
Me.txtBody.Mult iline = True
Me.txtBody.Name = "txtBody"
Me.txtBody.Scro llBars = System.Windows. Forms.ScrollBar s.Both
Me.txtBody.Size = New System.Drawing. Size(416, 144)
Me.txtBody.TabI ndex = 13
Me.txtBody.Text = ""
'
'lstAttachments
'
Me.lstAttachmen ts.Location = New System.Drawing. Point(264, 32)
Me.lstAttachmen ts.Name = "lstAttachments "
Me.lstAttachmen ts.Size = New System.Drawing. Size(168, 95)
Me.lstAttachmen ts.TabIndex = 15
'
'Label6
'
Me.Label6.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label6.Locat ion = New System.Drawing. Point(264, 16)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing. Size(64, 16)
Me.Label6.TabIn dex = 14
Me.Label6.Text = "&Attachmen ts:"
'
'cboPriority
'
Me.cboPriority. DropDownStyle =
System.Windows. Forms.ComboBoxS tyle.DropDownLi st
Me.cboPriority. Items.AddRange( New Object() {"Low", "Normal", "High"})
Me.cboPriority. Location = New System.Drawing. Point(168, 136)
Me.cboPriority. Name = "cboPriorit y"
Me.cboPriority. Size = New System.Drawing. Size(88, 21)
Me.cboPriority. TabIndex = 11
'
'btnRemoveAttac hment
'
Me.btnRemoveAtt achment.FlatSty le = System.Windows. Forms.FlatStyle .System
Me.btnRemoveAtt achment.Locatio n = New System.Drawing. Point(352, 134)
Me.btnRemoveAtt achment.Name = "btnRemoveAttac hment"
Me.btnRemoveAtt achment.Size = New System.Drawing. Size(56, 24)
Me.btnRemoveAtt achment.TabInde x = 17
Me.btnRemoveAtt achment.Text = "&Remove"
'
'btnAddAttachme nt
'
Me.btnAddAttach ment.FlatStyle = System.Windows. Forms.FlatStyle .System
Me.btnAddAttach ment.Location = New System.Drawing. Point(288, 134)
Me.btnAddAttach ment.Name = "btnAddAttachme nt"
Me.btnAddAttach ment.Size = New System.Drawing. Size(56, 24)
Me.btnAddAttach ment.TabIndex = 16
Me.btnAddAttach ment.Text = "A&dd..."
'
'Label7
'
Me.Label7.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label7.Locat ion = New System.Drawing. Point(120, 138)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing. Size(48, 16)
Me.Label7.TabIn dex = 10
Me.Label7.Text = "&Priotity: "
'
'Label8
'
Me.Label8.FlatS tyle = System.Windows. Forms.FlatStyle .System
Me.Label8.Locat ion = New System.Drawing. Point(16, 152)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing. Size(48, 16)
Me.Label8.TabIn dex = 12
Me.Label8.Text = "&Message:"
'
'MainForm
'
Me.AutoScaleBas eSize = 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.btnAddAttac hment)
Me.Controls.Add (Me.btnRemoveAt tachment)
Me.Controls.Add (Me.cboPriority )
Me.Controls.Add (Me.Label6)
Me.Controls.Add (Me.lstAttachme nts)
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.FormBorderSt yle = System.Windows. Forms.FormBorde rStyle.FixedSin gle
Me.MaximizeBox = False
Me.Name = "MainForm"
Me.StartPositio n = System.Windows. Forms.FormStart Position.Center Screen
Me.Text = "SendMail"
Me.ResumeLayout (False)
End Sub
#End Region
Private Sub btnSend_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnSend.Click
Dim mailmsg As New MailMessage
With mailmsg
..From = Me.txtFrom.Text .Trim()
..To = Me.txtTo.Text.T rim()
..Cc = Me.txtCC.Text.T rim()
..Bcc = Me.txtBCC.Text. Trim()
..Subject = Me.txtSubject.T ext.Trim()
..Body = Me.txtBody.Text .Trim()
Select Case Me.cboPriority. SelectedIndex
Case 0
..Priority = MailPriority.Lo w
Case 1
..Priority = MailPriority.No rmal
Case 2
..Priority = MailPriority.Hi gh
End Select
Dim s As String
For Each s In Me.lstAttachmen ts.Items
..Attachments.A dd(New MailAttachment( s))
Next s
End With
Try
SmtpMail.Send(m ailmsg)
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 btnAddAttachmen t_Click(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnAddAttachmen t.Click
Dim ofdlg As New OpenFileDialog
ofdlg.CheckFile Exists = True
ofdlg.CheckPath Exists = True
If ofdlg.ShowDialo g(Me) = DialogResult.OK Then
Me.lstAttachmen ts.Items.Add(of dlg.FileName)
End If
End Sub
Private Sub btnRemoveAttach ment_Click(ByVa l sender As System.Object, ByVal e
As System.EventArg s) Handles btnRemoveAttach ment.Click
If Me.lstAttachmen ts.SelectedInde x >= 0 Then
Me.lstAttachmen ts.Items.Remove (Me.lstAttachme nts.SelectedIte m)
End If
End Sub
Private Sub MainForm_Load(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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*********@rt t.uk.com> wrote in message
news:38******** *************** ***@posting.goo gle.com...
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 GetDesktopWindo w Lib "user32" () As Long
Private Declare Function GetWindowThread ProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function BringWindowToTo p 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 "GetClassNa meA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessag eA" _
(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("not epad.exe")
'if found, prove it!
If hWndApp Then
'change its caption
Call SetWindowText(h WndApp, "The handle to Notepad is " &
CStr(hWndApp))
'bring it to the top
Call BringWindowToTo p(hWndApp)
'and add some text to its edit window
Call PutTextIntoNote pad(hWndApp)
End If
End Sub
Private Function GethWndFromProc essID(hProcessI DToFind As Long) As
Long
Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProces sID As Long
On Local Error GoTo GethWndFromProc essID_Error
'get the handle to the desktop
hWndDesktop = GetDesktopWindo w()
'get the first child under the desktop
hWndChild = GetWindow(hWndD esktop, GW_CHILD)
'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0
'get the ThreadProcessID of the window
Call GetWindowThread ProcessId(hWndC hild, hWndChildProces sID)
'if it matches the target, exit returning that value
If hWndChildProces sID = hProcessIDToFin d Then
GethWndFromProc essID = hWndChild
Exit Do
End If
'not found, so get the next hwnd
hWndChild = GetWindow(hWndC hild, GW_HWNDNEXT)
Loop
Exit Function
GethWndFromProc essID_Error:
GethWndFromProc essID = 0
Exit Function
End Function
Private Function StartShell(sApp lication As String) As Long
Dim hProcessID As Long
'start using shell, and get the process ID
hProcessID = Shell(sApplicat ion, vbNormalFocus)
'return the hwnd to the shelled process
StartShell = GethWndFromProc essID(hProcessI D)
End Function
Private Sub PutTextIntoNote pad(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 & "GetWindowThrea dProcessId, 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(hWndA pp, 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(hW ndChild, 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(hWn dChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub
End If
End If
'not found, so get the next hwnd
hWndChild = GetWindow(hWndC hild, GW_HWNDNEXT)
Loop
End Sub