Sending Email From Visual Basic - Stack Overflow

I am working on a project and part of this project is to send emails to a list of email addresses located in SQL. I am using the following code, which, when sent, just throws a "Sending Failed" error. Nothing else.

Can anyone please help me out with this one? I would really appreciate it.

'Connect to SQL Server database and query out only Address column to fill into DataTable Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=FigClubs;Integrated Security=True;Pooling=False") Dim cmd As SqlCommand = New SqlCommand("SELECT Email FROM Members", con) con.Open() Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd) Dim myDataTable As DataTable = New DataTable myDA.Fill(myDataTable) con.Close() con = Nothing 'New a MailMessage instance Dim mailMessage As MailMessage = New MailMessage() mailMessage.From = New MailAddress(TextBox4.Text.Trim()) ' Determine the mailMessage.To property based on CheckBox checked status If CheckBox1.Checked = True Then For Each dr As DataRow In myDataTable.Rows mailMessage.To.Add(New MailAddress(dr.Item(0).ToString)) Next Else mailMessage.To.Add(New MailAddress(TextBox3.Text.Trim())) End If mailMessage.Subject = TextBox1.Text.Trim() mailMessage.Body = TextBox2.Text.Trim() Dim smtpClient As SmtpClient = New SmtpClient("smtp.google.com") smtpClient.Port = ("587") smtpClient.Credentials = New System.Net.NetworkCredential("HIDDEN", "HIDDEN") smtpClient.Send(mailMessage) Try smtpClient.Send(mailMessage) Catch smtpExc As SmtpException 'Log errors MsgBox(smtpExc.Message) Catch ex As Exception 'Log errors MsgBox(ex.Message) End Try

I got that code from a google search. Any help you can provide to get this working would be so appreciated.

Thanks in advance, Dan

EDIT - Got it to work:

Got it to work using the following. Just in case anyone else needs it:

Try Dim Smtp_Server As New SmtpClient Dim e_mail As New MailMessage() Smtp_Server.UseDefaultCredentials = False Smtp_Server.Credentials = New Net.NetworkCredential("HIDDEN", "HIDDEN") Smtp_Server.Port = 587 Smtp_Server.EnableSsl = True Smtp_Server.Host = "smtp.gmail.com" e_mail = New MailMessage() e_mail.From = New MailAddress(TextBox4.Text) e_mail.To.Add(TextBox3.Text) e_mail.Subject = TextBox1.Text e_mail.IsBodyHtml = False e_mail.Body = TextBox2.Text Smtp_Server.Send(e_mail) MsgBox("Mail Sent") Catch error_t As Exception MsgBox(error_t.ToString) End Try

Thanks guys. Hope all is well :)

Từ khóa » Visual Basic Send Email