Reputation: 1
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.
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
Upvotes: 0
Views: 11222
Reputation: 7282
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.
Upvotes: 2