Mail Message in VB.net

How to send a mail message in vb.net

Page 1 of 1

7 Replies - 25325 Views - Last Post: 12 July 2011 - 05:57 AM Rate Topic: -----

#1 biggerB  Icon User is offline

  • If at first you donít succeed, call it version 1.0
  • member icon

Reputation: 80
  • View blog
  • Posts: 760
  • Joined: 11-June 10

Mail Message in VB.net

Posted 20 June 2010 - 04:47 AM

Hello..
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..
Is This A Good Question/Topic? 0
  • +

Replies To: Mail Message in VB.net

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Mail Message in VB.net

Posted 20 June 2010 - 05:52 AM

Yes it is possible, have you got any code you're working on to do this so that we can help you with it?
Was This Post Helpful? 1
  • +
  • -

#3 biggerB  Icon User is offline

  • If at first you donít succeed, call it version 1.0
  • member icon

Reputation: 80
  • View blog
  • Posts: 760
  • Joined: 11-June 10

Re: Mail Message in VB.net

Posted 20 June 2010 - 06:52 AM

View PostCharlieMay, on 20 June 2010 - 04:52 AM, said:

Yes it is possible, have you got any code you're working on to do this so that we can help you with it?


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

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Mail Message in VB.net

Posted 20 June 2010 - 08:57 AM

Here is a method I've used, there was a lot of issues with your code so I'm just going to list the entire solution
    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

Was This Post Helpful? 2
  • +
  • -

#5 biggerB  Icon User is offline

  • If at first you donít succeed, call it version 1.0
  • member icon

Reputation: 80
  • View blog
  • Posts: 760
  • Joined: 11-June 10

Re: Mail Message in VB.net

Posted 20 June 2010 - 10:54 AM

View PostCharlieMay, on 20 June 2010 - 07:57 AM, said:

Here is a method I've used, there was a lot of issues with your code so I'm just going to list the entire solution


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.
Was This Post Helpful? 0
  • +
  • -

#6 shin-lithium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-July 11

Re: Mail Message in VB.net

Posted 12 July 2011 - 04:53 AM

hi guys
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
Was This Post Helpful? 0
  • +
  • -

#7 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,507
  • Joined: 16-March 11

Re: Mail Message in VB.net

Posted 12 July 2011 - 05:46 AM

View Postshin-lithium, on 12 July 2011 - 04:53 AM, said:

hi guys
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?
Was This Post Helpful? 1
  • +
  • -

#8 hlx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 135
  • Joined: 13-November 10

Re: Mail Message in VB.net

Posted 12 July 2011 - 05:57 AM

Also, if you copied what was there verbatim, and replaced your entire Form1 code, that would by why you are getting that error.

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1