参数'address'不能是空字符串。参数名称:地址

时间:2013-09-02 02:26:44

标签: asp.net vb.net forms email

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

问题是,我有一个asp.net VB表单发送到电子邮件..但是,它出现错误“参数'地址'不能是一个空字符串。参数名称:地址”< / strong>当我点击提交时。但奇怪的是,它实际上仍然通过所有相关信息发送电子邮件。

任何人都有任何想法,为什么它会发出错误但仍在发送? 我觉得这很简单,但是我正在努力!如果您需要其他代码段,请告诉我。

代码背后:

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>

预先感谢大家

2 个答案:

答案 0 :(得分:2)

尝试使用

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

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

答案 1 :(得分:0)

.net本地DEV和生产服务器之间不匹配