Monday, March 26, 2012

smtp mail message

I want to programmatcially send a message, and was fooling around with the controls.

I already know that

messagesender.Send("--@dotnet.itags.org.comcast.net", "--@dotnet.itags.org.comcast.net", "Test", "Testing, 1, 2, 3.")

works. but I wanted to accomplish the same basic thing, but with a system.net.mail.mailmessage object. here is my little bit of test code:

Dim messagesender As New System.Net.Mail.SmtpClient
Dim message As New System.Net.Mail.MailMessage

messagesender.Send("--@dotnet.itags.org.comcast.net", "--@dotnet.itags.org.comcast.net", "Test", "Testing, 1, 2, 3.")
message.From = New System.Net.Mail.MailAddress("--@dotnet.itags.org.comcast.net", "My Name")
message.Sender = New System.Net.Mail.MailAddress("--@dotnet.itags.org.comcast.net", "My Name")
message.Subject = "TEST"

message.Body = "Blah blah blah" & Environment.NewLine & Environment.NewLine & Environment.NewLine & "Testing... 1, 2, 3... Testing 1, 2, 3."

as you can see, I am sending two messages, one that was set up within the system.net.mail.smtpclient object, and one that was setup as a mailmessage. The first one goes through, but the second one does not. It thinks I am using style #1 (or whatever you call it), (from, recipients, subject, body), when I am trying to use the (message as system.net.mail.mailmessage) version.

I would do something like this....

Dim oSmtpClientAs SmtpClient =New SmtpClientDim oMailMessageAs MailMessage =New MailMessageDim BodyHTMLAs String Dim oMailAddressAs MailAddress =New MailAddress("Test@.test.com, Test)'Compose the emails To, From, Subject, and Message oMailMessage.From = oMailAddress oMailMessage.To.Add("Email@.email.com") oMailMessage.Bcc.Add("Another@.email.com") oMailMessage.Subject ="Test" BodyHTML ="Blah blah blah blah" oMailMessage.Body = BodyHTML oSmtpClient.Send(oMailMessage)

Make sure that you import or use the namespace of system.net.mail and have your smtp information configured either in the web.config or the page

Hope this helps.


Hi rbxslvr,

could you pls look at this article?http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx

it's from msdn, and there is a code example there, which u can follow as a guide line.

Hope my suggestion helps :)

0 comments:

Post a Comment