[英]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服務器需要身份驗證,因此設置方法如下:
為了將外發電子郵件的副本存儲在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.