我试图运行这一块代码,导出我正在使用PDF格式的文件并通过电子邮件将其发送到使用gmail的“客户端”。 我不断收到“失败发送消息”消息,如果您能帮助我们了解一些情况,将不胜感激。 我也故意“*****”我的电子邮件凭证,原因很明显。vb.NET SmtpClient无法使用电子邮件发送电子邮件
<MiCode(ControlScriptEventType.AfterInkAdded, "Email")> _
Public Sub Ctl_Email_AfterInkAdded(ByVal e As AfterInkAddedEventArgs)
MsgBox("1")
Dim EmailTo As String = _EmailAddress.Value
Dim EmailToName As String = "Client"
Dim EmailFrom As String = "******"
Dim EmailFromName As String = "WHSD"
Dim fileName As String = String.Empty
Dim erl As New System.Collections.Generic.List(Of ExportResult)
For Each er As ExportResult In _form.Validator.ExportResults
erl.Add(er)
fileName = System.IO.Path.GetFileNameWithoutExtension(er.ExpandedFilePath)
Next
Try
Dim fromAddress As New MailAddress(EmailFrom, EmailFromName)
Dim toAddress As New MailAddress(EmailTo, EmailToName)
Using msg As New MailMessage(fromAddress, toAddress)
msg.Body = "This will be the body of the message you are sending." & VbCrLf & "Thank you for your purchase."
msg.Subject = (Me._form.Name & " (" & fileName & ")")
' Add the mail body - an HTML file attached to this form.
For Each attData As AttachmentData In _form.Attachments
If String.Compare(attData.Name, "Lead Generation.html") = 0 Then
msg.Body = System.Text.UTF8Encoding.UTF8.GetChars(attData.BinaryData())
msg.Body = msg.Body.Replace("[filename]", fileName)
End If
Next
' Add pdf/csv file attachments to mail - they are datapaths of the form.
For Each er As ExportResult In erl
If er.Success And (er.DataPathName = "PDF" Or er.DataPathName = "CSV") Then
msg.Attachments.Add(New Attachment(er.ExpandedFilePath))
End If
Next
Dim client As New SmtpClient("aspmx.l.google.com", 25)
'client.EnableSsl = True
'client.Timeout = 10000
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Credentials = New NetworkCredential("********", "******")
client.Send(msg)
Me.RecordExportResult("Email", True, "Sent email", "Sent email to " & EmailToName & "(" & EmailTo & ")", "")
MsgBox("Sent!")
End Using
Catch ex As Exception
Me.RecordExportResult("Email", False, ex.Message, ex.Message, ex.Message)
MsgBox(ex.Message)
End Try
End Sub
在方法的开始处设置断点并逐步执行代码。然后你就会知道错误在哪里,从这里你可以发现什么是空/不等于它应该是什么。 – Mathemats
请点击此链接:[Your Answer](http://stackoverflow.com/questions/29441471/failure-sending-mail-unable-to-connect-to-remote-server-a-socket-operation) – Mahadev
@Mathemats不幸的是,我使用的软件没有调试器,我一直在将消息框放在任何地方进行测试,到目前为止,它在“For Each er Export ....”代码块中断 – user2465853