繁体   English   中英

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

[英]vb.NET SmtpClient not able to send email using email

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

看起来您正在尝试使用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服务器使用您启用的默认地址重写邮件“发件人”字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM