Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send email

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

I came up with this SUB after reading online..
Code:
 Private Sub SendEmailToAnnouncer()
        Dim objMM As New MailMessage
        objMM.To = "Whatever"
        objMM.From = "Whatever"
        objMM.Subject = "Whatever"
        objMM.Body = "Whatever"
        SmtpMail.SmtpServer = "Email Server's IP Address"

        SmtpMail.Send(objMM)
    End Sub

is that correct...looks simple to me, so got a doubt...
how can i send some variables in the mail body..i mean what is the syntax...

thanks

-DNG
 
To send mail with asp.net you have to use the: system.net.mail namespace. It is a little different from sending mail with vb.net.
The right way is:

Code:
Imports System.Net
Imports System.Text

        Dim mm As New Mail.MailMessage

        mm.To.Add("address1;address2;...")
        mm.From = New Mail.MailAddress("x@xx.xxx")
        mm.Body = "Whatever"
        mm.Subject = "Whatever"

        Dim MailObj As New Mail.SmtpClient
        MailObj.Host = "Email Server's IP Address"

        MailObj.Send(mm)
 
i read that we should import Imports System.Web.Mail

is that right??

-DNG
 
DNG,
You're absolutly right. Except that there were some name changes in the later version of the .NET framework, and System.Net is where the email classes now are. If you're using .NET 1.0 (and 1.1 as well, I believe), then you'll need to use System.Web.Mail and the original code you posted above.

The body is simply a string variable which you create using regular string rules, so adding a variable to the body doesn't require any special syntax. You could specify the body format as html, though, in which case you could use html tags.

JC

_________________________________
I think, therefore I am. [Rene Descartes]
 
Thanks for the info JCruz063,

I pasted the above code given by TipGiver and the editor highlighted some errors...

mm.To.Add("address1;address2;...") ->add is not a member of string

New Mail.MailAddress("x@xx.xxx") -> Type Mail.MailAddress is not defined

Dim MailObj As New Mail.SmtpClient -> type Mail.SmtpClient is not defined...

is there any new syntax changes for these...

-DNG
 
Yes there are some changes in version 2.0 of the framework. To add a new mail recipient to send an email "To" use:
Code:
objMM.To.Add("me@myemail.com")
To add a "From" email address use:
Code:
objMM.From = New MailAddress("me@myemail.com")
and for setting an SMTP server and sending the email use:
Code:
Dim mySMTPServer As New SmtpClient
mySMTPServer.Host = "127.0.0.1"
mySMTPServer.Send(objMM)



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ok i am confused now...ca8msm, i am not using the original posted code...here is my current code...
Code:
 Private Sub SendEmailToAnnouncer()
        Dim mm As New Mail.MailMessage

        mm.To.Add("address1;address2;...")
        mm.From = New Mail.MailAddress("x@xx.xxx")
        mm.Body = "Whatever"
        mm.Subject = "Whatever"

        Dim MailObj As New Mail.SmtpClient
        MailObj.Host = "Email Server's IP Address"

        MailObj.Send(mm)
    End Sub

and i am getting the errors as posted in the previous email...

-DNG
 
What version of the framework are you using?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
this is how I do it with 1.1


Imports System.Web.Mail

Private Sub Email()

Dim objMM As New MailMessage

objMM.To = Label.Text
objMM.From = "Whatever.net"
objMM.Subject = "subject"
objMM.Body = "email body"

SmtpMail.SmtpServer = "your servers ip address"

SmtpMail.Send(objMM)

SmtpMail.SmtpServer = ""

End Sub


 
Yes, you had the code right in the first place if you are using 1.1 (my post and an earilier one by JCruz063 said that there were changes if you were using 2.0). So, to answer your original question change
Code:
objMM.Body = "Whatever"
to
Code:
objMM.Body = "Whatever" & strVariable1 & " " & strVariable2 etc...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
thank you so much guys...so i got the code right in the first place...

-DNG
 
Yes, it was just the variables that needed adding to the Body tag as you asked.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top