[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

How to send Email with attachments in VB

hi EE.
I want to send email with or without attachments from withing my VB application.
I don't want to use outlook. when user presses send information then my application itself able to send email silently.
  • 5
  • 3
2 Solutions
There are a lot of manners to send email from vb
1) Perhaps the easiest way: http://www.freevbcode.com/ShowCode.Asp?ID=109
2) You can also try with shellexecute

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub SendMail(ByVal strAddress As String, _
Optional ByVal strCC As String, _
Optional ByVal strBCC As String, _
Optional ByVal strSubject As String, _
Optional ByVal strBodyText As String)
   Dim strTemp As String
   If Trim(Len(strCC)) Then
   strTemp = "&CC=" & strCC
   End If
   If Trim(Len(strBCC)) Then
   strTemp = strTemp & "&BCC=" & strBCC
   End If
   If Trim(Len(strSubject)) Then
   strTemp = strTemp & "&Subject=" & strSubject
   End If
   If Trim(Len(strBodyText)) Then
   strTemp = strTemp & "&Body=" & strBodyText
   End If
   If Len(strTemp) Then
   Mid(strTemp, 1, 1) = "?"
   End If

   strTemp = "mailto:" & strAddress & strTemp
   ShellExecute 0, "open", strTemp, 0, 0, SW_NORMAL
End Sub
3) You need a reference (project - references): Microsoft outlook object library.
    This option will automatically send everything
(more info: http://windows.oreilly.com/news/cdomapi_0900.html)
     Sending e-mail through outlook express:
Public Sub SendEmailViaOE(AttachememtPath As String, FromEmail As String, ToEmail As String, Subject As String)
Dim objOutlook As Outlook.NameSpace
Dim objNewMail As Outlook.MailItem
Dim testvar As Variant

Dim cd As CDO.Message
Set cd = New CDO.Message
cd.Subject = Subject
cd.To = ToEmail
cd.From = FromEmail
cd.AddAttachment AttachememtPath
cd.TextBody = "Hello this Brent"
'cd.AddRelatedBodyPart "c:\txtVes.txt", "ss", cdoRefTypeId

Set cd = Nothing
End Sub

4) ...
More Examples:
NaeemgAuthor Commented:
hello Dhaest i tried both of ur given method in form load event, but when compiles an error msg "expecting =" raised.
NaeemgAuthor Commented:
sorry i was not used Call statement to call method.
but SendMail method opens another popup to send, i don't want that,

and by using sendEmailViaOE rais an compile erro "User-defined type not defined"
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

NaeemgAuthor Commented:
hello anv both of ur answers do not satisfied me.
did u download the source code avaialbale in the last link..

try it ..its good..
NaeemgAuthor Commented:
my problem is solved using CDONTS by anv.
thanks to all.
NaeemgAuthor Commented:
hi anv, would u pls give me an additional line of code to attach file.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now