By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

setting the TO field of a mail message?

P: n/a
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 = na**@domain.co.uk

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
Mar 25 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Howard" <no****@home.please.co.uk> wrote in message
news:La********************@pipex.net...
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

Mar 25 '06 #2

P: n/a
Thank you. Thats nice and clear now

Howard

"Homer J Simpson" <no****@nowhere.com> wrote in message
news:nLfVf.5599$K11.5112@clgrps12...

"Howard" <no****@home.please.co.uk> wrote in message
news:La********************@pipex.net...
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

Mar 25 '06 #3

P: n/a

"Howard" <no****@home.please.co.uk> wrote in message
news:nu******************************@pipex.net...
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.

Mar 26 '06 #4

P: n/a
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

"Homer J Simpson" <no****@nowhere.com> wrote in message
news:vxrVf.6616$K11.5804@clgrps12...

"Howard" <no****@home.please.co.uk> wrote in message
news:nu******************************@pipex.net...
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.

Mar 27 '06 #5

P: n/a

"Howard" <no****@home.please.co.uk> wrote in message
news:Wa********************@pipex.net...
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!
Mar 27 '06 #6

P: n/a
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

"Homer J Simpson" <no****@nowhere.com> wrote in message
news:yaVVf.9927$K11.7595@clgrps12...

"Howard" <no****@home.please.co.uk> wrote in message
news:Wa********************@pipex.net...
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!

Mar 27 '06 #7

P: n/a

"Howard" <no****@home.please.co.uk> wrote in message
news:_e********************@pipex.net...
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".

Mar 27 '06 #8

P: n/a

"Howard" <no****@home.please.co.uk> wrote in message news:Wa********************@pipex.net...
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

Mar 29 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.