Exception Thrown please check code

A

AZNewsh

System.InvalidOperationException was caught
Message="A from address must be specified."

Public Shared Sub sendEmail(ByVal fromAddy As String, ByVal toAddy As
String, ByVal subject As String, ByVal body As String)
Try
Dim fromAddress As New
System.Net.Mail.MailAddress(fromAddy)
Dim toAddress As New System.Net.Mail.MailAddress(toAddy)
Dim msg As New System.Net.Mail.MailMessage
msg.Subject = subject
msg.Body = body
msg.IsBodyHtml = True
Dim mailSender As New System.Net.Mail.SmtpClient()
mailSender.Host = "mailgate.nau.edu"
mailSender.Send(msg)
Catch ex As Exception
lblError = ex.ToString
End Try
End Sub

I have stepped through line by line and fromAddress does have a value
however I am still gettig the exception?
 
S

Stephany Young

Yes, at the point that you 'send' the message, fromAddress 'does have a
value', but what are you doing with fromAddress?

Once you've figured that out you have the same issue with toAddress.
 
C

CodeMonkey

AZNewsh said:
System.InvalidOperationException was caught
Message="A from address must be specified."

Public Shared Sub sendEmail(ByVal fromAddy As String, ByVal toAddy As
String, ByVal subject As String, ByVal body As String)
Try
Dim fromAddress As New
System.Net.Mail.MailAddress(fromAddy)
Dim toAddress As New System.Net.Mail.MailAddress(toAddy)
Dim msg As New System.Net.Mail.MailMessage
msg.Subject = subject
msg.Body = body
msg.IsBodyHtml = True
Dim mailSender As New System.Net.Mail.SmtpClient()
mailSender.Host = "mailgate.nau.edu"
mailSender.Send(msg)
Catch ex As Exception
lblError = ex.ToString
End Try
End Sub

I have stepped through line by line and fromAddress does have a value
however I am still gettig the exception?

You are not assigning the toAddress or the fromAddress to the msg.
 
A

AZNewsh

You are not assigning the toAddress or the fromAddress to the msg.- Hide quoted text -

- Show quoted text -

Duh, that's what you get for copy and pasting old code, not sure why
that was missing - anyway, I added:

msg.From = fromAddress
msg.To = toAddress

now I am getting an error saying msg.To is read only in the editor,
however no problem with msg.from
 
C

CodeMonkey

AZNewsh said:
Duh, that's what you get for copy and pasting old code, not sure why
that was missing - anyway, I added:

msg.From = fromAddress
msg.To = toAddress

now I am getting an error saying msg.To is read only in the editor,
however no problem with msg.from

That's because To is a collection of mail addresses. You can send to
multiple addresses, but you can only send from one address.
 
A

AZNewsh

That's because To is a collection of mail addresses. You can send to
multiple addresses, but you can only send from one address.- Hide quoted text -

- Show quoted text -

I am feeling pretty silly now but still don't follow:

msg.From = fromAddress does contain only one address and is not
causing an error, however
msg.To = toAddress does contain several addresses in the form:

fromAddy =
"(e-mail address removed);[email protected];[email protected]"

I don't understand why I get a read only error when I try to assign
these addresses to msg.To or what I should do instead.

By the way thank you both for your quick and polite replies
 
C

CodeMonkey

AZNewsh said:
I am feeling pretty silly now but still don't follow:

msg.From = fromAddress does contain only one address and is not
causing an error, however
msg.To = toAddress does contain several addresses in the form:

fromAddy =
"(e-mail address removed);[email protected];[email protected]"

I don't understand why I get a read only error when I try to assign
these addresses to msg.To or what I should do instead.

By the way thank you both for your quick and polite replies

Well, the From is a single mail address, so you can assign it by doing
msg.From = fromAddress. That is fine... With To being a collection, you
will have to do msg.To.Add(toAddress) in order to add to that collection.
 
A

AZNewsh

Well, the From is a single mail address, so you can assign it by doing
msg.From = fromAddress. That is fine... With To being a collection, you
will have to do msg.To.Add(toAddress) in order to add to that collection.

Perfect!!
Site is up and running, thanks for your help in finding a quick
solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top