vb.net 使用 Gmail 发送电子邮件会出现超时错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10690015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending email using Gmail gives a time out error
提问by Emad-ud-deen
We are testing some code to send email messages using Gmail from a form, but get a time out error.
我们正在测试一些代码以使用 Gmail 从表单发送电子邮件,但出现超时错误。
Can you tell us what is missing from this code to get the email message sent?
你能告诉我们这个代码缺少什么来发送电子邮件吗?
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.EnableSsl = True
SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "MyPasswordGoesHere")
SmtpServer.Port = 465
SmtpServer.Host = "smtp.gmail.com"
mail.From = New MailAddress("[email protected]")
mail.To.Add("[email protected]")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
SmtpServer.Send(mail)
MsgBox("mail sent")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Update: Code change using MailBee. This is how we are doing emails to all the customers:
更新:使用 MailBee 更改代码。这就是我们向所有客户发送电子邮件的方式:
Dim strSqlStatement As String = "Select CustomerName, Email " & _
"From Customers " & _
"Where Email Is Not Null"
If IsConnected() Then
' Set up the sql command and lookup the parent.
'----------------------------------------------
Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)
With objSqlCommand
' Open the SqlConnection before executing the query.
'---------------------------------------------------
Cursor = Cursors.WaitCursor
ObjConnection.Open()
Dim objDataReader As SqlDataReader = .ExecuteReader()
' Go through all the customers and send out the promotion emails.
'----------------------------------------------------------------
If objDataReader.HasRows Then
MailBee.Global.LicenseKey = "My license key goes here."
Dim objSMTP As New Smtp
Dim server As New SmtpServer(TextBoxSMTPServer.Text, TextBoxUserName.Text, TextBoxPassword.Text)
'SmtpServer.Host = TextBoxSMTPServer.Text
'SmtpServer.Port = TextBoxPort.Text
'SmtpServer.Timeout = 100
'If TextBoxUseSSL.Text = "Yes" Then
' SmtpServer.EnableSsl = True
'Else
' SmtpServer.EnableSsl = False
'End If
'If TextBoxUseDefaultCredentials.Text = "Yes" Then
' SmtpServer.UseDefaultCredentials = True
'Else
' SmtpServer.UseDefaultCredentials = False
'End If
'SmtpServer.Credentials = New Net.NetworkCredential(TextBoxUserName.Text, TextBoxPassword.Text)
objSMTP.SmtpServers.Clear()
objSMTP.SmtpServers.Add(server)
While objDataReader.Read()
If objDataReader("Email").ToString <> "" Then
objSMTP.Message.From.AsString = TextBoxEmailFrom.Text
objSMTP.Message.To.AsString = objDataReader("Email").ToString
objSMTP.Message.Subject = "Promotion: " & TextBoxID.Text
objSMTP.Message.BodyPlainText = "Dear " & objDataReader("CustomerName") & "," & vbCrLf & vbCrLf & TextBoxPromotionBodyText.Text
Try
objSMTP.Send()
Catch exBadPassword As MailBeeSmtpLoginBadCredentialsException
MsgBox("The login name or password is not correct.", MsgBoxStyle.Exclamation, "Email")
blnThereWereErrors = True
Catch exBadFromAddress As MailBeeSmtpRefusedSenderException
MsgBox("The sender email must be the same as the user's email address.", MsgBoxStyle.Exclamation, "Email")
blnThereWereErrors = True
Catch ex As Exception
MsgBox(ex.Message)
blnThereWereErrors = True
End Try
End If
If blnThereWereErrors Then
Exit While
End If
End While
If blnThereWereErrors = False Then
MessageBox.Show("Mass emailing has completed." & vbCrLf, _
"Email Message.", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
End If
objDataReader.Close()
ObjConnection.Close()
Cursor = Cursors.Default
End With ' objSqlCommand
End Using ' objSqlCommand
回答by Matt Wilko
Try using a different port number. You cannot use port 465 with System.Net.Mail
as it only supports "Explicit SSL". Have a look at this pagefor more info on this.
尝试使用不同的端口号。您不能使用端口 465,System.Net.Mail
因为它只支持“显式 SSL”。查看此页面以获取有关此内容的更多信息。
Gmail will accept port 25 or 587 when sending mail via VB.NET but times out using port 465.
通过 VB.NET 发送邮件时,Gmail 将接受端口 25 或 587,但使用端口 465 超时。
Also make sure you have UseDefaultCredentials = False
还要确保你有 UseDefaultCredentials = False
Also have a look at this exampleon how to send mail using GMail in C# it might give you some more clue.
回答by dav
I had similar problem, in my case I just forgot to specify the protocol, so instead of smtp.gmail.com
I had to put ssl://smtp.gmail.com
.
我有类似的问题,就我而言,我只是忘记指定协议,所以smtp.gmail.com
我不必将ssl://smtp.gmail.com
.