Sending mail message

neondestruction

New member
Joined
Nov 1, 2007
Messages
2
Programming Experience
Beginner
I'm fairly new to VB.NET, so I'll try to explain this as best as I can.

I have created a form that contains 2 sets of radiobuttonlists asking 2 questions. I store the answers to the 2 radiobuttonlists in variables, and then insert those answers into the body of a mail message.

All of that logic works fine. However, when I click the submit button, the maiil message fails to send on the first click. If I click it again, the mail message is sent, but always with the previous "click's" variables. So basically the mail message information is always one behind, if that makes any sense.

Here is the code:


VB.NET:
Sub Button1_Click(sender As Object, e As EventArgs)


    ' Build a MailMessage
    Dim mailMessage = New MailMessage


    'Int for counting rows in TLAssociates table
    Dim I As Integer = 0
    'String for storing e-mail addresses of Techlab Associates
    Dim fromString as String
    'dataset for looping through the dataset
    Dim emailSet As System.Data.DataSet = GetTLEmails()

    'Loop created to obtain e-mail addresses
    For I = 0 To emailSet.Tables(0).Rows.Count - 1

        If I = 0
            fromString = emailSet.Tables(0).Rows(I).Item("Email")

        Else
            fromString = fromString + ", " + emailSet.Tables(0).Rows(I).Item("Email")

        End If

    Next

    mailMessage.From = fromString
    mailMessage.To = "xxxxxxxxxxxxxxxxxx"
    mailMessage.Subject = "Chicken or Fish?"
    mailMessage.BodyFormat = MailFormat.Html

    Dim Chicken as String
    Dim Fish as String

    If chickenRBL.SelectedItem.text = "No"
        Chicken = "does not like"
    Else
        Chicken = "does like"
    End If

    If FishRBL.SelectedItem.text = "No"
        Fish = "does not like"
    Else
        Fish = "does like"
    End If

    Dim mailBody as String = "This person " + Chicken + " chicken. <BR>" + "This person " + Fish + " fish. <BR>"





    mailMessage.Body = mailBody

    System.Web.Mail.SmtpMail.SmtpServer = "xxxxxxxxxxxxxxx"
    System.Web.Mail.SmtpMail.Send(mailMessage)


        sentChicken.text = Chicken
        sentChicken.visible = true

        sentFish.text = Fish
        sentFish.visible = true

        chicken = ""
        fish = ""

End Sub

Can anyone help me out please?
 
Last edited by a moderator:
If you're using VB 2005, you should be using System.Net.Mail, its the newer version. A lot of information can be found about that here.

Otherwise, I don't really see anything that looks wrong. The way i do the SMTP server for system.net.mail is a little different (probably because I'm using net.mail instead of web.mail), but otherwise It all looks about the same. Here's how i do the send:

VB.NET:
Dim smtp As New SmtpClient("Server name", port)
smtp.Credentials = New Net.NetworkCredential("", "")
smtp.Send(mail)

The port will be whatever one your server uses, this should be an int.

Otherwise i'm not sure what else to tell you
 
Thanks, but I don't think that is the problem, because I've been using System.Web.Mail with another page I created, and it works fine. I'm wondering if it could have something to do with page.ispostback? I'm not very proficient in using it, so I didn't use it in this code.
 
Last edited:
Back
Top