VB.NET - Button_Click to send email-VBForums
Results 1 to 7 of 7

Thread: VB.NET - Button_Click to send email

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005

    VB.NET - Button_Click to send email

    How do i code a button click to send email to a person based
    on the txtName.Text, txtEmail.Text, txtSubject.Text and
    txtMessage.Text input by the user?

    How do i define by own from-name and from-email too?

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004

    Re: VB.NET - Button_Click to send email

    you want to use the system.web.mail class.

    more info on this is available at System.Web.Mail.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3
    New Member
    Join Date
    Apr 2005

    Re: VB.NET - Button_Click to send email

    This piece of code can be used to send mails with attachments. I'm sure it will help you a bit.
    VB Code:
    1. Dim email As New System.Web.Mail.MailMessage
    2.         ' Setting the from,to, message and subject for the mail
    3.         email.To = "urfriend@sample.com"
    4.         email.From = "yourid@gg.com"
    5.         email.Body = "Sample message that to be send to your friend"
    6.         email.Subject = "Ha Guys"
    7.         ' Adding the attachment to the mail
    8.         Dim Attach As String = "C:\02_Ranger.JPG"
    9.         Dim myAttachment As _
    10.             System.Web.mail.MailAttachment = _
    11.             New System.Web.mail.MailAttachment(Attach)
    12.         email.Attachments.Add(myAttachment)
    13.         email.BodyFormat = Web.Mail.MailFormat.Text
    14.         System.Web.Mail.SmtpMail.SmtpServer = "Put Mail Server Here"
    15.         Try
    16.             System.Web.Mail.SmtpMail.Send(email)
    17.         Catch ex As Exception
    18.             MessageBox.Show( _
    19.             ex.Message & vbNewLine & _
    20.             ex.Source & vbNewLine & _
    21.             ex.StackTrace, "Email Error", _
    22.             MessageBoxButtons.OK, MessageBoxIcon.Error)
    Brainstorming Guys

  4. #4
    Hyperactive Member
    Join Date
    Apr 2003
    Three Rivers, MI

    Re: VB.NET - Button_Click to send email

    This should do it for you.
    VB Code:
    1. Dim email As New MailMessage
    2.         Dim Attach As MailAttachment = _
    3.             New MailAttachment("C:\02_Ranger.JPG")
    5.         With email
    6.             .To = txtTo.Text
    7.             .From = txtFrom.Text
    8.             .Body = txtBody.Text
    9.             .Subject = txtSubject.Text
    10.             .Attachments.Add(Attach)
    11.             .BodyFormat = MailFormat.Text
    12.         End With
    14.         SmtpMail.SmtpServer() = txtMserver.Text
    16.         Try
    17.             SmtpMail.Send(email)
    18.             MessageBox.Show("Email was sent sucessfully", _
    19.                 "Email Sent", MessageBoxButtons.OK, _
    20.                 MessageBoxIcon.Information)
    21.         Catch ex As Exception
    22.             MessageBox.Show(ex.Message & vbNewLine & _
    23.             ex.Source & vbNewLine & ex.StackTrace, _
    24.             "Send Mail Error", MessageBoxButtons.OK, _
    25.             MessageBoxIcon.Error)
    26.         End Try
    Just remove the attachment lines if you don't need them. As an alternative you may want to use a Open File Dialog with the attachment so that it can be chosen dynamicaly/optionly instead of just being hard coded into your sub.

    Oh yea for this to work as written you need to make this your first line of code (very top of the page).

    Imports System.Web.Mail
    Last edited by BukHix; Apr 13th, 2005 at 07:03 AM.

  5. #5
    Member Retali8's Avatar
    Join Date
    Mar 2005

    Re: VB.NET - Button_Click to send email

    How would you send a crystal report that your program generates? Can you do it on the fly, or would you have to save it then add it as your attachment?

  6. #6
    Frenzied Member
    Join Date
    Jan 2001

    Re: VB.NET - Button_Click to send email

    myReport.ExportToDisk(CrystalDecisions.[Shared].ExportFormatType.WordForWindows, "c:\Temp\myDoc.doc")

    Create a report object (myReport), export it to a word document ("c:\Temp\myDoc.doc") then email the path to the document as an attachment as previously explained in this thread. Works nicely.

    Now I've got a question:

    I have used this class to send mail. I can't work out how to CC and BCC to multiple addresses. I have had no problem CC'ing and BCC'ing to a single address but can't work out how to send to multiple addresses.

  7. #7
    Frenzied Member
    Join Date
    Jan 2001

    Re: VB.NET - Button_Click to send email

    The answer was more obvious that what I thought - simply delimit multiple addresses with a semi-colon.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width