2015-04-07 284 views
0

我试图运行这一块代码,导出我正在使用PDF格式的文件并通过电子邮件将其发送到使用gmail的“客户端”。 我不断收到“失败发送消息”消息,如果您能帮助我们了解一些情况,将不胜感激。 我也故意“*****”我的电子邮件凭证,原因很明显。vb.NET SmtpClient无法使用电子邮件发送电子邮件

<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 
+0

在方法的开始处设置断点并逐步执行代码。然后你就会知道错误在哪里,从这里你可以发现什么是空/不等于它应该是什么。 – Mathemats

+0

请点击此链接:[Your Answer](http://stackoverflow.com/questions/29441471/failure-sending-mail-unable-to-connect-to-remote-server-a-socket-operation) – Mahadev

+0

@Mathemats不幸的是,我使用的软件没有调试器,我一直在将消息框放在任何地方进行测试,到目前为止,它在“For Each er Export ....”代码块中断 – user2465853

回答

0

看起来你要使用Gmail作为您的邮件服务器在这种情况下,你一定会需要使用SSL/TLS,并确保您的凭证如下;

谷歌的SMTP服务器需要身份验证,所以在这里是如何设置它:

  • SMTP服务器(即外发邮件):smtp.gmail.com
  • SMTP用户名:你 全Gmail或Google Apps电子邮件地址(例如[email protected][email protected]
  • SMTP密码:您的Gmail或Google Apps 电子邮件密码
  • SMTP端口:需要
  • SMTP TLS/SSL:

为了储存发出的电子邮件的副本,您的Gmail或谷歌应用程序发送文件夹,日志插入Gmail或Google Apps电子邮件设置,然后点击转发/ IMAP标签并向下滚动到IMAP访问部分:必须启用IMAP才能将电子邮件正确复制到发送的文件夹。

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

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