setting the TO field of a mail message?

  • Thread starter Thread starter Howard
  • Start date Start date
H

Howard

Using visual studio 2005 I am sending an email using a vaiable

Dim mail As New MailMessage()

and then later setting the recipient address by

mail.To = (e-mail address removed)

This works fine. However the compiler tells me that New MailMessage() is now
obsolete and that instead I should use

Dim mail2 As New System.Net.Mail.MailMessage()

But when I do and then try to set the To field using Mail2.To it tells me
this property is read only!
How do you set the TO field using the newer mailmessage class?

Howard
 
But when I do and then try to set the To field using Mail2.To it tells me
this property is read only!
How do you set the TO field using the newer mailmessage class?

Note that 'To,CC and BCC' are collections, so you need to get them to append
to them.

This is J# but

================================================
import System.Net.Mail.*;

private void btnSend_Click(Object sender, System.EventArgs e)
{
MailAddress myTo = new MailAddress(toaddress);
MailAddress myFrom = new MailAddress(fromaddress);
MailMessage myMessage = new MailMessage(myFrom, myTo);
myMessage.set_Body("This is the body");
myMessage.set_Subject("This is the subject");
SmtpClient myClient = new SmtpClient("smtp.server.net");
myClient.Send(myMessage);
}
================================================
toaddress and fromaddress should be email addresses in quotes
 
Thank you. Thats nice and clear now

Howard



Homer J Simpson said:
Note that 'To,CC and BCC' are collections, so you need to get them to
append to them.

This is J# but

================================================
import System.Net.Mail.*;

private void btnSend_Click(Object sender, System.EventArgs e)
{
MailAddress myTo = new MailAddress(toaddress);
MailAddress myFrom = new MailAddress(fromaddress);
MailMessage myMessage = new MailMessage(myFrom, myTo);
myMessage.set_Body("This is the body");
myMessage.set_Subject("This is the subject");
SmtpClient myClient = new SmtpClient("smtp.server.net");
myClient.Send(myMessage);
}
================================================
toaddress and fromaddress should be email addresses in quotes
 
Thank you. Thats nice and clear now

Here is a basic version

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim myTo As MailAddress, myFrom As MailAddress, myMessage As MailMessage
Dim myClient As SmtpClient
myTo = New MailAddress("whoATwhereDOTcom")
myFrom = New MailAddress("whoATwhereDOTcom")
myMessage = New MailMessage(myFrom, myTo)
myMessage.Body = ("This is the body")
myMessage.Subject = ("This is the subject")
myClient = New SmtpClient("smtp.server.net")
myClient.Send(myMessage)
End Sub

Note the AT and DOT need to be changed to @ and . -- Outlook screws them
up.
 
thank you, even better !
I don't suppose you could show me how to add word document attachment as
well.
The plain text version that my code generates is not so good and needs
coping and pasting into a word proforma at the other end. It would be better
if I could build up the word doc directly from VB and then email it as an
attachment.

Howard
 
thank you, even better !
I don't suppose you could show me how to add word document attachment as
well.
The plain text version that my code generates is not so good and needs
coping and pasting into a word proforma at the other end. It would be
better if I could build up the word doc directly from VB and then email it
as an attachment.

I haven't got that far yet. I believe adding attachments is pretty straight
forward. The real trick is with the collections, like To: and Bcc:. You have
to Get them and then Add to them. If you try sending mail to yourself you
can easily try some variations and see what happens.

Good luck!
 
Thanks anyway - can't blame for for trying!
The code you supplied is very useful - got me past a block in the project

Cheers
Howard
 
Thanks anyway - can't blame for for trying!
The code you supplied is very useful - got me past a block in the project

Yes. Learning .Net seems to be best described as "Programming by Cursing".
 
Howard said:
thank you, even better !
I don't suppose you could show me how to add word document attachment as
well.
The plain text version that my code generates is not so good and needs
coping and pasting into a word proforma at the other end. It would be better
if I could build up the word doc directly from VB and then email it as an
attachment.

Howard

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles MyBase.Load

Dim myTo As MailAddress, myFrom As MailAddress

Dim myClient As SmtpClient, myMessage As MailMessage



'A simple message

myMessage = New MailMessage("Person1_AT_mail_DOT_com", "Person2_AT_mail_DOT_com", _

"This is the simple subject", "This is the simple body")

myClient = New SmtpClient("smtp.server.net")

myClient.Send(myMessage)



'A more complex example

myTo = New MailAddress("Person1_AT_mail_DOT_com", "FirstNameA SecondNameA")

myFrom = New MailAddress("Person2_AT_mail_DOT_com", "FirstNameB SecondNameB")

myMessage = New MailMessage(myFrom, myTo)

myMessage.IsBodyHtml = True

myMessage.To.Add("Person3_AT_mail_DOT_com")

myMessage.Bcc.Add("Person4_AT_mail_DOT_com")

myMessage.Body = ("<B>This is the body</B>")

myMessage.Subject = ("This is the subject")

myMessage.Attachments.Add(New Attachment("C:\MyLargeImage1.bmp"))

myMessage.Attachments.Add(New Attachment("C:\MyLargeImage2.bmp"))

myMessage.Attachments.Add(New Attachment("C:\MySmallImage1.bmp"))

myMessage.Attachments.Add(New Attachment("C:\MySmallImage2.bmp"))

'myClient = New SmtpClient("smtp.server.net")

myClient.Send(myMessage)

End Sub
 
Back
Top