【问题标题】:Error Send Email : An invalid character was found in the mail header: ';'错误发送电子邮件:在邮件标题中发现无效字符:';'
【发布时间】:2014-07-24 13:32:37
【问题描述】:

我正在尝试创建一个发送电子邮件应用程序并发现一些错误。电子邮件将使用 gridview 中的复选框发送给某个收件人。 条件是:

如果第 16 列的 email 不为空,则直接发送邮件。 --> 这已经可以了

如果第 16 列中的电子邮件为空,则电子邮件将发送给第 17 列中的收件人。--> 这部分会引发错误。

这是我发送邮件的代码:

Dim X As Integer
    For X = 0 To GridView1.Rows.Count - 1
        Dim chkBox As CheckBox = CType(GridView1.Rows(X).Cells(18).Controls(1), CheckBox)
        If chkBox.Checked = True Then
Dim email2 As String
            If GridView1.Rows(X).Cells(16).Text <> "" Then
                email2 = Trim(GridView1.Rows(X).Cells(16).Text)
            Else
                email2 = Trim(GridView1.Rows(X).Cells(17).Text)
            End If
            Try
                Dim EmailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage
                EmailMessage.From = New MailAddress("mailaddress.bla.com")
                EmailMessage.To.Add(email2)
                EmailMessage.Subject = "Attire Request"
                EmailMessage.IsBodyHtml = True
                Dim link As String = "some link"
                EmailMessage.Body = "Please login by Using this link <a href=""" & link & """>Click here </a>"

                Dim smtp As New SmtpClient(Server2)
                Dim basicAuthenticationInfo As New System.Net.NetworkCredential(Username2, Password2)
                smtp.Credentials = basicAuthenticationInfo
                smtp.Send(EmailMessage)

            Catch ex As Exception
                ' lblError.Text = lblError.Text & ex.Message & " " & ex.InnerException.ToString
                lblError.Text = ex.Message
                lblError.Visible = True
            End Try

它抛出一个错误:在邮件标题中发现了一个无效字符:';'。 你有什么建议如何解决这个问题。 编辑:循环代码

提前致谢。

【问题讨论】:

  • 调试时email2的值是多少?
  • 我认为这是由于GridView1.Rows(X).Cells(17).Text中的输入错误检查字段值以进行确认。
  • @PawanS 将暗淡的电子邮件显示为字符串...
  • @SujithKarivelil 当我将其直接发送给第 17 列中的收件人时,它工作正常。
  • 那是什么问题

标签: c# asp.net vb.net email


【解决方案1】:

如下更改 if-else 语句:

If GridView1.Rows(x).Cells(16).Value.ToString <> "" Then
email2 = Trim(GridView1.Rows(x).Cells(16).Value.ToString)
Else
email2 = Trim(GridView1.Rows(x).Cells(17).Value.ToString)
End If

注意:- Row 是根据 X 的值选择的;循环可用于单击选择多行

【讨论】:

  • 您使用的是 C# 还是 vb.net?这是 vb.net 的代码
  • 查看解释我的执行的截图,希望现在我的回答能被你接受
  • 为什么它在我的代码上显示错误......我复制你的代码以确保我没有错字但它显示错误......
  • 可能是您正在使用的 VS 版本:我正在使用 VS2010
  • 我已经解决了...我使用 null 变量来存储电子邮件...感谢 Sujith 的帮助...非常感谢..
猜你喜欢
  • 2019-05-20
  • 1970-01-01
  • 2013-07-29
  • 2014-01-03
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多