参数"地址"不能为空字符串。参数名称:地址



我意识到这个问题以前也出现过,但答案并没有直接回答我的问题,过去几天我也在网上搜索过。

问题是,我有一个asp.net VB表单,可以发送到电子邮件。。但是,当我单击Submit时,它会出现错误"参数'address'不能是空字符串。参数名称:address"。但奇怪的是,它仍然发送包含所有相关信息的电子邮件。

有人知道为什么它会出错但仍在发送吗?我觉得这很简单,但它让我头疼!如果您需要其他代码片段,请告诉我。

代码背后:

Imports System.IO
Imports System.Net
Imports System.Net.Mail
Partial Class _default
Inherits System.Web.UI.Page
Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles submitButton.Click
        'send new confirmation email
    Try
        'create the email message
        Dim EmailMsg As New MailMessage()
        'set the address and subject
        EmailMsg.From = New MailAddress(emailTextBox.Text.ToString)
        EmailMsg.To.Add("myemailaddress")
        EmailMsg.Subject = "Website enquiry from " + firstnameTextBox.Text.ToString
        'set the content
        EmailMsg.Body = "First Name: " + firstnameTextBox.Text.ToString + "<br>" +
                        "Last Name: " + lastnameTextBox.Text.ToString + "<br>" +
                        "Reply to: " + emailTextBox.Text.ToString + "<br>" +
                        "Ph No.: " + phoneTextBox.Text.ToString + "<br>" +
                        "Dropdown value:" + DropDownList1.SelectedValue + "<br>" +
                        "Website Address: " + webAddressTextBox.Text.ToString + "<br>" +
                        "Other option: " + otherTextBox.Text.ToString
        EmailMsg.IsBodyHtml = True
        'send the message
        Dim smtp As New SmtpClient()
        smtp.Send(EmailMsg) 'uses web.config settings
        'if successful clear form and show success message
        firstnameTextBox.Text = String.Empty
        lastnameTextBox.Text = String.Empty
        emailTextBox.Text = String.Empty
        phoneTextBox.Text = String.Empty
        DropDownList1.SelectedValue = Val("0")
        webAddressTextBox.Text = String.Empty
        otherTextBox.Text = String.Empty
        lblMessage.Text = "Message sent successfully!"
    Catch ex As Exception
        'show error message if unsuccessful
        lblMessage.Text = ex.Message
    End Try
End Sub
End Class

Web.config:

<configuration>
<system.net>
<mailSettings>
  <smtp>
    <network host="server" 
             port="25"
             userName="myemailaddress"
             password="mypassword"/>
  </smtp>
</mailSettings>

提前感谢

尝试使用

EmailMsg.To.Add(New MailAddress("myemailaddress"))

我认为您需要将MailAddress对象添加到"收件人"列表中。我在我的代码中这样做,并且我没有得到任何错误。

最新更新