Link to home
Start Free TrialLog in
Avatar of suxintee
suxintee

asked on

how to send email?

hi. i would wan to know how to send email using visual basic. full source code will be most welcomed. if anyone who know how to do 2 way messaging(SMS). i need ur help badly. thanks. if u have source code, mail me at sooksing@bip.utm.my or suxintee@hotmail.com. dun worry, iwill grade u. cheer~
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Here is an example using Outlook, customize the code as neccessary:

Private Sub Command4_Click()
   Dim iOutlook As Outlook.Application
   Dim myApp As Outlook.MailItem
   Set iOutlook = New Outlook.Application
   Set myApp = iOutlook.CreateItem(olMailItem)
   temp = "ryancys"
   txtMessage = "Hello World Again!"
   AttachFile = "C:\Documents and Settings\Administrator\My Documents\My Pictures\next.gif"
   myApp.To = "abc@yahoo.com"
   myApp.Subject = "Hello World!"
   myApp.Attachments.Add AttachFile
   myApp.Body = "Hi " & temp & vbCrLf & vbCrLf & txtMessage
   'myApp.HTMLBody = ""
   myApp.Send
   'Do While iOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderOutbox).Items.Count <> 0
   '    DoEvents
   'Loop
   MsgBox "Successfully Sent", vbInformation, "Email Sent"
End Sub

Alternatively See use tools:

http://www.freevbcode.com/ShowCode.Asp?ID=109 
Avatar of pg_india
pg_india

hi friend i am also doing the same thing suggested by ryan but i am getting some run time error as : Some tme error comes that the item has been deleted or moved ...and error code is -214735267.....
Second error is "Sent method of _DsentItems failed..

Coding is:-
----------DECLARATION----------
Dim objOutlook As New Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
-----------------------------------------

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    MsgBox rs(1)
    objOutlookMsg.To = rs(1)
    objOutlookMsg.Subject = "test"
    objOutlookMsg.Body = Text1.Text
    'Dim i As Integer
    'For i = 0 To Me.lslfilename.ListCount - 1
    '    objOutlookMsg.Attachments.Add Me.lslfilename.List(i)
    'Next
    objOutlookMsg.Save
    objOutlookMsg.Send
    objOutlookMsg.SaveSentMessageFolder.Display
    Set objOutlookMsg = Nothing
    rs.MoveNext

---THIS IS MY CODING...

ALTHOUGH THE MAILS ARE GOING BUT THIS ERROR IS COMING....Time being i have put "ON ERROR RESUME NEXT" but i don't want to do that...plz tell me what to do!!!!!!!!!!!
Thanks



ASKER CERTIFIED SOLUTION
Avatar of Monchanger
Monchanger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Create a class module (clsOutlook)

Option Explicit

Private outlook As New outlook.Application
Private mblnTerminateOutlook As Boolean

Private Sub Class_Initialize()
 mblnTerminateOutlook = outlook.ActiveExplorer Is Nothing
End Sub

Private Sub Class_Terminate()
 If mblnTerminateOutlook Then outlook.Quit
 Set outlook = Nothing
End Sub

Public Sub SendMail(strTo As String, strSubject As String, strMessage As String, strCc As String, strBcc As String, Optional vntAttachmentPath As Variant)
   Dim i As Integer
   Dim str As String

   With outlook.CreateItem(olMailItem)
       .To = strTo
       .Subject = strSubject
       .Body = strMessage & vbCrLf & vbCrLf
       
       If strCc <> Empty Then .CC = strCc
       If strBcc <> Empty Then .BCC = strBcc
       
       If IsArray(vntAttachmentPath) Then
           For i = 0 To UBound(vntAttachmentPath)
               .Attachments.Add vntAttachmentPath(i), , Len(.Body)
           Next i
       End If
'        If you want to save the email as file
'        str = ("c:\" & strTo & "-" & strSubject & ".msg")
'        .SaveAs str
       
       .Send
   End With
End Sub


On your form where you want to send your email:
' Declaration
Private mOutlook As New clsOutlook

(replace to,subject,message,cc,bcc,attach to valid strings)

' send mail versturen with attachments
mOutlook.SendMail To, Subject, Message, CC, bcc, attaach

'mail without attachments
mOutlook.SendMail To, Subject, Message, CC, bcc
Try CDONTS library

-------------
dim odont
dim omapi
dim eFrom
dim eTo
dim econtext
dim etext

efrom = "any@domain.com"
eTo = "any@domain.com"
econtext = "sample text..."
etext= "sample......."

set odont = createobject ("CDONTS.NewMail")
set omapi = createobject ("MAPI.Session")

odont.send efrom,eto,econtext,etext

set odont = nothing

msgbox "ok"
This program sends a email using the Microsoft Mapi Controls 6.0.
U can try this.
MadhavV


Code:
Private Sub cmdReceive_Click()
    MS.NewSession = True
    MS.UserName = mack
    MS.Password = Form1.Tag
    MS.Action = 1
    MM.SessionID = MS.SessionID
    MM.FetchUnreadOnly = True
    txtSubject.Text = MM.MsgSubject
    txtMessage = MM.MsgNoteText
    MS.Action = 2
End Sub
Private Sub cmdSend_Click()
    MS.NewSession = True
    MS.Action = 1
    MM.SessionID = MS.SessionID
    MM.Compose
    MM.MsgSubject = txtSubject.Text
    MM.MsgNoteText = txtMessage
    'MM.MsgOrigAddress = "abc@experts.com"
    MM.RecipAddress = txtTo.Text
    MM.Send
    txtMessage.Text = ""
    MS.Action = 2
End Sub
Avatar of suxintee

ASKER

hi guys..thanks for ur help..i will try the methods and come back to this...really appreciate ur help!
anyone who has source code to send to me? better than copy and paste and create icons again in  visual basic....thanks a lot~
You have to do something yourself suxintee, you are not paying anyone here to do work for you. You ask for help with a problem and get suggestions on how to fix it.

We all gave you different source code, and each is good for different reasons. You have to decide which is best for you and your situation.

If you really need a complete solution, try offering more points and maybe someone will spend more time on you. Otherwise, please understand that the experts here are not payed employees of EE and cannot do others' work as well as thier own.

Monchanger
ok! i think i found something i want from vbcode.com. thanks a lot from everybodY!
thanks for the website anyway.
Weird, same link as what i have posted, but not me deserve the points..
Shucks ryancys, I didn't see you posted that. I kinda skipped over your post after noticing the type of code you used.)
I asked CS to transfer the answer to you.
suxintee,

You're off to a pretty weak start here. First of all, you selected the wrong comment as an answer; I can fix that.

Second of all, you gave the grade a C, even though you said you used the suggested code. Grades here aren't like school, and even if they were, that one would be intolerable. You should grade and assume an A, unless you have a reason not to (such as you had to do more research, or had to figure something out from the code), in which case a B might be okay.

Instead of giving a C, you should ALWAYS give the Experts a chance to raise the grade; they're willing to help you if you'll tell them the results aren't satisfactory. After all, it's your question -- you don't have to close it until you're satisfied.

ryancys: points for you at https://www.experts-exchange.com/questions/20402388/For-ryancys-re-20398968.html

Monchanger: so it does not reflect on your grading history, I've changed the grade here.

Netminder
CS Moderator