How do I send an email in Java?
Author: Deron Eriksson
Description: This Java tutorial describes sending an email in Java.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page: < 1 2

(Continued from page 1)

If we execute the EmailTest class, we see the following result in our console:

DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "cakelycakes.com", port 25, isSSL false
220 DERBIZZ.cakelycakes.com ESMTP MailEnable Service, Version: 1.981-- ready at 04/10/07 03:29:56
DEBUG SMTP: connected to host "cakelycakes.com", port: 25

EHLO DERBIZZ
250-cakelycakes.com [127.0.0.1], this server offers 4 extensions
250-AUTH LOGIN
250-SIZE 5120000
250-HELP
250 AUTH=LOGIN
DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
DEBUG SMTP: Found extension "SIZE", arg "5120000"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<me@here.there.everywhere>
250 Requested mail action okay, completed
RCPT TO:<bigcakes@cakelycakes.com>
250 Requested mail action okay, completed
RCPT TO:<littlecakes@cakelycakes.com>
250 Requested mail action okay, completed
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   bigcakes@cakelycakes.com
DEBUG SMTP:   littlecakes@cakelycakes.com
DATA
354 Please start mail input.
From: me@here.there.everywhere
To: bigcakes@cakelycakes.com
Cc: littlecakes@cakelycakes.com
Message-ID: <14982605.01176200996750.JavaMail.Der@DERBIZZ>
Subject: Email from Java
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is an email from Java
.
250 Mail queued for delivery.
QUIT
221 Closing connection. Good bye.

In MailEnable, if we refresh our Mailboxes window, we can see that emails to bigcakes and littlecakes were received by the email server, since the Inbox Messages counts are up to 1 for bigcakes and littlecakes.

MailEnable

If I double-click bigcakes, it brings up the 'Mailbox Properties' window. If I go to the Messages tab, I can see the list of email messages currently on the email server for bigcakes.

Mailbox Properties

If I double-click the email message, it brings up the email message in Notepad.

Email Message

This shows that we were successfully able to send email from our JavaSW EmailTest class to the MailEnable email server that we set up on our development machine.

Page: < 1 2