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

iTunes Gift Card Offer

For a limited time get a $50.00 iTunes gift card free with qualifying purchase.

Click for details.


News

JSCAPE Secure FTP Server 7.0 Released
08/20/2010 03:31 AM

JSCAPE launches Employee Giving Program
07/03/2010 08:26 AM

SSH Factory 3.6 Released
07/03/2010 06:22 AM

Secure iNet Factory 8.5 Released
07/03/2010 06:07 AM

Secure FTP Factory 8.5 Released
07/03/2010 06:03 AM

Secure FTP Applet 6.2 Released
06/10/2010 02:23 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

Open up corporate data to your partners
08/03/2010 10:01 PM

Access vital corporate documents on the go
07/01/2010 02:15 PM

SFTP and Encryption
05/17/2010 09:52 PM

Streamlining web uploads with ZIP archives
12/14/2009 10:11 AM

Using regular expressions in complex trigger conditions
09/08/2009 07:42 AM

Using custom forms to automate business processes
07/03/2009 08:51 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.