I can't send mail

RensD

Member
Joined
Mar 28, 2008
Messages
8
Programming Experience
5-10
I seem to have exactly the same problem.

I can send mail from my computer using Outlook Express or a program alike.
I can't send from my VB.NET appilcation using System.Net.Mail in VB 2008.

Why o why?

Rens
 
Solved - For me...

I have spent the last 48 hours trying to find a solution to my issue of sending SMTP emails through vb.net (VS2008, .Net 3.5).
I use go daddy as an email host, they have a altered SMTP port and they require you to authenticate.

What bothered me was that outlook would send fine, but the application would not.

Here is the code that worked for me. I have data based the settings for sending email; I do not use the config file.
This allows me to change SMTP setting when I choose, or when someone uses my application.

--------------------------------------------------------------------------
VB.NET:
        Call Connect()  '/ Connects to my database
        Dc = New OleDbCommand("SELECT email.[ID], email.[Host], email.[SMTPPort], _
        email.[SSL], email.[Authenticate], email.[UserName], email.[Password], _
        email.[Fromemail] FROM email;") 
        Dc.Connection = DB : Dr = Dc.ExecuteReader

        While Dr.Read   /' only one row in the dataset

            Dim mail As New MailMessage()
            Dim smtp As New SmtpClient(Dr("HOST").ToString)

            smtp.Port = CInt(Dr("SMTPPort").ToString)
            mail.From = New MailAddress(Dr("Fromemail").ToString)
            mail.To.Add("[COLOR="Red"]recpt@domain.com[/COLOR]")  '<-- change me
            mail.Subject = "Hello - test"
            mail.Body = "hello"
            mail.IsBodyHtml = True

            If Dr("Authenticate") Then  '/ This is a boolean field in the table true/false
                smtp.UseDefaultCredentials = False
                smtp.Credentials = New NetworkCredential(Dr("Username").ToString, Dr("Password").ToString)
            Else
                smtp.UseDefaultCredentials = True
            End If

            If Dr("SSL") Then '/ This is a boolean field in the table true/false
                smtp.EnableSsl = True
            Else
                smtp.EnableSsl = False
            End If

            Try
                smtp.Send(mail)
                Label1.Text = "Email sent"
            Catch ex As Exception
                MsgBox("Failed to send " & Err.Description)
            End Try
        End While
    End Sub

Its pretty easy to understand, if you have any questions let me know.
 
Last edited:
Nope.....

"The operation timed out"

I have used the code like this:
VB.NET:
        Dim mail As New MailMessage()
        Dim smtp As New SmtpClient("smtp.gmail.com")

        smtp.Port = 465
        mail.From = New MailAddress("MyMailAddress@gmail.com")
        mail.To.Add("Recipient.Name@somewhere.nl")  '<-- change me
        mail.Subject = "Hello - test"
        mail.Body = "hello"
        mail.IsBodyHtml = True

        smtp.UseDefaultCredentials = False
        smtp.Credentials = New NetworkCredential("MyName", "MyPassword")

        smtp.EnableSsl = True

        Try
            smtp.Send(mail)
            MsgBox("Email sent")
        Catch ex As Exception
            MsgBox("Failed to send " & Err.Description)
        End Try

Couldn't be simpeler.
No-go however. :confused:
 
Nope.....

"The operation timed out"

I have used the code like this:
VB.NET:
        Dim mail As New MailMessage()
        Dim smtp As New SmtpClient("smtp.gmail.com")

        smtp.Port = 465
        mail.From = New MailAddress("MyMailAddress@gmail.com")
        mail.To.Add("Recipient.Name@somewhere.nl")  '<-- change me
        mail.Subject = "Hello - test"
        mail.Body = "hello"
        mail.IsBodyHtml = True

        smtp.UseDefaultCredentials = False
        smtp.Credentials = New NetworkCredential("MyName", "MyPassword")

        smtp.EnableSsl = True

        Try
            smtp.Send(mail)
            MsgBox("Email sent")
        Catch ex As Exception
            MsgBox("Failed to send " & Err.Description)
        End Try

Couldn't be simpeler.
No-go however. :confused:

I does work, you might have a port wrong, you may need to not authenticate, your ISP maybe blocking ports, etc...

Also you are using Gmail, better check with Gmail to see what the proper server is to use, and port. I have seen a few different ones out there. My solution is for genuine POP3 accounts, not hacking Gmail servers.

Also you have changed the code, this stuff is pretty exact so I would leave it as I put it up.
 
Well.
The funny thing is, it's working fine in a program made in VB 2003, using FW 2.x.
(Using: System.Web.Mail instead)

If you want to test it yourself with gmail, please use the following test account:
Username: vb.net.test
Password vbdotnet

Would you be so kind?
 
I think this code is working I used it by my self.

It is for VB.NET 2005

VB.NET:
    Private Sub sendMail()
        Dim obj As New system.net.mail.smtpClient
        Dim objSmtpClient As New System.Net.Mail.SmtpClient
        Dim mailmsg As New system.net.mail.mailmessage

        'SMTP server
        objsmtpclient.host = "smtp.server.com"

        'Email subject
        mailmsg.subject = "Subject"

        'SENDING EMAIL
        mailmsg = New System.Net.Mail.MailMessage("user@server.com", "to@server.com", mailmsg.Subject, "Message")
        objsmtpclient.Send(mailmsg)
    End Sub
 
Back
Top