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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Email in VB .Net 4

Status
Not open for further replies.

faust13

Programmer
Aug 7, 2001
176
US
Is there a better was to send email in VB .Net? My old approach of using Outlook doesn't work now, and I was curious if there is a better way now?

Cheers. ----------------------------------------
Is George Lucas Kidding...
 
why there sure is. I am using the following function in my program

Private Function EmailCust(ByVal Email As String, ByVal msgBody As String) As Boolean
Dim myMessage As New Mail.MailMessage()
Dim myMailServer As Mail.SmtpMail

myMessage.To = Email
myMessage.From = "somebody@someplace.com"
myMessage.Subject = "You subject here"

myMessage.Body = msgBody

myMailServer.SmtpServer = "your mail server here"
myMailServer.Send(myMessage)

myMessage = Nothing
Return True
End Function

hope this helps you bud [peace]
 
Beautiful, thanks!

Someone should write an FAQ on this for VB.Net, this probablly won't be the last time this is asked. ----------------------------------------
Is George Lucas Kidding...
 
Thanks for the star I think I will write an FAQ [peace]
 
This solution works fine if you are using ASP.NET. Does anybody has a solution to send email from a VB.NEt stand alone program? The class system.web.mail is not available if you are not using a ASP.NET project.

THanks
 
You can still do this in VB.NET standalone actually.
You just need to add a reference to the dll
Go to Project -> Add Reference -> Find System.Web.dll -> Select and OK.

You now have a reference to that class and can use all of the objects. That'l do donkey, that'l do
[bravo] Mark
 
Dear Zarcom,
need help with vb.net coding with the emailing problem. about the code you submitted, i dont know where to use it. i had the same problem as yohann7.
 
Did you follow the instructions I gave him?

What do you mean you don't know where to use it. I am not sure of your question so if you could clear it up for me I would be glad to help. I did end up writing an FAQ for this problem, which I recently updated to use a class. It is a bit more flexible now.
Check it out here faq796-2119 That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I am having the same issue as Yohann7 and wannabeel33t in VB.NET. I am not sure what environment they are working in, but I am on an XP Professional client with Exchange 2000 on a W2000 Advanced server

For some reason, I get the following error:

Could not access 'CDO.Message' object.

I have read up on this and some people say that you just need to register the dll (CDOSYS.DLL). This didn't work for me. Has anyone else come across this?

Peter
 
Did you follow my FAQ? With .NEt you dont' use the CDO objects. The included namespace that I used in my FAQ uses the email capabilities built into Windows to email people.
That is unless you specify a email server then it uses that. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hi Zarcom,

Sorry, my mistake...I got it to work. Thanks for the code. One final question. Why does the email not appear in my sent items box? I am using Outlook 2000 and my only delivery service is Microsoft Exchange Transport.

Thanks
Peter
 
Because it doesn't use an email client. Your sent items box is merely a email client piece of functionality. If you had two clients on your machine and sent mail from one it wouldn't appear in the other ones sent items either. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
OK
I find why I get error with the FAQ code class of mail.

I just added System.WEB before each mail.blabla in code.

To :

System.Web.Mail.MailMessage()
System.Web.Mail.MailFormat

So maybe this should help?

bye
 
PM2000, I get the same error message when VB try to send the email at line :

myMailServer.Send(myMessage)

error message :
An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll
Additional information: Could not access 'CDO.Message' object.

how you correct your problem?
 
lermure,

I had my SMTP server name wrong....silly me, it was a long day!

My code is currently identical to that which is in the FAQ that Zarcom wrote.

My best guess would be that you have the SMTP server wrong, but it could be something else. It would help if you posted your full piece of code so that I could see if everything is right.

Peter
 
ok, i just look and all is ok in my code. i have the right SMTP server.

Finaly i get a other code. (sorry zarcomm not your fault but i dont knwo why your code not work here).

I read this thread :

Its say :
Add reference to system.web.dll
(Project --> add reference)

then use this code:

'at the top of your page:
Imports System.Web.Mail


'in a sub or something
SmtpMail.SmtpServer = "mymailserver.mydomain.com"

Dim from As String = "from@mydomain.com"
Dim mailto As String = "to@yourdomain.com"
Dim subject As String = "This is the subject"
Dim body As String = &quot;<html><body>This is the HTML Message body. Remove tags
for text message</body></html>&quot;

SmtpMail.Send(from, mailto, subject, body)


I dont this and its work now. great!
 
This is excellent code but it requires an SMTP server to send mail through. Using the CDO COM object will allow the app to send mail without an SMTP server. The following instructions are for Winforms and Console applications. Web applications may differ slightly in the approach. Here are my steps:

Add a reference to the CDO COM Object: Mine is called &quot;Microsoft CDO for Windows 2000 Library&quot; and is version 1.0. I am running Windows XP, SP1. Your mileage (and COM Object name and version) may vary.

Add the following code when you want to send mail:
Code:
Dim CDOMail As New CDO.Message()
CDOMail.From = &quot;santaclause@northpole.com&quot;
CDOMail.To = &quot;rudolph@northpole.com&quot;
CDOMail.Subject = &quot;Christmas is coming soon&quot;
CDOMail.TextBody = &quot;Please get your nose ready for practice on Tuesday.&quot;
CDOMail.Send()

There are many properties and methods in the CDO class. You can send HTML mail, include file attachments, and more. Enjoy!
 
If I want to embed this code to a distributable .exe that will run from the client's machine how do I find out what is the client's SMTP server name??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top