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

Send Email From VB 5

Status
Not open for further replies.

newbie1983

Programmer
Sep 9, 2003
52
GB
Hi there,

id like to know how i could send an email from within a vb application. Ive been advised to use smtp. Any ideas?

Kind Regards

HP
 
This works for me:

Private Sub cmdEmail_Click()
On Error GoTo Err_cmdEmail_click
Rem This is a test

Rem This opens up an Email message in Outlook, and the user has full control over what happens next.
Rem Open Email Message "Recipient" "Subject Line" "Message body"
DoCmd.SendObject , , , Me.Text40, , , "Re: Incident No. " & txtIncidentNumber & ", HelpDesk No. " & txtHelpDeskNumber, Me.txtSummary

Exit_cmdEmail_Click:
Exit Sub

Err_cmdEmail_click:
MsgBox Err.Description
Resume Exit_cmdEmail_Click

End Sub
 
You can use:

1. The VB MAPI controls. Needs a Simple or Extended MAPI e-mail client such as Outlook Express or Outlook

2. Automate Outlook

3. CDO 1.21. A MAPI COM object that comes with Outlook.

4. CDONTS/CDO for Windows 2000/CDO for Exchange Server. These are SMTP COM objects.

5. Shell the Mailto protocol. Very limited but works with all e-mail clients. Can't add attachments or send the e-mail; the user must complete it.

Paul Bent
Northwind IT Systems
 
>Mailto...Can't add attachments

This is to do with whatever resolves the Mailto URL (typically the default email application), and not a built-in limitation of the Mailto URL protocol [hammer]
 
You can use the MAPI control, but the BEST solution I found is VBSendMail, a free DLL available from here:


It does attachments, html emailing and all sorts of other cool stuff. Well worth looking at. I'm using it in an app that's been working beautifully.

Stu.
 
I agree with StuH. I also use the vbsendmail.dll and it works like a well oiled machine! You can send mails in ascii or html format, with or without attachments, to a single or to multiple addressees. Good stuff!

Merlin is the name, and logic is my game...
 
I'm using the following code in a procedure to send an automated email, in html to one user.

Set poSendMail = New clsSendMail
poSendMail.From = "becky@absoluteproductions.co.uk"
poSendMail.FromDisplayName = "Diary"
poSendMail.Recipient = "chris@absoluteproductions.co.uk"
poSendMail.RecipientDisplayName = "Chris"
poSendMail.Subject = "Forthcoming Events " & Date
poSendMail.AsHTML = True
Set ts = fso_OpenTextFile("C:\Documents and Settings\Desktop\no1.htm", ForReading)
messageFile = ts.ReadAll
poSendMail.Message = messageFile
poSendMail.Send

When this runs, vb hangs. What am i doing wrong??? I have declared the following within the form declarations

Public WithEvents poSendMail As vbSendMail.clsSendMail

Private Sub poSendMail_SendSuccesful()
MsgBox ("Mail Sent OK!")
End Sub

Private Sub poSendMail_SendFailed(Explanation As String)
MsgBox ("Mail Failed!" & vbCrLf & Explanation)
End Sub

Thanks for any help

BB
 
I see you declared the subs:
Code:
Private Sub poSendMail_SendSuccesful()
    MsgBox ("Mail Sent OK!")
End Sub

Private Sub poSendMail_SendFailed(Explanation As String)
    MsgBox ("Mail Failed!" & vbCrLf & Explanation)
End Sub


Try declaring those as well:
Code:
Private Sub SendMail_Progress(PercentComplete As Long)
  ' eventually display the PercentComplete 
  ' somewhere on the form (progressbar??)
End Sub

Private Sub SendMail_Status(Status As String)
  ' eventually display the status somewhere on the form
End Sub

Merlin is the name, and logic is my game...
 
Added both of these subs within the form. Neither of these values increase.

Where am i going wrong?

BB
 
Didn't set the value for the SMTPHost. Although you can leave this to autodetect, the PC you are running from needs to have an email account already set up. Our test machine does not.

Thanks

BB
 
Sorry, error in naming:

Private Sub SendMail_...

should of course be:

Private Sub poSendMail_...


Merlin is the name, and logic is my game...
 
Does the VBSendMail DLL avoid the automatic popup security warnings with Outlook 2000?

Robert
 
i'm using shellexecute and it works perfectly
ny only question is :
how can i control the text displayed in the outlook threw vb
i mean, how can i do it with spaces, right aligned and so..

please help!!!!!!!!!
 
To TheVampire:

vbSendMail does NOT use Outlook. It is a standalone dll for e-mailing.

To Sophie19:
You could use rtf-formatting in a richtextbox and then transfer the contents of the rtbox to Outlook's bodytext.
Or, you could try using html-formatting.
Outlook supports both rtf and html.

The advantage of vbSendMail is that the end-user of your vb-App doesn't need to have Outlook installed. Lots of people work with another e-mail client. I'm one of them ;)


Merlin is the name, and logic is my game...
 
BiggerBrother, the machine you are using vbSendMail on does not have to have an e-mail account set up. The mailserver does. You can use any account that is known on the mailserver from any machine that is connected to it.
I use vbSendMail on a server where there is not one e-mail account defined. What's more, there isn't even an e-mail client program on the server. Just my app with the vbSendMail dll. The dll combined with my app replaces the e-mail client prog.

And of course you have to set all needed parameters for the dll to work. Have a look at the files accompanying the dll. You'll find what's needed and what's not.


Merlin is the name, and logic is my game...
 
I also used Shellexecute

When I try and send several emails at once - Outlook can't keep up - I get the message

Microsoft Word Mail Could not be started.
Close any Open Dialog Boxes in word and try again.

I use

Application.Wait (Now + TimeValue("0:00:04"))
Application.SendKeys "%s"

at the end of my code to try and give outlook a chance

Should I increase the time - Or try another method?
 
Hi there,

I'm using the mapirtf.dll to send emails in RTF format ( The code works on one computer but not on another one, the function WriteRTF returns 896.

Does anyone know what is wrong?
Here is a part of the code

With MDIApp.mapiMessages
.Compose
.MsgReceiptRequested = False
.MsgSubject = objDASEmailItem.Subject

' Add recipients to the message...
' Verify if any Recipient specified...
If objDASEmailItem.Recipients.Count = 0 Then
SendMail = MSGEMAILNAME
Goto EndOfProcedure
End If

For Each objDASEmailRecipientItem In objDASEmailItem.Recipients
.RecipIndex = iRecipientIndex
.RecipAddress = objDASEmailRecipientItem.Address
.RecipDisplayName = objDASEmailRecipientItem.Address 'objDASEmailRecipientItem.DisplayName
.RecipType = mapBccList
iRecipientIndex = iRecipientIndex + 1
Next
DoEvents

' Set the RTF property...
lStatus = WriteRTF(.Name, .MsgId, CStr(.SessionID), objDASEmailItem.NoteText)
If lStatus <> 0 Then
SendMail = MSGUDRTFNOTSUCCESSFULL
Goto EndOfProcedure
End If

Call .Send(False)
DoEvents
End With
 
If it works on one computer but not on another then the problem may not be the code.

The two computers may have different versions of MAPI or one has no MAPI at all.

See if you can use the package and deployment wizard to make a setup INSTALL program.... this *should* make sure that you have the correct version of any supporting files required.
 
We have the same versions of the DLLs. They were installed with a Setup program.

I even try the code on the microsoft website and it's the same problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top