错误消息:指定的字符串不是电子邮件地址所需的格式

时间:2014-05-13 07:08:50

标签: .net vb.net email visual-studio-2012

函数ValidateEmail()返回true但trimiteEmail()抛出错误:

The specified string is not in the form required for an e-mail address.

这是发送功能

Public Function trimiteEmail(destinatar As String) As Boolean

    Dim upString As String = "<div>xxx</div>"
    Dim downString As String = "<div>xxx</div>"

    If Not ValidateEmail(destinatar) = True Then
        MsgBox(destinatar & " nu este un mail valid")
        Return False
    End If

    Try
        Dim mail As New MailMessage
        mail.From = New MailAddress("gabriel@ftnet.ro")
        mail.To.Add(New MailAddress(destinatar))
        mail.Subject = Baza.subiect.Text
        mail.Body = upString & Baza.sourceCode.Text & downString
        mail.IsBodyHtml = True


        Dim smtp As New SmtpClient("mail.ftnet.ro")
        smtp.Port = 587
        smtp.EnableSsl = False

        Dim credentials As New NetworkCredential("gabriel@ftnet.ro", "******")

        smtp.Credentials = credentials
        smtp.Host = "mail.ftnet.ro"


        smtp.Send(mail)
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try


End Function

这是参考计时器。

Public Sub TimerSend()

        If InternetRetea.verificareAdresa("http://www.fire.ro/") = False Then
            Baza.TimerSend.Stop()
            initTimeCheck()
        Else

            Dim count As Integer = Baza.listaEmail.Rows.Count

            If trimiteEmail(Baza.listaEmail.Rows(sCount).Cells("Email").Value.ToString) = True Then
                Baza.status.Text = sCount + 1 & "/" & count & " Success To: " & Baza.listaEmail.Rows(sCount).Cells("Email").Value
                My.Computer.FileSystem.WriteAllText(Baza.listaNews.SelectedItem & ".txt", Baza.listaEmail.Rows(sCount).Cells("Email").Value & " SEND " & sCount & vbCrLf, True)
            Else
                Baza.status.Text = sCount + 1 & "/" & count & " Failed To: " & Baza.listaEmail.Rows(sCount).Cells("Email").Value
                My.Computer.FileSystem.WriteAllText(Baza.listaNews.SelectedItem & ".txt", Baza.listaEmail.Rows(sCount).Cells("Email").Value & " ERROR " & sCount & vbCrLf, True)
            End If

            sCount = sCount + 1

            If count = sCount Then
                Baza.status.Text = "Process completed"
                Baza.TimerSend.Stop()
                sCount = 0
            End If

        End If

    End Sub

这是例外

System.FormatException was unhandled
  Message=The specified string is not in the form required for an e-mail address.
  Source=System
  StackTrace:
       at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName)
       at System.Net.Mail.MailAddress.ParseValue(String address)
       at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
       at Fire_Newsletter.Setari.trimiteEmail(String destinatar) in D:\Proiecte\Basic\Fire Newsletter\Fire Newsletter\Setari.vb:line 214
       at Fire_Newsletter.Setari.TimerSend() in D:\Proiecte\Basic\Fire Newsletter\Fire Newsletter\Setari.vb:line 288
       at Fire_Newsletter.Setari._Lambda$__2(Object a0, EventArgs a1) in D:\Proiecte\Basic\Fire Newsletter\Fire Newsletter\Setari.vb:line 274
       at System.Windows.Forms.Timer.OnTick(EventArgs e)

谢谢

1 个答案:

答案 0 :(得分:0)

  

是:trimiteEmail(“gabriel@ftnet.ro”)工作!

然后问题在于表达式:

Baza.listaEmail.Rows(sCount).Cells("Email").Value.ToString

你需要找出它正在评估的内容,因为它显然不是你所期望的。这可能只是因为它有填充(单词之前和/或之后的空格),但可能是因为.Value是某种类型,它不提供您期望的ToString实现。 默认 ToString实现只是:完整的类型名称,因此.Value.ToString可能是Some.Namespace.SomeType。您需要找出正在评估的内容并进行修复。如前所述,最简单的方法可能是在trimiteEmail中设置一个断点,点击播放,当它暂停时只需检查destinatar的值。如果它看起来像电子邮件地址,请检查填充;可能是添加destinar = destinar.Trim修复了它。如果它看起来不像电子邮件地址,那么您需要调查正确的从网格控件中获取单元格值的方法。