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

JSCAPE Secure FTP Server 5.2 Released
06/03/2009 12:04 PM

JSCAPE Web Document Viewer Released
06/03/2009 12:02 PM

JSCAPE Secure FTP Server Plugin for Outlook Released
06/03/2009 10:03 AM

Secure iNet Factory 8.1.0 Released
04/22/2009 09:53 AM

Secure FTP Factory 8.1.0 Released
04/22/2009 08:40 AM

Secure FTP Applet 5.5 Released
04/07/2009 03:00 PM

more...


Tutorials

Enabling Phone Authentication
04/08/2009 11:24 AM

Detecting and Handling Brute Force Password Attacks
01/29/2009 09:44 AM

Creating a Domain
12/15/2008 11:33 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Formatting MimeMessages using .NET
09/14/2008 04:31 PM

Communicating with an IMAP4 server in .NET
09/14/2008 03:54 PM

more...


Articles

Using custom forms to automate business processes
07/03/2009 08:51 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Best Practices for Configuring Your FTP Server
06/03/2008 04:47 PM

What is the difference between passive and active FTP?
05/20/2008 09:27 AM

What is the difference between FTP, FTPS and SFTP?
05/19/2008 04:29 PM

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

more...


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.