« SSH Using Java | Main | FTP Directory Listing Using Java »

March 09, 2008

Sending Email Using Java

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 }

  1. Lines 12-15.  Add necessary import statements.
  2. Lines 21-23.  Create new Smtp instance, add listener and enable debug mode.
  3. Lines 26-33.  Connect to SMTP server and create email message.
  4. Lines 36-39.  Send email message and disconnect.
  5. Lines 42-50.  Event handling methods for SmtpListener.

 

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e550fd7033883300e55133d7d68834

Listed below are links to weblogs that reference Sending Email Using Java:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.