943,660 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 10234
  • VB.NET RSS
Aug 27th, 2008
0

send email using vb.net

Expand Post »
Hi all
in my appliction i want to send the document through mail i.e: outlook
here is my code

Dim myWS As Object
Dim RegKey As String
Dim Key As String
Key = "HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\"
'access Windows scripting
myWS = CreateObject("WScript.Shell")
'read key from registry
RegKey = myWS.RegRead(Key)
If RegKey = "Microsoft Outlook" Then


Dim mailString As String = "mailto:" & "?subject= &body="
Process.Start(mailString)
Dim aFile As String = attachfile
Dim Ret As IntPtr
While Ret = 0
Application.DoEvents()
Ret = FindWindow(vbNullString, " ")
End While
SendKeys.Send("%if" & attachfile & "{ENTER}")
but it is not attaching the file just it opens the outlook
can anybady tell me where im my code goes wrong?
Last edited by Pgmer; Aug 27th, 2008 at 3:36 am.
Similar Threads
Reputation Points: 51
Solved Threads: 112
Practically a Master Poster
Pgmer is offline Offline
668 posts
since Apr 2008
Aug 27th, 2008
0

Re: send email using vb.net

Reputation Points: 6
Solved Threads: 2
Junior Poster
scrypt3r is offline Offline
114 posts
since Aug 2007
Aug 28th, 2008
0

Re: send email using vb.net

hey the code which u have posted works for all ? i mean to ask if i copy this code in my application (VB.NET 2005) will this work ? ie..atleast ms outlook window will open ?
and one more thing, RegKey...from where u got it ? or is it common to all ? am a raw fresher so need help kindly reply....thanks a lot in advance

Click to Expand / Collapse  Quote originally posted by Pgmer ...
Hi all
in my appliction i want to send the document through mail i.e: outlook
here is my code

Dim myWS As Object
Dim RegKey As String
Dim Key As String
Key = "HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\"
'access Windows scripting
myWS = CreateObject("WScript.Shell")
'read key from registry
RegKey = myWS.RegRead(Key)
If RegKey = "Microsoft Outlook" Then


Dim mailString As String = "mailto:" & "?subject= &body="
Process.Start(mailString)
Dim aFile As String = attachfile
Dim Ret As IntPtr
While Ret = 0
Application.DoEvents()
Ret = FindWindow(vbNullString, " ")
End While
SendKeys.Send("%if" & attachfile & "{ENTER}")
but it is not attaching the file just it opens the outlook
can anybady tell me where im my code goes wrong?
Reputation Points: 10
Solved Threads: 2
Junior Poster
laghaterohan is offline Offline
170 posts
since Aug 2008
Aug 28th, 2008
0

Re: send email using vb.net

Hello friend try this in vb.net;

VB.NET Syntax (Toggle Plain Text)
  1. Imports System.Web.mail
  2.  
  3. Public Class Form1
  4. Inherits System.Windows.Forms.Form
  5. ' Variable which will send the mail
  6. Dim obj As System.Web.Mail.SmtpMail
  7.  
  8. 'Variable to store the attachments
  9. Dim Attachment As System.Web.Mail.MailAttachment
  10.  
  11. 'Variable to create the message to send
  12. Dim Mailmsg As New System.Web.Mail.MailMessage
  13.  
  14. Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
  15. 'Show open dialogue box to select the files to attach
  16. Dim Counter As Integer
  17. OFD.CheckFileExists = True
  18. OFD.Title = "Select file(s) to attach"
  19. OFD.ShowDialog()
  20.  
  21. For Counter = 0 To UBound(OFD.FileNames)
  22. lstAttachment.Items.Add(OFD.FileNames(Counter))
  23. Next
  24. End Sub
  25.  
  26. Private Sub BtnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRemove.Click
  27. 'Remove the attachments
  28. If lstAttachment.SelectedIndex > -1 Then
  29. lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
  30. End If
  31. End Sub
  32.  
  33. Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  34. Dim Counter As Integer
  35.  
  36. Try
  37. 'Validate the data
  38. If txtSMTPServer.Text = "" Then
  39. MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information, "Send Email")
  40. Exit Sub
  41. End If
  42.  
  43. If txtFrom.Text = "" Then
  44. MsgBox("Enter the From email address ...!!!", MsgBoxStyle.Information, "Send Email")
  45. Exit Sub
  46. End If
  47.  
  48. If txtTo.Text = "" Then
  49. MsgBox("Enter the Recipient email address ...!!!", MsgBoxStyle.Information, "Send Email")
  50. Exit Sub
  51. End If
  52.  
  53. If txtSubject.Text = "" Then
  54. MsgBox("Enter the Email subject ...!!!", MsgBoxStyle.Information, "Send Email")
  55. Exit Sub
  56. End If
  57.  
  58. 'Set the properties
  59. 'Assign the SMTP server
  60. obj.SmtpServer = txtSMTPServer.Text
  61. 'Multiple recepients can be specified using ; as the delimeter
  62. 'Address of the recipient
  63. Mailmsg.To = txtTo.Text
  64.  
  65.  
  66. 'Your From Address
  67. 'You can also use a custom header Reply-To for a different replyto address
  68. Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"
  69.  
  70.  
  71. 'Specify the body format
  72. If chkFormat.Checked = True Then
  73. Mailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML Format
  74. Else
  75. Mailmsg.BodyFormat = MailFormat.Text
  76. End If
  77.  
  78. 'If you want you can add a reply to header
  79. 'Mailmsg.Headers.Add("Reply-To", "Manoj@geinetech.net")
  80. 'custom headersare added like this
  81. 'Mailmsg.Headers.Add("Manoj", "TestHeader")
  82.  
  83. 'Mail Subject
  84. Mailmsg.Subject = txtSubject.Text
  85.  
  86. 'Attach the files one by one
  87. For Counter = 0 To lstAttachment.Items.Count - 1
  88. Attachment = New MailAttachment(lstAttachment.Items(Counter))
  89. 'Add it to the mail message
  90. Mailmsg.Attachments.Add(Attachment)
  91. Next
  92.  
  93. 'Mail Body
  94. Mailmsg.Body = txtMessage.Text
  95. 'Call the send method to send the mail
  96. obj.Send(Mailmsg)
  97. MsgBox("Send Complete!")
  98. Catch ex As Exception
  99. MsgBox(ex.Message)
  100. End Try
  101.  
  102. End Sub
  103.  
  104. End Class
Reputation Points: 10
Solved Threads: 1
Newbie Poster
johnnykenshien is offline Offline
3 posts
since Aug 2008
Aug 28th, 2008
0

Re: send email using vb.net

thanks... hope it works....

cya,
Rohan
Reputation Points: 10
Solved Threads: 2
Junior Poster
laghaterohan is offline Offline
170 posts
since Aug 2008
Aug 28th, 2008
0

Re: send email using vb.net

Thanks
ur code may work but im interfacing outlook in my appliction. i just want to attach the file with outlook.
Reputation Points: 51
Solved Threads: 112
Practically a Master Poster
Pgmer is offline Offline
668 posts
since Apr 2008
Sep 2nd, 2008
0

Re: send email using vb.net

Hi laghaterohan
sory for late reply.
yes it works for all u need to have outlook istalled iin ur machine
basically im checking for the windows registry key, that method vill give u the default mail client once u get it vill open the outlook and attach the file.
Reputation Points: 51
Solved Threads: 112
Practically a Master Poster
Pgmer is offline Offline
668 posts
since Apr 2008
Sep 2nd, 2008
0

Re: send email using vb.net

Click to Expand / Collapse  Quote originally posted by Pgmer ...
Hi laghaterohan
sory for late reply.
yes it works for all u need to have outlook istalled iin ur machine
basically im checking for the windows registry key, that method vill give u the default mail client once u get it vill open the outlook and attach the file.
okie....so from where can i get windows registy key ? btw, the above code in vb.net is also working properly or not ??? i am aksing u this because so that i can directly use it...'coz i dont want to create mess as my application already has too much of coding.......
clarify
Last edited by laghaterohan; Sep 2nd, 2008 at 7:48 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster
laghaterohan is offline Offline
170 posts
since Aug 2008
Dec 13th, 2008
0

Re: send email using vb.net

Following Coding Run successfully. Any Body want send a mail Via VB.net. You can use this code.


Send Email with Gmail

This sample base on vb.net 2005.

There are two step to create this.

1. You must insert mailSettings tag in app.config for window application or insert in web.config for web application.

2. Use System.Net.Mail namespace.

Sample Code for window application.
1. Insert this tag in app.config (Config Google SMTP).

<system.net >
<mailSettings >
<smtp from ="Test@Test">
<network host ="smtp.gmail.com" port ="587" password ="your Password" userName ="Your Mail Id"/>
</smtp>
</mailSettings>
</system.net>


2. Use System.Net.Mail namespace.


Imports System.Net.Mail




Dim client As New SmtpClient()

Dim sendTo As New MailAddress("sendToAccount@gmail.com")
Dim from As MailAddress = New MailAddress("from@address.com")
Dim message As New MailMessage(from, sendTo)


message.IsBodyHtml = True
message.Subject = "HI"
message.Body = "Type Your Msg.!!"
'multipleattach()
' Use the same account in app.config to authenticate.
System.Net.NetworkCredential("yourAccount@gmail.com", "YourPassword")


client.Host = "smtp.gmail.com"
client.UseDefaultCredentials = False
client.Credentials = basicAuthenticationInfo
client.EnableSsl = True

Try

client.Send(message)
MsgBox("SUCCESS")

Catch ex As Exception

MsgBox("SEND FAIL")
End Try

All The Best
Reputation Points: 8
Solved Threads: 1
Newbie Poster
gomathinayagam is offline Offline
15 posts
since Nov 2008
Dec 13th, 2008
0

Re: send email using vb.net

Yes it wil work
just copy and paste the code and run..
if not works plz let me know.
Reputation Points: 51
Solved Threads: 112
Practically a Master Poster
Pgmer is offline Offline
668 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: How can set windows state
Next Thread in VB.NET Forum Timeline: Web Browser and Media Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC