Tuesday, May 22nd 2012, 12:40am UTC+2

You are not logged in.

  • Login
  • Register

1

Tuesday, March 15th 2011, 4:53pm

Outlook On Jabaco Using JACOB

Hi everybody:

I'm new in Jabaco and I don't programming in Java. Sorry for any inconvenience.

I found this code in java that send emails by outlook using JACOB. But I don't know how can I convert some parts:

Code found in : http://www.guj.com.br/java/100403-outloo…lvido-com-jacob


ActiveXComponent axcOutlook = new ActiveXComponent("Outlook.Application");
Dispatch criacaoEmail =
Dispatch.invoke(axcOutlook.getObject(), "CreateItem", Dispatch.Get,
new Object[] { "0" }, new int[0]).toDispatch();

String destinatario = "dpostatni@gmail.com";
String assunto = "Teste Jacob";
String corpoMensagem = "Teste JACOB com anexo";
Object anexo1 = new Object();
Object anexo2 = new Object();
anexo1 = "C:/Teste1.txt";
anexo2 = "C:/Teste2.txt";

Dispatch.put(criacaoEmail, "To", destinatario);
Dispatch.put(criacaoEmail, "Subject", assunto);

Dispatch.put(criacaoEmail, "Body", corpoMensagem);
Dispatch.put(criacaoEmail, "ReadReceiptRequested", "false");

Dispatch attachs = Dispatch.get(criacaoEmail, "Attachments").toDispatch();
Dispatch.call(attachs, "Add", anexo1);
Dispatch.call(attachs, "Add", anexo2);
Dispatch.call(criacaoEmail, "Display");


Thanks in Advance and sorry my poor english
Joe Kaisaka

A1880

Intermediate

Posts: 500

Location: Hanover, Germany

Occupation: Software Engineer

2

Tuesday, March 15th 2011, 5:11pm

This code snippet is using Outlook as ActiveX control. This is not directly feasible in Jabaco.
Jabaco does not offer direct support for ActiveX.
JACOB (= Java COM Bridge) can be linked into Jabaco programs.
Use the search facility of this forum to find a JACOB/Jabaco sample.

Another option would be to use a Java-based mailing framework.

Please post your findings!

A1880

3

Tuesday, March 15th 2011, 6:19pm

A1880:

Thanks for your reply. You are a very active member of this community and I'm happy because you read and answer my single post.

I know the code that I post is java + jacob and jabaco doesn't offer direct support of ActiveX. But reading the great post of Stefan Schnell [How to use JavaCOMBridge (JaCoB) with Microsoft Word] I was thinking to try convert the code, but I don't know how can I convert some parts of code (red painted).

I need send emails using outlook and I think to make this in jabaco to learn more about this powerful tool.

Thanks in Advance

A1880

Intermediate

Posts: 500

Location: Hanover, Germany

Occupation: Software Engineer

4

Wednesday, March 16th 2011, 9:02am

As far as I know, the red snippets cannot be migrated to Jabaco.
The reason is that Jabaco does not support passing Java arrays as parameters when calling a method.

One could write the code in question in Java, export it into a Jar file and import this into the Jabaco project.

To simply send mails from a Jabaco application, you could use an external tool like blat
Using the "Interaction.Shell()" method, such external tools can be started as subprocess of your application.

Have fun!

A1880

5

Thursday, March 17th 2011, 11:58am

My Friends :

I made this. This code works for me.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Public Sub JSendEmailFromOutlook( cTo As String, cSubject As String, cBody As String, Optional arrAttachments() As String, Optional lReadReceiptConfirmation As Boolean = False)
Dim oOutlook As com#jacob#activeX#ActiveXComponent 
Dim oOL As com#jacob#com#Dispatch 
Dim oNs As com#jacob#com#Dispatch
Dim oMail As com#jacob#com#Dispatch
Dim oAttachs As com#jacob#com#Dispatch
Dim mapi(0)As Object
Dim email(0) As Object
Dim nAttach As Integer
Dim attachment(0) As Object

mapi(0) = "MAPI"
email(0) = 0
Set oOutlook = New ActiveXComponent("Outlook.Application")
oOL = oOutlook.getObject()
oNs = com#jacob#com#Dispatch.call(oOL,"GetNamespace",mapi).toDispatch
oMail = com#jacob#com#Dispatch.call(oOutlook,"CreateItem",email).toDispatch

com#jacob#com#Dispatch.put(oMail, "To", cTo) 
com#jacob#com#Dispatch.put(oMail, "Subject", cSubject) 
com#jacob#com#Dispatch.put(oMail, "Body", cBody) 
com#jacob#com#Dispatch.put(oMail, "ReadReceiptRequested", lReadReceiptConfirmation)

If Ubound(arrAttachments) > 0 Then
	oAttachs = com#jacob#com#Dispatch.get(oMail, "Attachments").toDispatch() 
	For nAttach = 0 To Ubound(arrAttachments) 
		 attachment(0) = arrAttachments(nAttach) 
		 com#jacob#com#Dispatch.call(oAttachs, "Add", attachment)
	Next nAttach
End If 

com#jacob#com#Dispatch.call(oMail, "Send")

End Sub


Thanks for the patience.


PS : Tested only Outlook 2010. I will make tests in others Outlook Versions

This post has been edited 1 times, last edit by "joekaisaka" (Mar 17th 2011, 12:17pm)


6

Thursday, March 17th 2011, 3:03pm

Hi Folks:

I made some changes and fix some bugs. Hope you enjoy.

New features:

- CC propery
- BCC property
- Importance property
- Close Outlook after the mail is sent

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Public Sub JSendEmailFromOutlook( cTo As String, Optional cSubject As String = "", Optional cBody As String = "", Optional cCC As String = "", Optional cBCC As String = "", Optional arrAttachments() As String, Optional lReadReceiptConfirmation As Boolean = False, Optional nImportance As Integer = 0)

Dim oOutlook As com#jacob#activeX#ActiveXComponent 
Dim oOL As com#jacob#com#Dispatch 
Dim oNs As com#jacob#com#Dispatch
Dim oMail As com#jacob#com#Dispatch
Dim oAttachs As com#jacob#com#Dispatch
Dim mapi(0)As Object
Dim email(0) As Object
Dim nAttach As Integer
Dim attachment(0) As Object

mapi(0) = "MAPI"
email(0) = 0

Set oOutlook = New ActiveXComponent("Outlook.Application")
oOL = oOutlook.getObject()
oNs = com#jacob#com#Dispatch.call(oOL,"GetNamespace",mapi).toDispatch
oMail = com#jacob#com#Dispatch.call(oOutlook,"CreateItem",email).toDispatch

com#jacob#com#Dispatch.put(oMail, "To", cTo) 
com#jacob#com#Dispatch.put(oMail, "Subject", cSubject) 

If Len(Trim(cCC)) > 0 Then
      com#jacob#com#Dispatch.put(oMail, "CC", cCC)
End If      

If Len(Trim(cBCC)) > 0 Then
      com#jacob#com#Dispatch.put(oMail, "BCC", cBCC)
End If      
       
com#jacob#com#Dispatch.put(oMail, "Body", cBody) 
com#jacob#com#Dispatch.put(oMail, "ReadReceiptRequested", lReadReceiptConfirmation)
com#jacob#com#Dispatch.put(oMail, "Importance", nImportance) 

If Ubound(arrAttachments) > 0 Then

   oAttachs = com#jacob#com#Dispatch.get(oMail, "Attachments").toDispatch() 
   
   For nAttach = 0 To Ubound(arrAttachments)
      attachment(0) = arrAttachments(nAttach)
      com#jacob#com#Dispatch.call(oAttachs, "Add", attachment)
   Next nAttach
   
End If   

com#jacob#com#Dispatch.call(oMail, "Send")
com#jacob#com#ComThread.Release
   
End Sub


I know I missing some cool things like error handling. Also have not tested the image in the message body. But did the best I could.
I thank you all.

Sorry the poor english,

Joe Kaisaka

A1880

Intermediate

Posts: 500

Location: Hanover, Germany

Occupation: Software Engineer

7

Thursday, March 17th 2011, 3:05pm

Great!

I tried it with Outlook 2007. It worked without problems.

A slightly reformatted version which makes use of the import statement:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Option Explicit

Import com#jacob#activeX 
Import com#jacob#com 

Public Sub JSendEmailFromOutlook( cTo As String, cSubject As String, cBody As String, Optional arrAttachments() As String, Optional lReadReceiptConfirmation As Boolean = False)
   Dim oOutlook As ActiveXComponent 
   Dim oOL As Dispatch 
   Dim oNs As Dispatch
   Dim oMail As Dispatch
   Dim oAttachs As Dispatch
   Dim mapi(0)As Object
   Dim email(0) As Object
   Dim nAttach As Integer
   Dim attachment(0) As Object

   mapi(0) = "MAPI"
   email(0) = 0
   Set oOutlook = New ActiveXComponent("Outlook.Application")
   oOL = oOutlook.getObject()
   oNs = Dispatch.call(oOL,"GetNamespace",mapi).toDispatch
   oMail = Dispatch.call(oOutlook,"CreateItem",email).toDispatch

   Dispatch.put(oMail, "To", cTo) 
   Dispatch.put(oMail, "Subject", cSubject) 
   Dispatch.put(oMail, "Body", cBody) 
   Dispatch.put(oMail, "ReadReceiptRequested", lReadReceiptConfirmation)

   If Ubound(arrAttachments) > 0 Then
      oAttachs = Dispatch.get(oMail, "Attachments").toDispatch() 
      For nAttach = 0 To Ubound(arrAttachments) 
          attachment(0) = arrAttachments(nAttach) 
          Dispatch.call(oAttachs, "Add", attachment)
      Next nAttach
   End If 

   Dispatch.call(oMail, "Send")

End Sub

Public Sub Command1_Click()
   JSendEmailFromOutlook "xyz@myDomain.org", "Test", "a small" & vbCrLf & "Test"
End Sub


As soon as the project is actually saved in a project directory, the Jacob native dll has to be on the library search path.
I've put it into the project directory.

I was wrong assuming that Jabaco could not pass genuine Java arrays when calling a method.
Arrays of Objects are properly passed.
I tried arrays of char without success.

Greetings!

A1880

Rate this thread
WoltLab Burning Board