locked
Email in vb RRS feed

  • Question

  • I am trying to make something where you click a button and it sends an email to my email address saying something specific.

    Please help me.

    I don't care who the email is from

    Friday, August 12, 2016 12:40 PM

Answers

  •  If you are using Yahoo,  Gmail,  or another service which is not setup on your computer like OutLook would be,  then you can try the below example.  It is just a simple example to send just plain text with no attachments.  You can however set it up to send the message in an HTML format or even add files as attachments if wanted.

     You can use your email address in the From and To, to send an email to yourself if you need to.

    Imports System.Net.Mail
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Try
                Using e_mail As New MailMessage()
                    e_mail.From = New MailAddress("YourEmailAddress@Something.com")
                    e_mail.To.Add(New MailAddress("EmailAddressOfOtherPerson@Something.com"))
                    e_mail.Subject = "Some Subject..."
                    e_mail.IsBodyHtml = False
                    e_mail.Body = "Here is the message body to send to the person"
    
                    '            |------HOST-------|--PORT--|
                    'Yahoo   ->  smtp.mail.yahoo.com   587
                    'GMail   ->  smtp.gmail.com        587
                    'HotMail ->  smtp.live.com         587
    
                    Using Smtp_Server As New SmtpClient("smtp.mail.yahoo.com", 587) 'i am using Yahoo mail
                        Smtp_Server.UseDefaultCredentials = False
                        Smtp_Server.Credentials = New Net.NetworkCredential("YourEmailAddress@Something.com", "YourEmailPassword")
                        Smtp_Server.EnableSsl = True
                        Smtp_Server.Send(e_mail)
                    End Using
    
                    MessageBox.Show("Mail Sent")
                End Using
            Catch error_t As Exception
                MessageBox.Show(error_t.ToString)
            End Try
        End Sub
    
    End Class


    If you say it can`t be done then i`ll try it

    Sunday, August 14, 2016 12:29 PM

All replies

  • I am trying to make something where you click a button and it sends an email to my email address saying something specific.

    Please help me.

    I don't care who the email is from

    Hi

    There are several ways to do this, but here is one that I have used successfully in the past. Did you know that this forum has a really good search capability?

    Dim [to] As String = "recipient email address"
            Dim cc As String = Nothing
            Dim subject As String = "subject string"
            Dim body As String = "message body text"
            Process.Start(String.Format("mailto:{0}?cc={1}&subject={2}&body={3}", [to], cc, subject, body))


    Regards Les, Livingston, Scotland

    Friday, August 12, 2016 12:51 PM
  • It isn't clear what e-mail transport you are using, but one option is to use System.Web.Mail:

    How to send e-mail programmatically with System.Web.Mail and Visual Basic .NET


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, August 12, 2016 12:53 PM
  • Here is Full Working & Very Easy Solution.

    1st Read Complete & Understand & Then Copy Code as per your requirement.

    'Create a Visual basic application And drop following Controls and set the properties accordingly
    
    Control			Property
    
    
    Label			Text  : Smtp Server
    
    TextBox			Name : txtSMTPServer
    
    Label			Text  : From 
    
    TextBox			Name : txtFrom
    
    Label			Text : From Display Name
    
    TextBox			Name : txtFromDisplayName
    
    Label			Text :  Recipient 
    
    TextBox			txtTo
    
    Label			Text : Attachment
    
    ListBox			Name : lstAttachment
    
    Label			Text : Subject
    
    TextBox			Name : txtSubject
    
    Label			Text : Message
    
    TextBox			Name : txtMessage
    			Multiline : True
    			Scrollbars : Both
    
    Button			Text : Add attachment
    			Name : BtnAdd
    
    
    Button			Text : Remove attachment
    			Name : btnRemove
    
    
    Button			Text : Send
    			Name : btnSend
    
    
    CheckBox		Text: Send As HTML
    			Name : chkFormat
    
    
    OpenFileDialog		Name : OFD
    			DefaultExt : *.*
    			InitialDirectory : c:\
    			Multiselect : true
    
    				
    
    'Now let us see the coding part
    
    
    'Open the Code widow and type the following statement above the Class declaration
    Imports System.Web.Mail
    'Within the Class declaration, in the general section declare variables required for this project
    
    ' Variable which will send the mail
    Dim obj As System.Web.Mail.SmtpMail
    
    'Variable to store the attachments 
    Dim Attachment As System.Web.Mail.MailAttachment 
    
    'Variable to create the message to send
    Dim Mailmsg As New System.Web.Mail.MailMessage()    
    
    
    Double click on the addattachment button to add the code
    
    'Type the following lines
    
    'Show open dialogue box to select the files to attach
    Dim Counter As Integer
    OFD.CheckFileExists = True
    OFD.Title = "Select file(s) to attach"
    OFD.ShowDialog()
    
    For Counter = 0 To UBound(OFD.FileNames)
    lstAttachment.Items.Add(OFD.FileNames(Counter))
    Next
    
    'Double Click on the Removeattachment button 
    
    'Type the following lines
    
    'Remove the attachments
    If lstAttachment.SelectedIndex > -1 Then
    lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
    End If
    
    
    
    'Double Click on the Send button 
    
    'Type the following lines
    
    
    Dim Counter As Integer
    
    'Validate the data
    If txtSMTPServer.Text = "" Then
    MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information,  "Send Email")
    Exit Sub
    End If
    
    If txtFrom.Text = "" Then
                MsgBox("Enter the From email address ...!!!", MsgBoxStyle.Information, "Send Email")
    Exit Sub
    End If
    
    If txtTo.Text = "" Then
    MsgBox("Enter the Recipient email address ...!!!", MsgBoxStyle.Information, "Send Email")
    Exit Sub
    End If
    
    If txtSubject.Text = "" Then
    MsgBox("Enter the Email subject ...!!!", MsgBoxStyle.Information, "Send Email")
    Exit Sub
    End If
    
    'Set the properties
    ‘Assign the SMTP server
    obj.SmtpServer = txtSMTPServer.Text
    'Multiple recepients can be specified using ; as the delimeter
    ‘Address of the recipient
    Mailmsg.To = txtTo.Text
    
    
    ‘Your From Address
    ‘You can also use a custom header Reply-To for a different replyto address
    Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"
    
      
    'Specify the body format
    If chkFormat.Checked = True Then
    Mailmsg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
    Else
    Mailmsg.BodyFormat = MailFormat.Text
    End If
    
    'If you want you can add a reply to header 
    'Mailmsg.Headers.Add("Reply-To", "Enter Here your Email Address")
    'custom headersare added like this
    'Mailmsg.Headers.Add("Your Name", "TestHeader")
    
    ‘Mail Subject
    Mailmsg.Subject = txtSubject.Text
    
    ‘Attach the files one by one
    For Counter = 0 To lstAttachment.Items.Count - 1
    Attachment = New MailAttachment(lstAttachment.Items(Counter))
    ‘Add it to the mail message
                Mailmsg.Attachments.Add(Attachment)
    Next
    
    ‘Mail Body
    Mailmsg.Body = txtMessage.Text
    ‘Call the send method to send the mail
    obj.Send(Mailmsg)
    
    'This application is now ready to run , try it.

    Friday, August 12, 2016 8:25 PM
  • This is what I am using. Why isn't it working?

    Public Class Schedule
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            email address"
            Dim cc As String = Nothing
            Dim subject As String = "Bookind"
            Dim body As String = Textbox1.Text + " Has booked in with you for next monday"
            Process.Start(String.Format("mailto:{0}?cc={1}&subject={2}&body={3}", [to], cc, subject, body))
        End Sub
    End Class
    End Sub
    

    Friday, August 12, 2016 11:46 PM
  • This is what I am using. Why isn't it working?

    Public Class Schedule
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            email address"
            Dim cc As String = Nothing
            Dim subject As String = "Bookind"
            Dim body As String = Textbox1.Text + " Has booked in with you for next monday"
            Process.Start(String.Format("mailto:{0}?cc={1}&subject={2}&body={3}", [to], cc, subject, body))
        End Sub
    End Class
    End Sub


    This method will require that you have e-mail client software installed and configured. Do you?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Saturday, August 13, 2016 12:01 AM
  • I don't

    Do you mean like gmail?

    Saturday, August 13, 2016 11:15 AM
  • This is what I am using. Why isn't it working?

    Public Class Schedule
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            email address"
            Dim cc As String = Nothing
            Dim subject As String = "Bookind"
            Dim body As String = Textbox1.Text + " Has booked in with you for next monday"
            Process.Start(String.Format("mailto:{0}?cc={1}&subject={2}&body={3}", [to], cc, subject, body))
        End Sub
    End Class
    End Sub

    Hi

    Looks like you are trying out the code I posted. However, you haven't included the

    Dim [to] As String = "recipient email address"

    If the code is complete, it will open your default email client (in my case, I have Mozilla ThunderBird configured as my default), you need the variables to be set to the correct data - such as the variable '[to]' to have a valid recipient email address, 'subject' as a string for the subject field and 'body' set to the required email body text.

      Dim [to] As String = "recipient email address"
      Dim cc As String = Nothing
      Dim subject As String = "subject string"
      Dim body As String = "message body text"
      Process.Start(String.Format("mailto:{0}?cc={1}&subject={2}&body={3}", [to], cc, subject, body))


    Regards Les, Livingston, Scotland

    Saturday, August 13, 2016 11:30 AM
  • I don't

    Do you mean like gmail?


    No, I mean Outlook or another e-mail app that is installed on your computer. This method uses the app that is associated with mailto.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Saturday, August 13, 2016 12:11 PM
  • It just opens a new tab on my web browser. I didn't get the email either
    Sunday, August 14, 2016 11:22 AM
  • It just opens a new tab on my web browser. I didn't get the email either

    Hi

    Please post the code you used for that.What email client do you use normally (ie what is your default email client)?


    Regards Les, Livingston, Scotland

    Sunday, August 14, 2016 11:49 AM
  •  If you are using Yahoo,  Gmail,  or another service which is not setup on your computer like OutLook would be,  then you can try the below example.  It is just a simple example to send just plain text with no attachments.  You can however set it up to send the message in an HTML format or even add files as attachments if wanted.

     You can use your email address in the From and To, to send an email to yourself if you need to.

    Imports System.Net.Mail
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Try
                Using e_mail As New MailMessage()
                    e_mail.From = New MailAddress("YourEmailAddress@Something.com")
                    e_mail.To.Add(New MailAddress("EmailAddressOfOtherPerson@Something.com"))
                    e_mail.Subject = "Some Subject..."
                    e_mail.IsBodyHtml = False
                    e_mail.Body = "Here is the message body to send to the person"
    
                    '            |------HOST-------|--PORT--|
                    'Yahoo   ->  smtp.mail.yahoo.com   587
                    'GMail   ->  smtp.gmail.com        587
                    'HotMail ->  smtp.live.com         587
    
                    Using Smtp_Server As New SmtpClient("smtp.mail.yahoo.com", 587) 'i am using Yahoo mail
                        Smtp_Server.UseDefaultCredentials = False
                        Smtp_Server.Credentials = New Net.NetworkCredential("YourEmailAddress@Something.com", "YourEmailPassword")
                        Smtp_Server.EnableSsl = True
                        Smtp_Server.Send(e_mail)
                    End Using
    
                    MessageBox.Show("Mail Sent")
                End Using
            Catch error_t As Exception
                MessageBox.Show(error_t.ToString)
            End Try
        End Sub
    
    End Class


    If you say it can`t be done then i`ll try it

    Sunday, August 14, 2016 12:29 PM
  • Razerz,

    It works with my server too. That's something I have been wanting to try. This example sends the email.

    I also ran Leshay's example which opens an email doc in the Outlook editor on my system.

    Sunday, August 14, 2016 8:18 PM
  • Razerz,

    It works with my server too. That's something I have been wanting to try. This example sends the email.

    I also ran Leshay's example which opens an email doc in the Outlook editor on my system.

    Hi

    Yes, in that particular application where I grabbed that code from, I load a bunch of formatted stuff from the application to the email body text, and allow the user to add comments/details to the email before sending.


    Regards Les, Livingston, Scotland

    Sunday, August 14, 2016 11:17 PM
  • Here is Full Working & Very Easy Solution.

    1st Read Complete & Understand & Then Copy Code as per your requirement.

    'Create a Visual basic application And drop following Controls and set the properties accordingly
    
    Control			Property
    
    
    Label			Text  : Smtp Server
    
    TextBox			Name : txtSMTPServer
    
    Label			Text  : From 
    
    TextBox			Name : txtFrom
    
    Label			Text : From Display Name
    
    TextBox			Name : txtFromDisplayName
    
    Label			Text :  Recipient 
    
    TextBox			txtTo
    
    Label			Text : Attachment
    
    ListBox			Name : lstAttachment
    
    Label			Text : Subject
    
    TextBox			Name : txtSubject
    
    Label			Text : Message
    
    TextBox			Name : txtMessage
    			Multiline : True
    			Scrollbars : Both
    
    Button			Text : Add attachment
    			Name : BtnAdd
    
    
    Button			Text : Remove attachment
    			Name : btnRemove
    
    
    Button			Text : Send
    			Name : btnSend
    
    
    CheckBox		Text: Send As HTML
    			Name : chkFormat
    
    
    OpenFileDialog		Name : OFD
    			DefaultExt : *.*
    			InitialDirectory : c:\
    			Multiselect : true
    
    				
    
    'Now let us see the coding part
    
    
    'Open the Code widow and type the following statement above the Class declaration
    Imports System.Web.Mail
    'Within the Class declaration, in the general section declare variables required for this project
    
    ' Variable which will send the mail
    Dim obj As System.Web.Mail.SmtpMail
    
    'Variable to store the attachments 
    Dim Attachment As System.Web.Mail.MailAttachment 
    
    'Variable to create the message to send
    Dim Mailmsg As New System.Web.Mail.MailMessage()    
    
    
    Double click on the addattachment button to add the code
    
    'Type the following lines
    
    'Show open dialogue box to select the files to attach
    Dim Counter As Integer
    OFD.CheckFileExists = True
    OFD.Title = "Select file(s) to attach"
    OFD.ShowDialog()
    
    For Counter = 0 To UBound(OFD.FileNames)
    lstAttachment.Items.Add(OFD.FileNames(Counter))
    Next
    
    'Double Click on the Removeattachment button 
    
    'Type the following lines
    
    'Remove the attachments
    If lstAttachment.SelectedIndex > -1 Then
    lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
    End If
    
    
    
    'Double Click on the Send button 
    
    'Type the following lines
    
    
    Dim Counter As Integer
    
    'Validate the data
    If txtSMTPServer.Text = "" Then
    MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information,  "Send Email")
    Exit Sub
    End If
    
    If txtFrom.Text = "" Then
                MsgBox("Enter the From email address ...!!!", MsgBoxStyle.Information, "Send Email")
    Exit Sub
    End If
    
    If txtTo.Text = "" Then
    MsgBox("Enter the Recipient email address ...!!!", MsgBoxStyle.Information, "Send Email")
    Exit Sub
    End If
    
    If txtSubject.Text = "" Then
    MsgBox("Enter the Email subject ...!!!", MsgBoxStyle.Information, "Send Email")
    Exit Sub
    End If
    
    'Set the properties
    ‘Assign the SMTP server
    obj.SmtpServer = txtSMTPServer.Text
    'Multiple recepients can be specified using ; as the delimeter
    ‘Address of the recipient
    Mailmsg.To = txtTo.Text
    
    
    ‘Your From Address
    ‘You can also use a custom header Reply-To for a different replyto address
    Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"
    
      
    'Specify the body format
    If chkFormat.Checked = True Then
    Mailmsg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
    Else
    Mailmsg.BodyFormat = MailFormat.Text
    End If
    
    'If you want you can add a reply to header 
    'Mailmsg.Headers.Add("Reply-To", "Enter Here your Email Address")
    'custom headersare added like this
    'Mailmsg.Headers.Add("Your Name", "TestHeader")
    
    ‘Mail Subject
    Mailmsg.Subject = txtSubject.Text
    
    ‘Attach the files one by one
    For Counter = 0 To lstAttachment.Items.Count - 1
    Attachment = New MailAttachment(lstAttachment.Items(Counter))
    ‘Add it to the mail message
                Mailmsg.Attachments.Add(Attachment)
    Next
    
    ‘Mail Body
    Mailmsg.Body = txtMessage.Text
    ‘Call the send method to send the mail
    obj.Send(Mailmsg)
    
    'This application is now ready to run , try it.

    Sorry to forget to say, you must enter following text in textboxes.

    txtFrom.text = something@yahoo.com 'This is your Email Address.

    txtTo.text = something@gmail.com 'This email address is where you want to send Email.

    txtSubject.text = This is testing Email. 'This is Email Subject.

    txtSMTPServer.Text = smtp.gmail.com or smtp.yahoo.com or smtp.hotmail.com 'Your outgoing Email Server Name.

     

    Saturday, August 20, 2016 7:14 PM
  • Here is Code For Gmail, But Working with all SMTP Based Email Client (Like : Hotmail, Live, Yahoo, Rediffmail & Also Your Personal Mail)

    Just change smtp.Host = "smtp.gmail.com" to any other.

    'Copy & paste this code in button click event & Change All Email Data. (This code is tested & worked)

     On Error GoTo X

    Dim msg As New System.Net.Mail.MailMessage Dim smtp As New System.Net.Mail.SmtpClient Dim addrFrom As New System.Net.Mail.MailAddress("123@gmail.com") 'Change to Your Email address Dim addrTo As New System.Net.Mail.MailAddress("133@yahoo.com", "NEXUS") 'Change to Other Email Address & NickName msg.From = addrFrom msg.To.Add(addrTo) msg.Subject = "Testing Mail From Gmail" msg.IsBodyHtml = True msg.Body = "<h1>This is NEW Testing Mail From Gmail</h1>" smtp.Host = "smtp.gmail.com" smtp.Port = 25 smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network smtp.Credentials = New System.Net.NetworkCredential("YourEmail@Address", "yourpassword") smtp.EnableSsl = True smtp.Send(msg)

    x:

            If Err.Number = 0 Then
                MsgBox("Mail is Successfully Sended.")
            Else
                MsgBox(Err.Number & vbCrLf & Err.Description)
            End If


    If you Like it. Mark as Answer

    Saturday, August 20, 2016 7:40 PM
  • Kamlesh,

    Please consider not using GoTo (this is VB6 method) but instead traditional VB.NET exception handling with Try/Catch.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, August 21, 2016 7:19 AM
  •  If you are using Yahoo,  Gmail,  or another service which is not setup on your computer like OutLook would be,  then you can try the below example.  It is just a simple example to send just plain text with no attachments.  You can however set it up to send the message in an HTML format or even add files as attachments if wanted.

     You can use your email address in the From and To, to send an email to yourself if you need to.

    Imports System.Net.Mail
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Try
                Using e_mail As New MailMessage()
                    e_mail.From = New MailAddress("YourEmailAddress@Something.com")
                    e_mail.To.Add(New MailAddress("EmailAddressOfOtherPerson@Something.com"))
                    e_mail.Subject = "Some Subject..."
                    e_mail.IsBodyHtml = False
                    e_mail.Body = "Here is the message body to send to the person"
    
                    '            |------HOST-------|--PORT--|
                    'Yahoo   ->  smtp.mail.yahoo.com   587
                    'GMail   ->  smtp.gmail.com        587
                    'HotMail ->  smtp.live.com         587
    
                    Using Smtp_Server As New SmtpClient("smtp.mail.yahoo.com", 587) 'i am using Yahoo mail
                        Smtp_Server.UseDefaultCredentials = False
                        Smtp_Server.Credentials = New Net.NetworkCredential("YourEmailAddress@Something.com", "YourEmailPassword")
                        Smtp_Server.EnableSsl = True
                        Smtp_Server.Send(e_mail)
                    End Using
    
                    MessageBox.Show("Mail Sent")
                End Using
            Catch error_t As Exception
                MessageBox.Show(error_t.ToString)
            End Try
        End Sub
    
    End Class


    If you say it can`t be done then i`ll try it


    I would suggest following this post and using this example code as the basis for your solution.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, August 22, 2016 1:54 PM