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