Overview
A common need across many software applications is the ability to send email messages. Fortunately, virtually every email sent today are sent using two popular protocols known as SMTP (Simple Mail Transfer Protocol) and MIME (Multipurpose Internet Mail Extensions). Using the Java based SMTP and MIME components provided in Secure iNet Factory this article will demonstrate how you can easily embed email capabilities into your own Java applications.
The SMTP (Simple Mail Transport Protocol) protocol is the standard used by mail servers for sending and receiving email. The SMTP protocol is published under RFC 821. The MIME (Multipurpose Internet Mail Extensions) is the standard used when composing email messages. The MIME protocol is published under RFC 1521.
Code Example
Download SmtpExample.java (2.6K)
01 /*
02 * @(#)SmtpExample.java
03 *
04 * Copyright (c) JSCAPE LLC.
05 *
06 * This software is the confidential and proprietary information of
07 * JScape. ("Confidential Information"). You shall not disclose such
08 * Confidential Information and shall use it only in accordance with
09 * the terms of the license agreement you entered into with JScape.
10 */
11
12 import com.jscape.inet.smtp.*;
13 import com.jscape.inet.email.*;
14 import com.jscape.inet.mime.MimeException;
15 import java.io.*;
16
17 public class SmtpExample extends SmtpAdapter {
18 public void sendMessage(String hostname, String to, String from, String subject, String body) throws SmtpException,
19 MimeException {
20 // create instance and add listener
21 Smtp smtp = new Smtp(hostname);
22 smtp.addSmtpListener(this);
23 smtp.setDebug(true);
24
25 // connect to mail server
26 smtp.connect();
27
28 // create email message
29 EmailMessage email = new EmailMessage();
30 email.setTo(to);
31 email.setFrom(from);
32 email.setSubject(subject);
33 email.setBody(body);
34
35 // send email message
36 smtp.send(email);
37
38 // disconnect from server
39 smtp.disconnect();
40 }
41
42 // capture connect event
43 public void connected(SmtpConnectedEvent evt) {
44 System.out.println("Connected to SMTP server: " + evt.getHostname());
45 }
46
47 // capture disconnect event
48 public void disconnected(SmtpDisconnectedEvent evt) {
49 System.out.println("Disconnected from SMTP server: " + evt.getHostname());
50 }
51
52
53 public static void main(String[] args) {
54 String hostname;
55 String to;
56 String from;
57 String subject;
58 String body;
59 try {
60 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
61 System.out.print("Enter Smtp hostname (e.g. mail.domain.com): ");
62 hostname = reader.readLine();
63 System.out.print("Enter To address (e.g. recipient@domain.com): ");
64 to = reader.readLine();
65 System.out.print("Enter From address (e.g. sender@domain.com): ");
66 from = reader.readLine();
67 System.out.print("Enter Subject (e.g. Hello World): ");
68 subject = reader.readLine();
69 System.out.print("Enter Message Body (e.g. Just saying hi!): ");
70 body = reader.readLine();
71 SmtpExample example = new SmtpExample();
72 example.sendMessage(hostname,to,from,subject,body);
73 }
74 catch(Exception e) {
75 e.printStackTrace();
76 }
77 }
78 }
- Lines 12-15. Add necessary import statements.
- Lines 21-23. Create new Smtp instance, add listener and enable debug mode.
- Lines 26-33. Connect to SMTP server and create email message.
- Lines 36-39. Send email message and disconnect.
- Lines 42-50. Event handling methods for SmtpListener.
Comments