6 Replies - 42270 Views - Last Post: 15 October 2008 - 02:41 PM Rate Topic: -----

#1 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

How to send email using VB.net 2008?

Posted 07 October 2008 - 10:24 AM

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 sendMail As New Net.Mail.SmtpClient
        Dim userToken As New Object
        With sendMail
            .Host = "smtp.gmail.com"
            .Port = 465 'port use by gmail to send mail
            .Credentials = New Net.NetworkCredential("usrname", "password")
            .UseDefaultCredentials = False
        End With


        sendMail = New Net.Mail.SmtpClient("smtp.gmail.com")
        sendMail.SendAsync(tbFrom.Text, tbTo.Text, tbSubject.Text, tbBody.Text, userToken)
End Sub
End Class



I import system.net.mail, by right I can just write smtpClient, but VB.net complained "Type Expected" and forced me to write Net.Mail.SmtpClient

I enabled port in my gmail account so that I can send and receive mail user an email client. The above code failed to work, after I click on send button my message was not delievered.. I did not receive message in my hotmail mailbox.. Did I miss out something?

Btw what is usertoken?

This post has been edited by rakyomin: 07 October 2008 - 10:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to send email using VB.net 2008?

#2 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: How to send email using VB.net 2008?

Posted 07 October 2008 - 05:52 PM

Add an event handler...

        sendMail = New Net.Mail.SmtpClient("smtp.gmail.com")
        AddHandler sendMail.SendCompleted, AddressOf sendMail_SendCompleted '<<< Add this
        sendMail.SendAsync(tbFrom.Text, tbTo.Text, tbSubject.Text, tbBody.Text, userToken)



And add the sub the handler points too..

    Private Sub sendMail_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        Dim userToken As Object = e.UserState 'This is how you know which send has completed, you pass some identifier in usertoken

        If e.Cancelled Then 'Operation was cancelled
            Debug.WriteLine("Email cancelled.")
        End If

        If e.Error IsNot Nothing Then 'An error occured
            Debug.WriteLine("Error: {0}", e.Error.ToString)
        Else 'No error occured
            Debug.WriteLine("Email sent!")
        End If
    End Sub



Now you can see if an error occurs and what it is!
Was This Post Helpful? 1
  • +
  • -

#3 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to send email using VB.net 2008?

Posted 07 October 2008 - 07:34 PM

Thanks again Magicmonkey you are great :^: :^: :^:

tested with other smtp it works! email indeed was sent out, i can receive the mail sent from application.
not sure why smtp.gmail.com did not work, no error or whatsoever...

I intend to extend my traceroute gui application feature with this.

after trace was done, user can choose to send result via mail.

do i create another form and attach to my traceroute project and from main traceroute form i call formSMTP.show()?
Was This Post Helpful? 0
  • +
  • -

#4 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to send email using VB.net 2008?

Posted 08 October 2008 - 02:38 AM

I found another cool way of doing this:

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim SendMessage As New Net.Mail.SmtpClient
        Dim userToken As New Object
        Dim EmailMessage As New MailMessage
        With EmailMessage
            .From = New MailAddress(cbxFrom.Text)
            .To.Add(cbxTo.Text)
            .Subject = tbSubject.Text
            .Body = tbBody.Text
            .Attachments.Add(New Attachment(tbAttachment.Text))

        End With


        AddHandler SendMessage.SendCompleted, AddressOf SendMessage_SendCompleted

        With SendMessage
            .Host = cbxSMTPServer.Text
            .Port = 25
            .UseDefaultCredentials = False
            .Credentials = New Net.NetworkCredential("username", "password")
            .EnableSsl = False
        End With

        'SendMessage.SendAsync(cbxFrom.Text, cbxTo.Text, tbSubject.Text, tbBody.Text, userToken)
        SendMessage.SendAsync(EmailMessage, userToken)



I am using the MailMessage class to do this.
.SendAsync(MailMessage, Object), one of the way to send mail asynchronously.
MailMessage.To is a readonly property, which can be added using .To.Add() method, input is as String.
MailMessage.From is also a readonly property, create a new instance of MailAddress(input as String)
MailMessage.Attachments, is also a readonly property, use .Attachments.Add() method, input as String

Question is... how do I use Do Until Loop statement to repeat this process:
MailMessage.Attachment.Add() and MailMessage.To.Add() if users have multiple attachments and addresses to mail to... How

Do Until 'what condition should I write here to know it is the end of address and attachments input by user?'
...
Loop


Was This Post Helpful? 0
  • +
  • -

#5 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to send email using VB.net 2008?

Posted 09 October 2008 - 06:08 AM

Hi Magicmonkey

How do I send multiple attachment and addresses?

I know for every attachment, I need to do this:

Dim mail As New MailMessage()
'to add multiple attachments i need to call .add() method again and again
mail.Attachments.Add(New Attachment("c:\temp\example2.txt"))
mail.Attachments.Add(New Attachment("c:\temp\example3.txt"))



I know I have to use a do until loop statement, but since the attachment field is a text box how can my application know how many addresses to store until?
Was This Post Helpful? 0
  • +
  • -

#6 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: How to send email using VB.net 2008?

Posted 09 October 2008 - 07:08 AM

Well if you instruct the user to seperate email addresses with a ; then you can split the textbox string into an array of strings with the string.split(). Then you can loop through the string array it returns.
Was This Post Helpful? 0
  • +
  • -

#7 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to send email using VB.net 2008?

Posted 15 October 2008 - 02:41 PM

Hi Magicmonkey,

Thank you for your guidance, your hint about the split method is a good start for me to look into how to do it...
below are my codes and is working, i put the codes here in case i forgot and in case someone needs it.

Dim strTo() As String = cbxTo.Text.Split(";")
                'each s is an address in strTo array separated by delimiter
                'for each s create a new recipient using New MailAddress(s)
                For Each s As String In strTo
                    .To.Add(New MailAddress(s))
                Next


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1