从 Visual Basic 发送电子邮件

2024-02-18

我正在开发一个项目,该项目的一部分是将电子邮件发送到位于 SQL 中的电子邮件地址列表。 我正在使用以下代码,发送时只会抛出“发送失败”错误。没有其他的。

谁能帮我解决这个问题吗?我真的很感激。

       '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

我从谷歌搜索中得到了该代码。 如果您能为这项工作提供任何帮助,我们将不胜感激。

提前致谢, 担

编辑-让它工作:

使用以下命令让它工作。以防万一其他人需要它:

  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

多谢你们。希望一切安好 :)


好吧,这里有一个很好的解决方案适合您......

Imports System.Net.Mail 'Namespace for sending the email

Public Class Form1 'Whatever class your doing this from...

'I tested with a button click event...
Private Sub btnSendEmail_Click(sender As Object, e As EventArgs) Handles btnSendEmail.Click
    Dim dtEmails As New DataTable
    Dim strEmails() As String = {"[email protected] /cdn-cgi/l/email-protection", "[email protected] /cdn-cgi/l/email-protection"}
    Dim strBuilder As New System.Text.StringBuilder 'Can be used to build a message

    'This was only for my testing...
    dtEmails.Columns.Add("EmailAddress")
    For Each Str As String In strEmails
        dtEmails.Rows.Add(Str)
    Next

    'Loop through our returned datatable and send the emails...'
    If dtEmails.Rows.Count > 0 Then
        strBuilder.AppendLine("Emails Confirmation")
        strBuilder.AppendLine(" ")

        For i As Integer = 0 To dtEmails.Rows.Count - 1 'Whatever your datatbale is called'
            Try
                Dim newMail As New Mail 'Use our new mail class to set our properties for the email'
                newMail.MailMessageTo = dtEmails.Rows(i).Item("EmailAddress") 'What your email address column name is in the data table'
                newMail.MailSubject = "Just a Test email!"
                newMail.MailMessage = "Did you get this email, please let me know!"

                If Mail.SendMail(newMail) Then
                    strBuilder.AppendLine("SENT - " & dtEmails.Rows(i).Item("EmailAddress").ToString.ToUpper)
                Else
                    strBuilder.AppendLine("FAILED - " & dtEmails.Rows(i).Item("EmailAddress").ToString.ToUpper)
                End If

            Catch ex As Exception
                Continue For
            End Try
        Next
    End If

    If strBuilder.Length > 0 Then
        MessageBox.Show(strBuilder.ToString())
    End If
 End Sub

 End Class

 'You can put this class at the bottom of your class your using...This handles the emails...
 Public Class Mail
  Public Property MailMessageTo As String
  Public Property MailMessage As String
  Public Property MailSubject As String

 'This will send your mail...
 Public Shared Function SendMail(ByVal oMail As Mail) As Boolean
    Dim Smtp_Server As New SmtpClient
    Dim e_mail As New MailMessage()

    Try
        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential("EMAIL", "PASSWORD")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        e_mail = New MailMessage()
        e_mail.From = New MailAddress("EMAIL") 'Whatever you want here'
        e_mail.To.Add(oMail.MailMessageTo)
        e_mail.Subject = oMail.MailSubject
        e_mail.IsBodyHtml = False
        e_mail.Body = oMail.MailMessage
        Smtp_Server.Send(e_mail)

        Return True
    Catch error_t As Exception
        Return False
    Finally
        Smtp_Server = Nothing
        e_mail = Nothing
    End Try

  End Function

 End Class

这非常有效,您可以根据需要进行编辑。这对于您的需要来说更有组织性并且更容易维护。另外,要记住循环通过 DataTable 发送电子邮件,您可能需要将其中一些放在 BackgroundWorker 上,因为这可能会锁定 UI 线程...循环 DataTable 时要检查的另一件事是,您可能需要检查您引用的电子邮件是否不是“DBNull.value”,我没有检查这一点,否则它将引发异常。

快乐编码!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Visual Basic 发送电子邮件 的相关文章

随机推荐