|
The free Google email service Gmail offers over 2GB of storage space prompting many people to signup for accounts. In addition to providing POP access, Gmail also provides remote SMTP access allowing you to send mail using the Gmail SMTP server.
In order to prevent spam and provide increased security the Gmail SMTP server requires that you connect using SMTP SSL and login using your account credentials prior to sending any mail.
The purpose of this article is to demonstrate how to securely connect to the Gmail SMTP server and send mail using the Java com.jscape.inet.smtpssl.SmtpSsl class found in Secure iNet Factory.
The procedures in this article may also apply to other SMTP servers which provide secure SSL/TLS access.
For a tutorial on retrieving email from Gmail see Retrieving email securely from Gmail using POP SSL.
- A Gmail account. As of this writing this service is still in beta. If you do not have an account you must get invited to open an account from someone who already has one.
- JDK 1.2.2 or above and Secure iNet Factory
Note
If using a JDK prior to JDK 1.4 then you must download and install the JSSE (Java Secure Socket Extensions). Instructions for doing this are provided in the Secure iNet Factory User Guide.
01 import com.jscape.inet.smtpssl.*;
02 import com.jscape.inet.email.*;
03
04 public class SmtpGmail {
05
06 public static void main(String[] args) {
07 SmtpSsl smtp = null;
08
09 // gmail username - CHANGE THIS
10 String username = "username@gmail.com";
11
12 // gmail password - CHANGE THIS
13 String password = "password";
14
15 // address to send mail to - CHANGE THIS
16 String to = "user@domain.com";
17 try {
18 // create a new SmtpSsl instance connecting securely via port 465 using implicit SSL
19 smtp = new SmtpSsl("smtp.gmail.com",465);
20
21 // establish secure connection
22 smtp.connect();
23
24 // login using gmail account details
25 smtp.login(username,password);
26
27 // create new email message
28 EmailMessage message = new EmailMessage();
29 message.setTo(to);
30 message.setFrom(username);
31 message.setSubject("Sending email via Gmail SMTP");
32 message.setBody("This is the body of the message");
33
34 // send message
35 smtp.send(message);
36
37 // disconnect
38 smtp.disconnect();
39 } catch(Exception e) {
40 // capture any exception and print to console
41 e.printStackTrace();
42 }
43 }
44 }
- Lines 1-2. Import necessary classes.
- Lines 7-16. Define SmtpSsl member variable, Gmail account information and address to send message to.
- Line 19. Create new SmtpSsl instance.
- Line 22. Establish secure connection to Gmail SMTP server
- Line 25. Login using Gmail account credentials.
- Lines 28-32. Create email message.
- Line 35. Send message.
- Line 38. Disconnect from Gmail SMTP server.
In this article you learned how to securely send email via the Gmail SMTP server using the secure SMTP classes provided in Secure iNet Factory.
The procedures in this article may also apply to other SMTP servers which provide secure SSL/TLS access.
|