O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending Email: For Real

Problem

You need to send email, and the browser trick in Section 19.2 won’t cut it.

Solution

Provide a real email client.

Discussion

A real email client allows the user considerably more control. Of course, it also requires more work. In this recipe I’ll build a simple version of a mail sender, relying upon the JavaMail standard extension in package javax.mail and javax.mail.internet (the latter contains classes that are specific to Internet email protocols). This first example shows the steps of sending mail over SMTP, the standard Internet mail protocol. The steps are listed in the sidebar.

As is usual in Java, you must catch certain exceptions. This API requires that you catch the MessagingException , which indicates some failure of the transmission. Class Sender is shown in Example 19-3.

Example 19-3. Sender.java

import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.internet.*; /** sender -- send an email message. */ public class Sender { /** The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required