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. Download
a FREE 30 day Secure iNet Factory Evaluation
SMTP (Simple Mail Transfer Protocol)
The SMTP protocol is the standard used by mail servers for sending
and receiving email. The SMTP protocol is published under RFC 821.
Details of this protocol are beyond the scope of this article. For
more information on the SMTP protocol go to http://www.faqs.org/rfcs/rfc821.html
MIME (Multipurpose Internet Mail Extensions)
The MIME protocol is the standard used when composing email messages.
The MIME protocol is published under RFC 1521. Details of this protocol
are beyond the scope of this article. For more information on the
MIME protocol go to http://www.faqs.org/rfcs/rfc1521.html
Importing the necessary components
In order to send electronic mail you must ensure that the following
import statements are included in your Java code. The com.jscape.inet.smtp
package contains the necessary classes for communicating with an
SMTP server. The com.jscape.inet.email package contains the necessary
classes for generating a MIME compliant email message.
import com.jscape.inet.smtp.*;
Import com.jscape.inet.email.*;
Establishing a Connection
In order to send email you must first establish a network connection
to your SMTP server. If you are unsure of the hostname of your SMTP
server please contact your system administrator.
// create a new Smtp instance using SMTP hostname as argument
Smtp smtp = new Smtp("smtp.here.com");
// establish connection
smtp.connect();
Composing the Email
Next is to compose the MIME compliant email message you wish to
send.
// Create a new EmailMessage instance
EmailMessage message = new EmailMessage();
// set From address
message.setFrom("me@here.com");
// set To address
message.setTo("someone@somewhere.com");
// set subject of message
message.setSubject("Hello!");
// set body of message
message.setBody("Have a nice day");
In the event you wish to have multiple recipients you may use the
setCc method. This will carbon-copy the message to any recipients
you define here.
Example
// set carbon-copy recipients
message.setCc("someoneelse@somewhere.com");
Sending the Email
Once you have successfully established a connection and composed
the email message sending the email is simple.
// send the email message
smtp.send(message);
Releasing the Connection
Once you have finished sending your email message(s) it is important
that you disconnect from the SMTP server. This is easily accomplished
using the code below.
// release SMTP server connection
smtp.disconnect();
In this article you have learned the very basics needed to send
email using Java. The SMTP and MIME components provided in iNet
Factory make this easy removing the need to understand all the complexities
of SMTP and MIME protocols. To see what else Secure iNet Factory has to
offer Download a FREE 30 day Secure iNet
Factory Evaluation.
|