Link to home
Start Free TrialLog in
Avatar of xersoft
xersoft

asked on

Sending an email!

I got this idea today in class. I program at home and at school. I bring my work home and then put it on my computer and then work on it. The next day I move it all back to the disk and then to school again. Well the disks get worn and loose data and my program will soon out weigh 3 disks. So I was tinking I could write a program that right before I shut down the computer would zip all the files in my program and email them to my hotmail account. I would then down load it at home. I have no idea where to start. If someone could please help... Thanks
Avatar of xersoft
xersoft

ASKER

Well sorry... I guess I do know where to start... All I need help with is sending the email with an attchemnt to my hotmail address.
Use MAPI, or the Outlook object model to send the mail if you have it.  It is very easy to do.  Look at this code and instructions.

build the form, as follows:
1.Create a new folder to contain the files for this project. Let's call the folder "SENDMSG".
2.Start a new project in Visual Basic.
3.Save the form as "sendmsg.frm", and save the project as "sendmsg.vbp".
4.Set the caption of the form to "Send Message".
5.Add the MAPI controls to your toolbox. (See above, under "Getting Started.")
6.Add a MAPI session control to the form, and set its Name property to "MAPISession1".
7.Add a MAPI messages control to the form, and set its Name property to "MAPIMessages1".
Note: Because MAPI session and MAPI messages controls aren’t visible at runtime, it doesn’t matter where on the form you place them.
8.Add three text boxes to the form: one for the "Send To" address, one for the subject, and one for the message itself. 9.Set the Name properties on these text boxes to txtSendTo, txtSubject, and txtMessage, respectively. 10.Set the Multiline property of the txtMessage text box to True. 11.Add descriptive labels next to each text box on the form. 12.Add a command button to the form. Set its Caption property to "Send", and its Name property to "cmdSend". 13.Add the following code to the form load event:

Private Sub Form_Load()

       MAPISession1.SignOn
End Sub

14.Add the following code to the form unload event:
Private Sub Form_Unload(Cancel As Integer)

       MAPISession1.SignOff
End Sub

15.Add the following code to the command button click event:

Private Sub cmdSend_Click()

       With MAPIMessages1
       .MsgIndex = -1
       .RecipDisplayName = txtSendTo.Text
       .MsgSubject = txtSubject.Text
       .MsgNoteText = txtMessage.Text
       .SessionID = MAPISession1.SessionID
       .Send
End With

MsgBox "Message sent!", , "Send Message"
End Sub

Save and run the program, then enter and send a message. If you don't get any runtime errors, then your mail message is on its way! (If you do get an error, you may have entered the recipient's address incorrectly.) That's all it takes to write a mail-enabled application. (Don't you just love Visual Basic?)
Just what have we done? As you can see from the code, the format of your mail-enabled applications will be:
1.Establish a mail session using the MAPI session control. 2.Do some message processing with the MAPI messages control. 3.Release the mail session, again using the MAPI session control.
Receiving Mail
Processing received mail is only a little more complex than sending mail. You first must fetch the mail by calling the MAPI messages control’s Fetch method. This loads the control with unread messages from the user’s inbox. After calling the Fetch method, examine the control’s MsgCount property to find out how many messages were fetched, then set the MsgIndex property to tell the control which message you would like to process. Note that the MsgIndex property is 0-based. That is, the first message has an index of 0, the second message has an index of 1, and so on. An example will illustrate these concepts.
1.Create a new folder to contain the files for this project. Let's call the folder "RECMSG".
2.Start a new project in Visual Basic.
3.Save the form as "recmsg.frm", and save the project as "recmsg.vbp".
4.Set the Caption property of the form to "Receive Messages". 5.Add the MAPI controls to your toolbox. (See above, under "Getting Started.")
6.Add a MAPI session control to the form, and set its Name property to "MAPISession1".
7.Add a MAPI messages control to the form, and set its Name property to "MAPIMessages1".
8.Add three labels and a text box to the form. These will be used to show the date, the sender, the subject, and the message itself. Name the labels lblMsgDateReceived, lblMsgOrigDisplayName, and lblMsgSubject, and name the text box txtMsgNoteText. Clear out the Captions of the labels, and Text property of the text box. Refer to Figure 4 for size and placement.
9.Set the Locked property of the text box to True, the Multiline property to True, and the ScrollBars property to 2 - Vertical. 10.Add four more labels to provide the identifying text for the above fields. (I.e., add labels that say, "Date," "From," "Subject," and "Message."
11.Add a label to indicate the number of messages, and which message we are currently on. Set the Name property of this label to lblMsgCount, and set the Caption property to "Message 0 of 0". Refer to Figure 4, and note where the form says, "Message 0 of 0." Place your label in a similar location.
12.Add three command buttons to the form, and set their Names to cmdPrevious, cmdNext, and cmdClose. Set their Caption properties to Previous, Next, and Close, respectively. Position them as shown in Figure 4.
13.Insert a procedure named FetchNewMail. Set its type to Sub, and its Scope to Public.

Public Sub FetchNewMail()

       MAPIMessages1.FetchUnreadOnly = True
       MAPIMessages1.Fetch
End Sub

14.Insert a procedure named DisplayMessage. Set its type to Sub, and its Scope to Public.

Public Sub DisplayMessage()

       lblMsgCount.Caption = "Message " & _
       LTrim(Str(MAPIMessages1.MsgIndex + 1)) & " of " & _
       LTrim(Str(MAPIMessages1.MsgCount))
       lblMsgDateReceived.Caption = MAPIMessages1.MsgDateReceived
       txtMsgNoteText.Text = MAPIMessages1.MsgNoteText
       lblMsgOrigDisplayName.Caption = MAPIMessages1.MsgOrigDisplayName
       lblMsgSubject.Caption = MAPIMessages1.MsgSubject
End Sub

15.Add the following code to the form’s Load event:

Private Sub Form_Load()

       MAPISession1.SignOn
       MAPIMessages1.SessionID = MAPISession1.SessionID
       FetchNewMail
       DisplayMessage
End Sub

16.Add the following code to the cmdPrevious command button’s Click event

Private Sub cmdPrevious_Click()


              If MAPIMessages1.MsgIndex > 0 Then
                     MAPIMessages1.MsgIndex = MAPIMessages1.MsgIndex - 1
                     DisplayMessage
              Else
                     Beep
              End If

End Sub

17.Similarly, add the following code to the cmdNext command button’s Click event:

Private Sub cmdNext_Click()


              If MAPIMessages1.MsgIndex < MAPIMessages1.MsgCount - 1 Then
                     MAPIMessages1.MsgIndex = MAPIMessages1.MsgIndex + 1
                     DisplayMessage
              Else
                     Beep
              End If

End Sub

18.Add the following code to the cmdClose command button’s Click event:

Private Sub cmdClose_Click()

       Unload Me
End Sub

Save and run the project. When the form loads, the code in the Load event fetches the new mail (if any), and displays the first message. If there are multiple messages, you can use the Next button to move through them. The Previous button, of course, moves you back toward the first message.

Avatar of xersoft

ASKER

I don't have MS Outlook... Is there another way?
This answer does not use Outlook.  It uses the MAPI control in VB.  You can find in in the components list.  That is all you need.  You do not need Outlook.  Follow the instructions in the above answer.  
Avatar of xersoft

ASKER

Ok... I tried the program... I got the program its self to work at home but no email was sent. Then I tried the program at school where we do have outlook and it still did not work. In fact it asked what outlook profile I wanted to use or something like that. If you would please try the program your self and send an email to   testing38@hotmail.com  Make the subject say TESTEMAIL. Thanks. I will try to get it to work again...  But what I would like: I want the program to do everything and send the email all by it's self. I don't even want to know it is happening. I raised the points up by 10... bacisly because that's all I have... Thanks again!
I don't know if that would help but this opens the email program and fill the address and the title.

Public Sub OpenEMail(ByVal EMailAddress As String, ByVal Text As String)
    Dim Ret As Variant

    'The line above will open the default mail client with your
    '     address on it, also:

    Ret = Shell("Start.exe " & "mailto:" & EMailAddress & "?Subject=" & Text & "?Text=Eyal", 0)
End Sub

I dont know whether this will help but

http://www.netfokus.dk/vbadmincode

in the Winsock section as code for sending mail from VB! :)
I tried this code as well and did not see any email sent.
VB5 (SP3)
NT 4 (SP3)

The message box pops up "Message Sent!"  However,  when I run the recmsg,  there is nothing there.  I have tried several different email addresses.  No one receives the email.

Any suggestions would be great.
Thanks

I havent looked at it YET, but I'll look at it within the week, okay?
Great.  Thanks.  I did notice that the original code above sends the email to the "Outbox" Folder in MS Outlook.  And that the above code receives the email if there is mail in the "Inbox" in MS Outlook.  Knowing how to do this without Outlook would be very helpful
Thanks for your time and concern,
DoughBoy
If you want I can send you a vb5 class to send email.
This class only use MAPI32.DLL, but you must create a email profile in the Inbox.

Avatar of xersoft

ASKER

Ok... I don't want to offend anyone but: is there any way to send an email with out using ANY other programs? I just want my program to be able to email. I don't want it to have to rely on other programs (other then windows). I will add 50 more points if someone can send me the program source, using the program it's self! Thanks
xersoft:

MAPI32.DLL is part of Windows, it is not 'another program'
The InBox is part of Windows,  it is not 'another program'

Both programs are installed in the Windows Setup !!!
Great. I'll try the class.
Please send it at your earliest convenience.
Thanks
DoughBoy
guiloui@zdnetmail.com
DoughBoy: Are you xersoft?
No, aren't you?
DoughBoy:

Post a question, and I'll send you the class.
OK.  sounds good to me.
Thanks,
DoughBoy.
Question has been posted.
Avatar of xersoft

ASKER

Hello people I'm still here!
xersoft:

Give me your email address.  I'll send you the files
Great.
guiloui@zdnetmail.com
(How do I get the attached files from ZDNetmail?)
Avatar of xersoft

ASKER

Thank you CarlosJac, please do send me the files. My email address is XerSoft@hotmail.com    Thanks. You can also post this as an answer. I'll give you the points if I can get the program to work. Thanks again!
Xersoft:
I've sended the classes
Avatar of xersoft

ASKER

CarlosJac- thanks for the classes! But this uneducated VB programer is not sure how to use them. I'll add100 points to this qustion, if you could please write a very simple program that sends an attachment to an email address, just so I could look at it and have an idea on how to start. Thanks a lot! If you don't want to then just say and I'll give you the 215 already owed.
Thanks for the files.  I will look at them and try to use them.
Here's the sample.  Paste this code in a blank form.  
Add the classes into your project.  Drop a command button in your form. Run.

Merry Christmas!


Private Sub Command1_Click()
Dim WithEvents email As EmailHandler
Dim Attachs As String
Dim SendTo As String
Dim CC As String
Dim Subject As String
Dim Text As String
Dim ProfileName as String
Dim ProfilePassword as String
Dim FileName as String

Set email = New EmailHandler
ProfileName = "Carlos Ruben Jacobs"
ProfilePassword = ""
FileName = "c:\autoexec.bat"

If email.OpenSession(ProfileName, ProfilePassword, False) Then
   
    Subject = "Test Subject"
    Text = "This is the mail text"
    SendTo = "CarlosJac@alvisoft.com.ar;crjacobs@hotmail.com"
    CC = "AnaliaFri@alvisoft.com.ar"
    Attachs = FileName
   
    email.SendMail Subject, Text, SendTo, CC, Attachs
   
    email.CloseSession
End If

End Sub

Avatar of xersoft

ASKER

This looked so good to! Well I tired your code and I got a few problems. First the line
Dim WithEvents email As EmailHandler
will not go. the WithEvents keyword will not take in a sub or function. I hope you know whats wrong... Thanks for all the help!
Avatar of xersoft

ASKER

I looked up withevents in the help thingy and it said it could only be used in a class. I know nothing of classes and don't know how to change the code to make it work. I am using VB5.0 if it matters... Also when I add one of your classes to my progrm it said Class1 can not be public in this type of porject. The item has been changed to private.
I don't know what this means or what it could effect. I only have 115 points left and if you can get this to work for me I'll add all 115 That's a total of 330! (15 more then previously stated). Thanks for all the help you have given so far!
Put the "WithEvents" on the General Declaration of the form
ASKER CERTIFIED SOLUTION
Avatar of CarlosJac
CarlosJac

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
CarlosJac I will be greatful if you can send me your class to.
My e-mail is eyal@magen.com.
Thanks in advance