Mail Message in VB.netHow to send a mail message in vb.net
Page 1 of 1
7 Replies - 12550 Views - Last Post: 12 July 2011 - 05:57 AM
#1
Mail Message in VB.net
Posted 20 June 2010 - 04:47 AM
I am making a program that would allow the user to send me feedback directly from the program I have a user's Experience log. I need a way for the user to send me a mail without him knowing my email address and also attaching the user's experience log (a text file) as an attachment in the mail sent to me. Is there a way to send this email to me with the attachment. I am using vb.net 2010 on framework 3.5. Thanks..
Replies To: Mail Message in VB.net
#2
Re: Mail Message in VB.net
Posted 20 June 2010 - 05:52 AM
#3
Re: Mail Message in VB.net
Posted 20 June 2010 - 06:52 AM
CharlieMay, on 20 June 2010 - 04:52 AM, said:
Yes Here is the button code
Private Sub Button_Send_click(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles SendButton.click
Timer1.Stop()
' the following code is the way i am trying to send the mail. I found it on a website but it does not work
Dim From As String = "From@email.com"
Dim sendTo As String = "To@email.com"
Dim Subject As String = "Subject"
Dim Body As String = keylog ' This is a variable of the input data from the user in a textbox
Dim AttachmentFile As String = CurDir() & "usrlog.txt" ' This is the attachment
Dim CC As String = ""
Dim BCC As String = ""
Dim SMTPServer As String = "smtp.mail.google.com" ' Using Google
Dim myMessage As MailMessage
Try
myMessage = New MailMessage()
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""
If My.Computer.FileSystem.FileExists(AttachmentFile) Then _
.Attachments.Add(AttachmentFile)
End With
If SMTPServer <> "" Then _
SmtpMail.SmtpServer = SMTPServer
SmtpMail.Send(myMessage)
Catch myexp As Exception
Throw myexp
End Try
End Sub
I think this is an old code and it does not work with framework 3.5.
Any help is appreciated.
Thanks
This post has been edited by biggerB: 20 June 2010 - 06:54 AM
#4
Re: Mail Message in VB.net
Posted 20 June 2010 - 08:57 AM
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential("your email address", "your password")
SmtpServer.Host = "smtp.gmail.com" 'so what goes here?'
SmtpServer.Port = 587 'or apparently you can use 587, do i actually have to define the port, and would the port depend on the host?'
SmtpServer.EnableSsl = True
mail.From = New MailAddress("sender_email@hotmail.com")
mail.To.Add("recipient@hotmail.com")
mail.Subject = "Test Message"
mail.Body = "This is for testing SMTP mail."
'here is where we are adding the attachment.
Dim attachment As New Attachment("D:\employee.txt") 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpServer.Send(mail)
MsgBox("Sent.")
End Sub
You will have to import System.Net.Mail for this to work
"Your Email Address" and "Your Password" will need to be changed to your gmail account's user id and password
The recipient email address is where you put where you want it sent to.
basically the two attachment lines are all you need to add an attachment to the email, just set the "D:\employee.txt" to what you want to send.
Of course, the subject and body text can be changed to whatever you want your email to say when it arrives.
I should also add that I would suggest you create a setup form so that the user can enter their login information for their smtp server so that you can use that information to send the mail from their machine. This way, no one can use software to scan your executable and retrieve your email address and password.
This post has been edited by CharlieMay: 20 June 2010 - 09:01 AM
#5
Re: Mail Message in VB.net
Posted 20 June 2010 - 10:54 AM
CharlieMay, on 20 June 2010 - 07:57 AM, said:
Thankyou Alot it works perfectly and i took your suggestion and added a form for the user to fill before sending.
Thanks hope i could be of some help to you someday.
#6
Re: Mail Message in VB.net
Posted 12 July 2011 - 04:53 AM
im having huge problems with this particular code. I am sort of a noob and using
Visual basic express 2010 on framework 4.0 i think.
This is my problem when i copy and paste this code onto vb 2010 express
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()SmtpServer.Credentials = New Net.NetworkCredential("your email address", "your password")
SmtpServer.Host = "smtp.gmail.com" 'so what goes here?'
SmtpServer.Port = 587 'or apparently you can use 587, do i actually have to define the port, and would the port depend on the host?'
SmtpServer.EnableSsl = True
mail.From = New MailAddress("sender_email@hotmail.com")
mail.To.Add("recipient@hotmail.com")
mail.Subject = "Test Message"
: mail.Body = "This is for testing SMTP mail."
'here is where we are adding the attachment.
Dim attachment As New Attachment("D:\employee.txt") 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpServer.Send(mail)
MsgBox("Sent.")
End Sub
i get an error in line 1 saying that
statement is not a valid namespace
can i anyone help me out?
thanks
#7
Re: Mail Message in VB.net
Posted 12 July 2011 - 05:46 AM
shin-lithium, on 12 July 2011 - 04:53 AM, said:
im having huge problems with this particular code. I am sort of a noob and using
Visual basic express 2010 on framework 4.0 i think.
This is my problem when i copy and paste this code onto vb 2010 express
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()SmtpServer.Credentials = New Net.NetworkCredential("your email address", "your password")
SmtpServer.Host = "smtp.gmail.com" 'so what goes here?'
SmtpServer.Port = 587 'or apparently you can use 587, do i actually have to define the port, and would the port depend on the host?'
SmtpServer.EnableSsl = True
mail.From = New MailAddress("sender_email@hotmail.com")
mail.To.Add("recipient@hotmail.com")
mail.Subject = "Test Message"
: mail.Body = "This is for testing SMTP mail."
'here is where we are adding the attachment.
Dim attachment As New Attachment("D:\employee.txt") 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpServer.Send(mail)
MsgBox("Sent.")
End Sub
i get an error in line 1 saying that
statement is not a valid namespace
can i anyone help me out?
thanks
Did you import the correct namespace before trying to run the code?
#8
Re: Mail Message in VB.net
Posted 12 July 2011 - 05:57 AM
The correct way is:
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential("your email address", "your password")
SmtpServer.Host = "smtp.gmail.com" 'so what goes here?'
SmtpServer.Port = 587 'or apparently you can use 587, do i actually have to define the port, and would the port depend on the host?'
SmtpServer.EnableSsl = True
mail.From = New MailAddress("sender_email@hotmail.com")
mail.To.Add("recipient@hotmail.com")
mail.Subject = "Test Message"
mail.Body = "This is for testing SMTP mail."
'here is where we are adding the attachment.
Dim attachment As New Attachment("D:\employee.txt") 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpServer.Send(mail)
MsgBox("Sent.")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Also note, that you need to add a button to your form. let it be named it's default.
|
|

New Topic/Question
Reply




MultiQuote





|