从Visual Basic发送电子邮件 [英] Sending Email from Visual Basic
问题描述
我正在开发一个项目,这个项目的一部分是将电子邮件发送到位于SQL中的电子邮件地址列表。
我正在使用以下代码,发送时,只会发出发送失败错误。没有其他的东西。
有人可以帮我出来吗?我真的很感激。
'连接到SQL Server数据库,只查询Address列填写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
'新建一个MailMessage实例
Dim mailMessage As MailMessage = New MailMessage()
mailMessage.From = New MailAddress(TextBox4。 Text.Trim())
'根据CheckBox检查状态确定mailMessage.To属性
如果CheckBox1.Checked = True Then
对于每个dr As DataRow在myDataTable.Rows
mailMessage.To.Add(新的MailAddress(dr.Item(0).ToString))
下一个
Else
mailMessage.To.Add(New MailAddress(TextBox3.Text.Trim()))
如果
mailMessage.Subject = TextBox1.Text.Trim()
mailMessage.Body = TextBox2.Text.Trim()
Dim smtpClient As SmtpClient = New SmtpClient (smtp.google.com)
smtpClient.Port =(587)
smtpClient.Credentials =新的System.Net.NetworkCredential(HIDDEN,HIDDEN)
smtpClient 。发送(mailMessage)
尝试
smtpClient.Send(mailMessage)
Catch smtpExc As SmtpException
'日志错误
MsgBox(smtpExc.Message)
Catch ex As Exception
'日志错误
MsgBox(ex.Message)
结束尝试
我得到那个代码一个谷歌搜索。
您可以提供的任何帮助,以获得这项工作将是如此赞赏。
提前感谢
Dan
编辑 - 使其工作:
使用以下功能使其正常工作以防万一有人需要它:
尝试
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials =新的Net.NetworkCredential(HIDDEN,HIDDEN)
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host =smtp.gmail.com
e_mail =新的MailMessage()
e_mail.From =新的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作为异常
MsgBox(error_t.ToString)
结束尝试
谢谢你们。希望一切顺利:)
好的,这是一个很好的解决方案...
导入System.Net.Mail'发送邮件的命名空间
公共类Form1'无论你从...执行什么类...
'我用一个按钮单击事件进行测试...
私有子btnSendEmail_Click(发件人作为对象,作为EventArgs)处理btnSendEmail.Click
Dim dtEmails As New DataTable
Dim strEmails()As String = {testing@yahoo.com,test@gmail.com}
Dim strBuilder As New System.Text.StringBuilder'可用于构建消息
'这只是我的测试...
dtEmails.Columns.Add(EmailAddress)
对于每个Str As String在strEmails
dtEmails.Rows.Add(Str )
下一个
'循环通过我们返回的datatable并发送电子邮件...'
如果dtEmails.Rows.Count> 0然后
strBuilder.AppendLine(电子邮件确认)
strBuilder.AppendLine()
对于i As Integer = 0到dtEmails.Rows.Count - 1'无论你的数据被称为'
尝试
Dim newMail As New Mail'使用我们的新邮件类来设置我们的电子邮件的属性
newMail.MailMessageTo = dtEmails.Rows(i).Item EmailAddress)'您的电子邮件地址列名称在数据表'
newMail.MailSubject =只是一个测试电子邮件!
newMail.MailMessage =你收到这封电子邮件,请让我知道!
如果Mail.SendMail(newMail)然后
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
继续为
结束尝试
下一个
结束如果
如果strBuilder.Length> 0然后
MessageBox.Show(strBuilder.ToString())
结束If
End Sub
结束类
'你可以放这个类在你的类的底部你使用...这个处理电子邮件...
公共类邮件
公共属性MailMessageTo As String
公共属性MailMessage As String
公开属性MailSubject As String
'这将发送您的邮件...
公共共享函数SendMail(ByVal oMail As Mail)As Boolean
Dim Smtp_Server As New SmtpClient
新的MailMessage()
尝试
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials =新的Net.NetworkCredential(EMAIL,PASSWORD)
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host =smtp.gmail.com
e_mail =新的MailMessage()
e_mail.From = New MailAddress(EMAIL)'无论你想要什么'
(eMailMessageTo)
e_mail.Subject )
返回True
Catch error_t作为异常
返回False
最后
Smtp_Server = Nothing
e_mail = Nothing
End Try
结束功能
结束类
工作得很好,可以根据需要进行编辑。这更有条理,更容易维护,您需要什么。另外一个很好的记录,记住你的循环通过一个DataTable发送电子邮件,你可能想把一些这在一个BackgroundWorker上,因为这可以锁定UI线程...另一件事要检查循环通过你的DataTable,你可能想要检查你的引用的电子邮件是不是DBNull.value,我没有检查,否则会引发异常。
快乐编码! / p>
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 :)
Okay, here's a great solution for you...
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 = {"testing@yahoo.com", "testing@gmail.com"}
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
This works really well, you can edit as needed to. This is much more organized and easier to maintain for what you would need. Also another good note to remember your looping through a DataTable sending emails, you may want to put some of this on a BackgroundWorker as this can lock up the UI thread... Another thing to check when looping through your DataTable, you may want to check if the email your referencing isn't 'DBNull.value', I didn't check for that, other wise it will throw an exception.
Happy Coding!
这篇关于从Visual Basic发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!