【问题标题】:vb.NET SmtpClient not able to send email using emailvb.NET SmtpClient 无法使用电子邮件发送电子邮件
【发布时间】:2015-06-11 14:26:43
【问题描述】:

我正在尝试运行这段代码,将我正在处理的文件导出为 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

【问题讨论】:

  • 在方法开始处设置断点并单步执行代码。然后您将知道错误在哪一行,从这里您可以找出什么是 Null/不等于它应该是什么。
  • 请点击此链接:Your Answer
  • @Mathemats 不幸的是,我使用的软件没有调试器,我一直在到处放置消息框来测试它,到目前为止它在“For Each er Export...”中中断。代码块

标签: vb.net email smtp


【解决方案1】:

看起来您正在尝试使用 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 已发送文件夹中,请登录您的 Gmail 或 Google Apps 电子邮件设置并: 单击“转发/IMAP”选项卡并向下滚动到“IMAP 访问”部分:必须启用 IMAP 才能将电子邮件正确复制到您发送的文件夹中。

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

解决方法:在您的 Google 电子邮件设置中,转到“帐户”标签/部分,并将“默认”设置为您的 Gmail/Google Apps 帐户以外的帐户。这将导致 Google 的 SMTP 服务器使用您启用的任何地址作为默认发送邮件作为地址重写“发件人”字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多