You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

fcassia

Beginner

  • "fcassia" started this thread

Posts: 1

Date of registration: Apr 17th 2011

  • Send private message

1

Sunday, April 17th 2011, 12:11pm

Jabaco app that sends email over SSL, how?

Hi there,

Being an old time Microsoft Basic PDS 7.x developer and also having used ReXX, VX-Rexx and CA-Realizer, I really like the idea of Jabaco.

However, I haven´t gotten past the "hello world" small tiny utilities when it comes to Java. This is to say, I only use the file I/O APIs in Java, rarely the Network ones.

So, if I have a Jabaco application that computes some result based on user input and must then e-mail the results to someone, how can I make Jabaco interface to JavaMail, and can it use SSL?.

Ideally, I´d like my Java app to establish a SSL session to SMTP.GMAIL.COM and send an email (supplying username and password).

If someone is experienced enough in Javamail and interfacing from Java to Google´s SSL-enabled SMTP servers, I´d really appreciate some hand-holding. It´s much better to learn by example than to pour several dozen complex APIs.

Thanks in advance.
FC

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Saturday, April 30th 2011, 7:54pm

Sending Google SSL Mails with Jabaco via JavaMail 1.4.3

The following is a small demo how to do this with JavaMail 1.4.3

The subroutine to send the mail:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Option Explicit
'
'  Send an Email via Gmail SMTP server using SSL connection.
'
'  inspired by
'  http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
'
'  the following library needs to be in the project classpath:
'  mail.jar     from javaMail 1.4.3
'

Import java#util#Properties
Import javax#mail#Message
Import javax#mail#MessagingException
Import javax#mail#PasswordAuthentication
Import javax#mail#Session
Import javax#mail#internet#InternetAddress
Import javax#mail#internet#MimeMessage
 
Private errMsg As String

Public Function getSendMailSSLError() As String
   getSendMailSSLError = errMsg
End Function


Public Function SendMailSSL(username As String, password As String, recipient As String, _
                                           fromAddress As String, mailSubject As String, bodyText As String) As Boolean 
   Dim props As Properties = New Properties()
   Dim session As Session 
   '  our helper class
   Dim authenticator As clsAuthenticator = New clsAuthenticator(username, password)
   Dim msg As MimeMessage 
   Dim recipientAddress As Address 
   Dim myAddress As Address
   
   On Error Goto ErrHandler
   
   Call props.put("mail.smtp.host", "smtp.gmail.com")
   Call props.put("mail.smtp.socketFactory.port", "465")
   Call props.put("mail.smtp.socketFactory.class", _
                         "javax.net.ssl.SSLSocketFactory")
   Call props.put("mail.smtp.auth", "true")
   Call props.put("mail.smtp.port", "465")
 
   session = javax#mail#Session.getDefaultInstance(props, authenticator)
        
   msg = New MimeMessage(session)
        
   recipientAddress = New InternetAddress(recipient)
   myAddress = New InternetAddress(fromAddress)

   '   
   '  set the message properties
  '
   Call msg.setFrom(myAddress)            
   '  for the time being we cannot add multiple recipient in one go
   '  because Jabaco is yet having problems with array parameters
   Call msg.addRecipient(javax#mail#Message$RecipientType.TO, recipientAddress)  '  note the $ sign!
   Call msg.setSubject(mailSubject)
   Call msg.setText(bodyText)
   
   '
   '  ready to send the mail
   '
   Call javax#mail#Transport.send(msg)

   errMsg = "OK"
   SendMailSSL = True
   Exit Function
   
 ErrHandler:
   errMsg = Err.getMessage()
   SendMailSSL = False 
End Function


A helper class is needed for username/password authentication:
(make sure to enter javax#mail#Authenticator as superclass in the Jabaco IDE for clsAuthenticator)

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Option Explicit

'
'  clsAuthenticator extends javax.mail.Authenticator
'

Private authentication As PasswordAuthentication 

'  a parameterized constructor
Public Sub clsAuthenticator(username As String, password As String) 
   authentication = New PasswordAuthentication(username, password)
End Sub

'  this ought be a protected function
'  but Jabaco does not swallow the "protected" here?
Public Function getPasswordAuthentication() As PasswordAuthentication
   getPasswordAuthentication = authentication
End Function


An example how to call our mailing subroutine:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Public Sub Command1_Click()
   Dim username As String = "a-user-name"
   Dim password As String = "a-password"        '  never store clear-text passwords in your code!
   Dim fromAddress As String = "xyz@googlemail.com"
   Dim recipient As String = "abc@def.com"
   Dim mailSubject As String = "a mail from Jabaco!"
   Dim bodyText As String = "now is the time" _
                                      & vbCrLf & "to come To the aid" _
                                      & vbCrLf & "of Jabaco"
   Dim ret As Boolean

   ret = SendMailSSL(username, password, recipient, fromAddress, mailSubject, bodyText)
   
   If ret Then
      MsgBox "OK! Mail sent!"
   Else
      MsgBox "Error sending Mail:" & vbCrLf & getSendMailSSLError()
   End If
End Sub


Be aware: This code is just a demo! It lacks proper error handling and is meant for experiments rather than for industrial usage.

Enjoy!

A1880

Posts: 5

Date of registration: Nov 21st 2012

  • Send private message

3

Wednesday, December 5th 2012, 9:27am

Hi A1880,

Hope all is well! I am trying to compile your gmail example, but I keep getting the following error:

class 'clsAuthenticator' not found...

Your assistance will be much appreciated.

Thanks
Elsebi

Rate this thread
WoltLab Burning Board