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 ...
Get Java Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.