Java SMTP, Java email, sending email using Java
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

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();

Summary

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.