vb.NET SmtpClient无法使用电子邮件发送电子邮件

时间:2015-04-07 04:53:00

标签: vb.net email smtp

我试图运行这个代码块,将我正在处理的文件导出为PDF并通过电子邮件发送给客户'使用Gmail。 我一直收到消息"发送消息失败",如果你能帮助阐明这一点,我将不胜感激。 我也故意" *****"我的电子邮件凭据有明显的原因。

 <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

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试使用gmail作为邮件服务器,在这种情况下,您肯定需要使用SSL / TLS并确保您的凭据如下;

Google的SMTP服务器需要身份验证,所以请按以下步骤进行设置:

  • SMTP服务器(即外发邮件): smtp.gmail.com
  • SMTP用户名:您的 完整的Gmail或Google Apps电子邮件地址(例如example@gmail.com或 example@yourdomain.com)
  • SMTP密码:您的Gmail或Google Apps 电子邮件密码
  • SMTP端口: 465
  • 需要SMTP TLS / SSL:

要在Gmail或Google Apps Sent文件夹中存储外发电子邮件的副本,请登录您的Gmail或Google Apps电子邮件设置,然后: 单击转发/ IMAP选项卡并向下滚动到IMAP访问部分:必须启用IMAP才能将电子邮件正确复制到您发送的文件夹。

注意: Google会自动将您通过其SMTP服务器发送的任何电子邮件的发件人行重写为您的Gmail或Google Apps电子邮件帐户设置中的默认发送邮件地址作为电子邮件地址。您需要了解这种细微差别,因为它会影响您的电子邮件的呈现,从收件人的角度来看,它也可能会影响某些程序的回复设置。

解决方法:在您的Google电子邮件设置中,转到“帐户”标签/部分,然后选择&#34;默认&#34;您的Gmail / Google Apps帐户以外的帐户。这将导致Google的SMTP服务器重新编写“发件人”字段,其中包含您启用的默认地址作为默认发送邮件地址。