vb.net 参数“地址”不能是空字符串。参数名称:地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18564898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:53:48  来源:igfitidea点击:

The parameter 'address' cannot be an empty string. Parameter name: address

asp.netvb.netformsemail

提问by user2738402

I realize this question has come up before but the answers don't answer my question directly, I've also searched the net for the last few days.

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

The problem is, I have a asp.net VB form that sends to email.. However, it comes up with the error "The parameter 'address' cannot be an empty string. Parameter name: address"when I click on Submit. But the strange thing is, it still actually sends the email through with all relevant info included.

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

Anyone have any ideas as to why it's giving an error but still sending? I feel like it's something simple, but it's doing my head in! Let me know if you need other code snippets.

任何人都知道为什么它会出错但仍在发送?我觉得这很简单,但它正在做我的头脑!如果您需要其他代码片段,请告诉我。

Code Behind:

背后的代码:

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>

Thanks heaps in advance

提前致谢

回答by DeanOC

Try using

尝试使用

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

I think you need to add MailAddress objects to your To list. I do in my code and I don't get any errors.

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

回答by wolf

.net mismatch between your local DEV and production server

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