Solved

Sending an email!

Posted on 1998-11-16
32
168 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:xersoft
  • 9
  • 8
  • 8
  • +3
32 Comments
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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.
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
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.

0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
I don't have MS Outlook... Is there another way?
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
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.  
0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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!
0
 
LVL 15

Expert Comment

by:Eyal
Comment Utility
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

0
 

Expert Comment

by:kennym141282
Comment Utility
I dont know whether this will help but

http://www.netfokus.dk/vbadmincode

in the Winsock section as code for sending mail from VB! :)
0
 

Expert Comment

by:DoughBoy
Comment Utility
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

0
 

Expert Comment

by:kennym141282
Comment Utility
I havent looked at it YET, but I'll look at it within the week, okay?
0
 

Expert Comment

by:DoughBoy
Comment Utility
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
0
 
LVL 1

Expert Comment

by:CarlosJac
Comment Utility
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.

0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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
0
 
LVL 1

Expert Comment

by:CarlosJac
Comment Utility
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 !!!
0
 

Expert Comment

by:DoughBoy
Comment Utility
Great. I'll try the class.
Please send it at your earliest convenience.
Thanks
DoughBoy
guiloui@zdnetmail.com
0
 
LVL 1

Expert Comment

by:CarlosJac
Comment Utility
DoughBoy: Are you xersoft?
0
 

Expert Comment

by:DoughBoy
Comment Utility
No, aren't you?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:CarlosJac
Comment Utility
DoughBoy:

Post a question, and I'll send you the class.
0
 

Expert Comment

by:DoughBoy
Comment Utility
OK.  sounds good to me.
Thanks,
DoughBoy.
0
 

Expert Comment

by:DoughBoy
Comment Utility
Question has been posted.
0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
Hello people I'm still here!
0
 
LVL 1

Expert Comment

by:CarlosJac
Comment Utility
xersoft:

Give me your email address.  I'll send you the files
0
 

Expert Comment

by:DoughBoy
Comment Utility
Great.
guiloui@zdnetmail.com
(How do I get the attached files from ZDNetmail?)
0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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!
0
 
LVL 1

Expert Comment

by:CarlosJac
Comment Utility
Xersoft:
I've sended the classes
0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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.
0
 

Expert Comment

by:DoughBoy
Comment Utility
Thanks for the files.  I will look at them and try to use them.
0
 
LVL 1

Expert Comment

by:CarlosJac
Comment Utility
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

0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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!
0
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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!
0
 
LVL 15

Expert Comment

by:Eyal
Comment Utility
Put the "WithEvents" on the General Declaration of the form
0
 
LVL 1

Accepted Solution

by:
CarlosJac earned 210 total points
Comment Utility
To resolve your problems:

1) Put Dim WithEvents email As EmailHandler in General Declaration of the form
2) Ignore the message: Class1 can not be public in this type of porject. The item has been changed to private.
3) Ready !


More Details
------------


Problem a) 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!

Answer:  From Eyal: Put the "WithEvents" on the General Declaration of the form.


b) I looked up withevents in the help thingy and it said it could only be used in a class .

Answer: That's true, but no problem.  Form is a class ...

c) 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.

When I developed this class, I compiled it as Activex DLL.  To do it is necesary a public class.
When you add the classes into your project, it said Class1 can not be public in this type of porject, because your project is Standard EXE.

d) The item has been changed to private.
No problem.  In Standard EXE Projects the class must be private

More info: VB5 Help: Instancing Property


Good Luck

 
 
 


0
 
LVL 15

Expert Comment

by:Eyal
Comment Utility
CarlosJac I will be greatful if you can send me your class to.
My e-mail is eyal@magen.com.
Thanks in advance
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now