简体   繁体   English

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

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

I'm trying to run this a block of code that exports the file I am working on to a PDF and emailing it to a 'client' using gmail. 我正在尝试运行此代码块,以将正在处理的文件导出为PDF,然后使用gmail将其通过电子邮件发送给“客户端”。 I keep getting the message "Failure Sending Message", if you could help shed some light on this that would be appreciated. 如果您可以帮助您对此有所了解,我将不断收到消息“发送失败消息”。 I also purposely "*****" my emails credentials for obvious reasons. 出于明显的原因,我还故意将我的电子邮件凭据“ *****”。

 <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

Looks like you are trying to use gmail as your mail server in which case you will definately need to use SSL/TLS and make sure your credentials are as follows; 看起来您正在尝试使用gmail作为邮件服务器,在这种情况下,您肯定需要使用SSL / TLS并确保您的凭据如下:

Google's SMTP server requires authentication, so here's how to set it up: Google的SMTP服务器需要身份验证,因此设置方法如下:

  • SMTP server (ie, outgoing mail): smtp.gmail.com SMTP服务器(即外发邮件): smtp.gmail.com
  • SMTP username: Your full Gmail or Google Apps email address (eg example@gmail.com or example@yourdomain.com) SMTP用户名: 您的完整Gmail或Google Apps电子邮件地址(例如example@gmail.com或example@yourdomain.com)
  • SMTP password: Your Gmail or Google Apps email password SMTP密码: 您的Gmail或Google Apps电子邮件密码
  • SMTP port: 465 SMTP端口: 465
  • SMTP TLS/SSL required: yes 需要SMTP TLS / SSL:

In order to store a copy of outgoing emails in your Gmail or Google Apps Sent folder, log into your Gmail or Google Apps email Settings and: Click on the Forwarding/IMAP tab and scroll down to the IMAP Access section: IMAP must be enabled in order for emails to be properly copied to your sent folder. 为了将外发电子邮件的副本存储在Gmail或Google Apps已发送文件夹中,请登录Gmail或Google Apps电子邮件设置,然后:单击转发/ IMAP选项卡并向下滚动到IMAP访问部分:必须在以下位置启用IMAP:为了将电子邮件正确复制到您的已发送文件夹中。

NOTE: Google automatically rewrites the From line of any email you send via its SMTP server to the default Send mail as email address in your Gmail or Google Apps email account Settings. 注意: Google会自动将您通过其SMTP服务器发送的任何电子邮件的“发件人”行改写为Gmail或Google Apps电子邮件帐户设置中的默认电子邮件发送为电子邮件地址。 You need to be aware of this nuance because it affects the presentation of your email, from the point of view of the recepient, and it may also affect the Reply-To setting of some programs. 您需要注意这一细微差别,因为从收件人的角度来看,它会影响电子邮件的显示方式,并且还可能会影响某些程序的“答复”设置。

Workaround: In your Google email Settings, go to the Accounts tab/section and make "default" an account other than your Gmail/Google Apps account. 解决方法:在“ Google电子邮件设置”中,转到“帐户”标签/部分,将“默认”帐户设置为Gmail / Google Apps帐户以外的其他帐户。 This will cause Google's SMTP server to re-write the From field with whatever address you enabled as the default Send mail as address. 这将导致Google的SMTP服务器使用您启用的默认地址重写邮件“发件人”字段。

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

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