473,473 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

setting the TO field of a mail message?

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
8 1517

"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
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

"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
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

"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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andy Hutchings | last post by:
Hi everybody - hope you can help out here. I have a form in a database, which is a columnar form from one of the tables in the db - there is a sub-form to the form which is a datasheet view of...
4
by: Josh | last post by:
Hi, I am trying to write a function in a module in MS Access 2000 that will change the data type of a field called 'Start' in table 'bo_cpm_CS01ALL'. Here is the code that I have done so far...
3
by: paulwilliamsonremove | last post by:
Hi, I have been manually setting up relationships in Access 2003. I received an error message when leaving a form that told me the record could not be saved because I had to have a related...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
3
by: Steve Yerkes | last post by:
There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There...
15
by: tshad | last post by:
I am trying to put an persons email address in a response I am sending another person. I can get it to work by doing the following: message.Body = resumeTop & vbCrLf & vbCrLf & "For Applicant:...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: paitoon | last post by:
Hi, there I have a problem to set the unread msg.Well, actually i can say i know nothing about this system. What i do is i have a field in database call open and in the send mail form i put a...
7
by: Orv | last post by:
I have a "Yes/No" combo box (set to required) and I want the focus to shift to differnet controls (on the same form) based on the selection of "Yes" or "No". Would this have to be placed on the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.