简体   繁体   中英

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. 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;

Google's SMTP server requires authentication, so here's how to set it up:

  • SMTP server (ie, outgoing mail): smtp.gmail.com
  • SMTP username: Your full Gmail or Google Apps email address (eg example@gmail.com or example@yourdomain.com)
  • SMTP password: Your Gmail or Google Apps email password
  • SMTP port: 465
  • SMTP TLS/SSL required: yes

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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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