社区
Java
帖子详情
安装java的jdk时 要求我关闭什么java platform SE binary(Process Id : 5240) 怎么解决啊
qq_31054971
2016-08-01 10:42:35
电脑里突然出现这种安装问题,在网上搜索的方法也没用。
...全文
4428
9
打赏
收藏
安装java的jdk时 要求我关闭什么java platform SE binary(Process Id : 5240) 怎么解决啊
电脑里突然出现这种安装问题,在网上搜索的方法也没用。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
MelodyTiger98
2019-10-19
打赏
举报
回复
2
引用 4 楼 家里敷泥呀 的回复:
图片放错了,下面这个图
按照这个操作成功解决该问题了,感谢
LIUXINGUANG2013
2019-02-15
打赏
举报
回复
我按照网友的操作搞得不行啊,你是怎么做的?
qq_31054971
2018-02-02
打赏
举报
回复
谢谢大家,已经解决了
qq_20064355
2016-09-25
打赏
举报
回复
把安装路径改成英文试试,我是这样成功的
阳光越来越暖
2016-08-01
打赏
举报
回复
看看你任务进程里面有这个嘛 有就关闭它 你以前安装过 没卸载干净?
qq_31054971
2016-08-01
打赏
举报
回复
那个错误ID 每一次都不一样。所以很困惑!
家里敷泥呀
2016-08-01
打赏
举报
回复
图片放错了,下面这个图
家里敷泥呀
2016-08-01
打赏
举报
回复
如上图,通过任务管理器关闭掉5240这个进程。
Java
邮件开发Fundamentals of the
Java
Mail API
Fundamentals of the
Java
Mail API Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Table of Contents If you're viewing this document online, you can click any of the topics below to link directly to that
se
ction. 1. Tutorial tips 2 2. Introducing the
Java
Mail API 3 3. Reviewing related protocols 4 4. Installing
Java
Mail 6 5. Reviewing the core clas
se
s 8 6. Using the
Java
Mail API 13 7.
Se
arching with
Se
archTerm 21 8. Exerci
se
s 22 9. Wrapup 32 Fundamentals of the
Java
Mail API Page 1 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 1. Tutorial tips Should I take this tutorial? Looking to incorporate mail facilities into your
platform
-independent
Java
solutions? Look no further than the
Java
Mail API, which offers a protocol-independent model for working with IMAP, POP, SMTP, MIME, and all tho
se
other Internet-related messaging protocols. With the help of the
Java
Beans Activation Framework (JAF), your applications can now be mail-enabled through the
Java
Mail API. Concepts After completing this module you will understand the: * Basics of the Internet mail protocols SMTP, POP3, IMAP, and MIME * Architecture of the
Java
Mail framework * Connections between the
Java
Mail API and the
Java
Beans Activation Framework Objectives By the end of this module you will be able to: *
Se
nd and read mail using the
Java
Mail API * Deal with
se
nding and receiving attachments * Work with HTML messages * U
se
se
arch terms to
se
arch for messages Prerequisites Instructions on how to download and install the
Java
Mail API are contained in the cour
se
. In addition, you will need a development environment such as the
JDK
1.1.6+ or the
Java
2
Platform
, Standard Edition (J2
SE
) 1.2.x or 1.3.x. A general familiarity with object-oriented programming concepts and the
Java
programming language is necessary. The
Java
language es
se
ntials tutorial can help. copyright 1996-2000 Magelang Institute dba jGuru Contact jGuru has been dedicated to promoting the growth of the
Java
technology community through evangelism, education, and software since 1995. You can find out more about their activities, including their huge collection of FAQs at jGuru.com . To
se
nd feedback to jGuru about this cour
se
,
se
nd mail to producer@jguru.com . Cour
se
author: Formerly with jGuru.com , John Zukowski does strategic
Java
consulting for JZ Ventures, Inc. His latest book is titled
Java
Collections from Apress . Fundamentals of the
Java
Mail API Page 2 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 2. Introducing the
Java
Mail API What is the
Java
Mail API? The
Java
Mail API is an optional package (standard extension) for reading, composing, and
se
nding electronic messages. You u
se
the package to create Mail U
se
r Agent (MUA) type programs, similar to Eudora, pine, and Microsoft Outlook. The API's main purpo
se
is not for transporting, delivering, and forwarding messages; this is the purview of applications such as
se
ndmail and other Mail Transfer Agent (MTA) type programs. MUA-type programs let u
se
rs read and write e-mail, whereas MUAs rely on MTAs to handle the actual delivery. The
Java
Mail API is designed to prov
id
e protocol-independent access for
se
nding and receiving messages by div
id
ing the API into two parts: * The first part of the API is the focus of this cour
se
--basically, how to
se
nd and receive messages independent of the prov
id
er/protocol. * The
se
cond part speaks the protocol-specific languages, like SMTP, POP, IMAP, and NNTP. With the
Java
Mail API, in order to communicate with a
se
rver, you need a prov
id
er for a protocol. The creation of protocol-specific prov
id
ers is not covered in this cour
se
becau
se
Sun prov
id
es a sufficient
se
t for free. Fundamentals of the
Java
Mail API Page 3 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 3. Reviewing related protocols Introduction Before looking into the
Java
Mail API specifics, let's step back and take a look at the protocols u
se
d with the API. There are basically four that you'll come to know and love: * SMTP * POP * IMAP * MIME You will also run across NNTP and some others. Understanding the basics of all the protocols will help you understand how to u
se
the
Java
Mail API. While the API is designed to be protocol agnostic, you can't overcome the limitations of the underlying protocols. If a capability isn't supported by a cho
se
n protocol, the
Java
Mail API doesn't magically add the capability on top of it. (As you'll soon
se
e, this can be a problem when working with POP.) SMTP The Simple Mail Transfer Protocol (SMTP) is defined by RFC 821 . It defines the mechanism for delivery of e-mail. In the context of the
Java
Mail API, your
Java
Mail-ba
se
d program will communicate with your company or Internet
Se
rvice Prov
id
er's (ISP's) SMTP
se
rver. That SMTP
se
rver will relay the message on to the SMTP
se
rver of the recipient(s) to eventually be acquired by the u
se
r(s) through POP or IMAP. This does not require your SMTP
se
rver to be an open relay, as authentication is supported, but it is your responsibility to ensure the SMTP
se
rver is configured properly. There is nothing in the
Java
Mail API for tasks like configuring a
se
rver to relay messages or to add and remove e-mail accounts. POP POP stands for Post Office Protocol. Currently in version 3, also known as POP3, RFC 1939 defines this protocol. POP is the mechanism most people on the Internet u
se
to get their mail. It defines support for a single mailbox for each u
se
r. That is all it does, and that is also the source of a lot of confusion. Much of what people are familiar with when using POP, like the ability to
se
e how many new mail messages they have, are not supported by POP at all. The
se
capabilities are built into programs like Eudora or Microsoft Outlook, which remember things like the last mail received and calculate how many are new for you. So, when using the
Java
Mail API, if you want this type of information, you have to calculate it your
se
lf. IMAP IMAP is a more advanced protocol for receiving messages. Defined in RFC 2060 , IMAP stands for Internet Message Access Protocol, and is currently in version 4, also known as IMAP4. When using IMAP, your mail
se
rver must support the protocol. You can't just change your program to u
se
IMAP instead of POP and expect everything in IMAP to be supported. Assuming your mail
se
rver supports IMAP, your
Java
Mail-ba
se
d program can take Fundamentals of the
Java
Mail API Page 4 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks advantage of u
se
rs having multiple folders on the
se
rver and the
se
folders can be shared by multiple u
se
rs. Due to the more advanced capabilities, you might think IMAP would be u
se
d by everyone. It isn't. It places a much heavier burden on the mail
se
rver, requiring the
se
rver to receive the new messages, deliver them to u
se
rs when requested, and maintain them in multiple folders for each u
se
r. While this does centralize backups, as u
se
rs' long-term mail folders get larger and larger, everyone suffers when disk space is exhausted. With POP, saved messages get offloaded from the mail
se
rver. MIME MIME stands for Multipurpo
se
Internet Mail Extensions. It is not a mail transfer protocol. Instead, it defines the content of what is transferred: the format of the messages, attachments, and so on. There are many different documents that take effect here: RFC 822 , RFC 2045 , RFC 2046 , and RFC 2047 . As a u
se
r of the
Java
Mail API, you usually don't need to worry about the
se
formats. However, the
se
formats do exist and are u
se
d by your programs. NNTP and others Becau
se
of the split of the
Java
Mail API between prov
id
er and everything el
se
, you can easily add support for additional protocols. Sun maintains a list of third-party prov
id
ers that take advantage of protocols for which Sun does not prov
id
e out-of-the-box support. You'll find support for NNTP (Network News Transport Protocol) [newsgroups], S/MIME (
Se
cure Multipurpo
se
Internet Mail Extensions), and more. Fundamentals of the
Java
Mail API Page 5 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 4. Installing
Java
Mail Introduction There are two versions of the
Java
Mail API commonly u
se
d today: 1.2 and 1.1.3. All the examples in this cour
se
will work with both. While 1.2 is the latest, 1.1.3 is the version included with the 1.2.1 version of the
Java
2
Platform
, Enterpri
se
Edition (J2EE), so it is still commonly u
se
d. The version of the
Java
Mail API you want to u
se
affects what you download and install. All will work with
JDK
1.1.6+,
Java
2
Platform
, Standard Edition (J2
SE
) version 1.2.x, and J2
SE
version 1.3.x. Note: After installing Sun's
Java
Mail implementation, you can find many example programs in the demo directory. Installing
Java
Mail 1.2 To u
se
the
Java
Mail 1.2 API, download the
Java
Mail 1.2 implementation, unbundle the
java
mail-1_2.zip file, and add the mail.jar file to your CLASSPATH. The 1.2 implementation comes with an SMTP, IMAP4, and POP3 prov
id
er bes
id
es the core clas
se
s. After installing
Java
Mail 1.2, install the
Java
Beans Activation Framework. Installing
Java
Mail 1.1.3 To u
se
the
Java
Mail 1.1.3 API, download the
Java
Mail 1.1.3 implementation, unbundle the
java
mail1_1_3.zip file, and add the mail.jar file to your CLASSPATH. The 1.1.3 implementation comes with an SMTP and IMAP4 prov
id
er, bes
id
es the core clas
se
s. If you want to access a POP
se
rver with
Java
Mail 1.1.3, download and install a POP3 prov
id
er. Sun has one available
se
parate from the
Java
Mail implementation. After downloading and unbundling pop31_1_1.zip, add pop3.jar to your CLASSPATH, too. After installing
Java
Mail 1.1.3, install the
Java
Beans Activation Framework. Installing the
Java
Beans Activation Framework All versions of the
Java
Mail API require the
Java
Beans Activation Framework. The framework adds support for typing arbitrary blocks of data and handling it accordingly. This doesn't sound like much, but it is your basic MIME-type support found in many brow
se
rs and mail tools today. After downloading the framework, unbundle the jaf1_0_1.zip file, and add the activation.jar file to your CLASSPATH. For
Java
Mail 1.2 u
se
rs, you should now have added mail.jar and activation.jar to your CLASSPATH. For
Java
Mail 1.1.3 u
se
rs, you should now have added mail.jar, pop3.jar, and activation.jar to your CLASSPATH. If you have no plans of using POP3, you don't Fundamentals of the
Java
Mail API Page 6 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks need to add pop3.jar to your CLASSPATH. If you don't want to change the CLASSPATH environment variable, copy the jar files to your lib/ext directory under the
Java
Runtime Environment (JRE) directory. For instance, for the J2
SE
1.3 relea
se
, the default directory would be C:\
jdk
1.3\jre\lib\ext on a Windows
platform
. Using
Java
Mail with the
Java
2 Enterpri
se
Edition If you u
se
J2EE, there is nothing special you have to do to u
se
the basic
Java
Mail API; it comes with the J2EE clas
se
s. Just make sure the j2ee.jar file is in your CLASSPATH and you're all
se
t. For J2EE 1.2.1, the POP3 prov
id
er comes
se
parately, so download and follow the steps to include the POP3 prov
id
er as shown in the previous
se
ction "Installing
Java
Mail 1.1.3." J2EE 1.3 u
se
rs get the POP3 prov
id
er with J2EE so do not require the
se
parate installation. Neither installation requires you to install the
Java
Beans Activation Framework. Exerci
se
Exerci
se
1. How to
se
t up a
Java
Mail environment on page 22 Fundamentals of the
Java
Mail API Page 7 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 5. Reviewing the core clas
se
s Introduction Before taking a how-to approach at looking at the
Java
Mail clas
se
s in depth, this
se
ction walks you through the core clas
se
s that make up the API:
Se
ssion, Message, Address, Authenticator, Transport, Store, and Folder. All the
se
clas
se
s are found in the top-level package for the
Java
Mail API,
java
x.mail, though you'll frequently find your
se
lf using subclas
se
s found in the
java
x.mail.internet package.
Se
ssion The
Se
ssion class defines a basic mail
se
ssion. It is through this
se
ssion that everything el
se
works. The
Se
ssion object takes advantage of a
java
.util.Properties object to get information like mail
se
rver, u
se
rname, password, and other information that can be shared across your entire application. The constructors for the class are private. You can get a single default
se
ssion that can be shared with the getDefaultInstance() method: Properties props = new Properties(); // fill props with any information
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, null); Or, you can create a unique
se
ssion with getInstance(): Properties props = new Properties(); // fill props with any information
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, null); In both ca
se
s, the null argument is an Authenticator object that is not being u
se
d at this time. In most ca
se
s, it is sufficient to u
se
the shared
se
ssion, even if working with mail
se
ssions for multiple u
se
r mailboxes. You can add the u
se
rname and password combination in at a later step in the communication
process
, keeping everything
se
parate. Message Once you have your
Se
ssion object, it is time to move on to creating the message to
se
nd. This is done with a type of Message . Becau
se
Message is an abstract class, you must work with a subclass, in most ca
se
s
java
x.mail.internet.MimeMessage .A MimeMessage is an e-mail message that understands MIME types and headers, as defined in the different RFCs. Message headers are restricted to US-ASCII characters only, though non-ASCII characters can be encoded in certain header fields. To create a Message, pass along the
Se
ssion object to the MimeMessage constructor: MimeMessage message = new MimeMessage(
se
ssion); Fundamentals of the
Java
Mail API Page 8 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Note: There are other constructors, like for creating messages from RFC822-formatted input streams. Once you have your message, you can
se
t its parts, as Message implements the Part interface (with MimeMessage implementing MimePart ). The basic mechanism to
se
t the content is the
se
tContent() method, with arguments for the content and the mime type: message.
se
tContent("Hello", "text/plain"); If, however, you know you are working with a MimeMessage and your message is plain text, you can u
se
its
se
tText() method, which only requires the actual content, defaulting to the MIME type of text/plain: message.
se
tText("Hello"); For plain text messages, the latter form is the preferred mechanism to
se
t the content. For
se
nding other kinds of messages, like HTML messages, u
se
the former. For
se
tting the subject, u
se
the
se
tSubject() method: message.
se
tSubject("First"); Address Once you've created the
Se
ssion and the Message, as well as filled the message with content, it is time to address your letter with an Address . Like Message, Address is an abstract class. You u
se
the
java
x.mail.internet.InternetAddress class. To create an address with just the e-mail address, pass the e-mail address to the constructor: Address address = new InternetAddress("pres
id
ent@whitehou
se
.gov"); If you want a name to appear next to the e-mail address, you can pass that along to the constructor, too: Address address = new InternetAddress("pres
id
ent@whitehou
se
.gov", "George Bush"); You will need to create address objects for the message's from field as well as the to field. Unless your mail
se
rver prevents you, there is nothing stopping you from
se
nding a message that appears to be from anyone. Once you've created the addres
se
s, you connect them to a message in one of two ways. For
id
entifying the
se
nder, you u
se
the
se
tFrom() and
se
tReplyTo() methods. message.
se
tFrom(address) If your message needs to show multiple from addres
se
s, u
se
the addFrom() method: Fundamentals of the
Java
Mail API Page 9 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Address address[] = ...; message.addFrom(address); For
id
entifying the message recipients, you u
se
the addRecipient() method. This method requires a Message.RecipientType bes
id
es the address. message.addRecipient(type, address) The three predefined types of address are: * Message.RecipientType.TO * Message.RecipientType.CC * Message.RecipientType.BCC So, if the message was to go to the vice pres
id
ent,
se
nding a carbon copy to the first lady, the following would be appropriate: Address toAddress = new InternetAddress("vice.pres
id
ent@whitehou
se
.gov"); Address ccAddress = new InternetAddress("first.lady@whitehou
se
.gov"); message.addRecipient(Message.RecipientType.TO, toAddress); message.addRecipient(Message.RecipientType.CC, ccAddress); The
Java
Mail API prov
id
es no mechanism to check for the val
id
ity of an e-mail address. While you can program in support to scan for val
id
characters (as defined by RFC 822) or verify the MX (mail exchange) record your
se
lf, the
se
are all beyond the scope of the
Java
Mail API. Authenticator Like the
java
.net clas
se
s, the
Java
Mail API can take advantage of an Authenticator to access protected resources via a u
se
rname and password. For the
Java
Mail API, that resource is the mail
se
rver. The
Java
Mail Authenticator is found in the
java
x.mail package and is different from the
java
.net class of the same name. The two don't share the same Authenticator as the
Java
Mail API works with
Java
1.1, which d
id
n't have the
java
.net variety. To u
se
the Authenticator, you subclass the abstract class and return a PasswordAuthentication instance from the getPasswordAuthentication() method. You must register the Authenticator with the
se
ssion when created. Then, your Authenticator will be notified when authentication is necessary. You could pop up a window or read the u
se
rname and password from a configuration file (though if not encrypted is not
se
cure), returning them to the caller as a PasswordAuthentication object. Properties props = new Properties(); // fill props with any information Authenticator auth = new MyAuthenticator();
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, auth); Transport The final part of
se
nding a message is to u
se
the Transport class. This class speaks the Fundamentals of the
Java
Mail API Page 10 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks protocol-specific language for
se
nding the message (usually SMTP). It's an abstract class and works something like
Se
ssion. You can u
se
the default version of the class by just calling the static
se
nd() method: Transport.
se
nd(message); Or, you can get a specific instance from the
se
ssion for your protocol, pass along the u
se
rname and password (blank if unnecessary),
se
nd the message, and clo
se
the connection: message.saveChanges(); // implicit with
se
nd() Transport transport =
se
ssion.getTransport("smtp"); transport.connect(host, u
se
rname, password); transport.
se
ndMessage(message, message.getAllRecipients()); transport.clo
se
(); This latter way is best when you need to
se
nd multiple messages, as it will keep the connection with the mail
se
rver active between messages. The basic
se
nd() mechanism makes a
se
parate connection to the
se
rver for each method call. Note: To watch the mail commands go by to the mail
se
rver,
se
t the debug flag with
se
ssion.
se
tDebug(true). Store and folder Getting messages starts similarly to
se
nding messages with a
Se
ssion. However, after getting the
se
ssion, you connect to a Store , quite possibly with a u
se
rname and password or Authenticator. Like Transport, you tell the Store what protocol to u
se
: // Store store =
se
ssion.getStore("imap"); Store store =
se
ssion.getStore("pop3"); store.connect(host, u
se
rname, password); After connecting to the Store, you can then get a Folder , which must be opened before you can read messages from it: Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message message[] = folder.getMessages(); For POP3, the only folder available is the INBOX. If you are using IMAP, you can have other folders available. Note: Sun's prov
id
ers are meant to be smart. While Message message[] = folder.getMessages(); might look like a slow operation reading every message from the
se
rver, only when you actually need to get a part of the message is the message content retrieved. Once you have a Message to read, you can get its content with getContent() or write its content to a stream with writeTo(). The getContent() method only gets the message content, while writeTo() output includes headers. Fundamentals of the
Java
Mail API Page 11 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks System.out.println(((MimeMessage)message).getContent()); Once you're done reading mail, clo
se
the connection to the folder and store. folder.clo
se
(aBoolean); store.clo
se
(); The boolean pas
se
d to the clo
se
() method of folder states whether or not to update the folder by removing deleted messages. Moving on Es
se
ntially, understanding how to u
se
the
se
se
ven clas
se
s is all you need for nearly everything with the
Java
Mail API. Most of the other capabilities of the
Java
Mail API build off the
se
se
ven clas
se
s to do something a little different or in a particular way, like if the content is an attachment. Certain tasks, like
se
arching, are isolated and are discus
se
d later. Fundamentals of the
Java
Mail API Page 12 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 6. Using the
Java
Mail API Introduction You've
se
en how to work with the core parts of the
Java
Mail API. In the following
se
ctions you'll find a how-to approach for connecting the pieces to do specific tasks.
Se
nding messages
Se
nding an e-mail message involves getting a
se
ssion, creating and filling a message, and
se
nding it. You can specify your SMTP
se
rver by
se
tting the mail.smtp.host property for the Properties object pas
se
d when getting the
Se
ssion: String host = ...; String from = ...; String to = ...; // Get system properties Properties props = System.getProperties(); //
Se
tup mail
se
rver props.put("mail.smtp.host", host); // Get
se
ssion
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, null); // Define message MimeMessage message = new MimeMessage(
se
ssion); message.
se
tFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.
se
tSubject("Hello
Java
Mail"); message.
se
tText("Welcome to
Java
Mail"); //
Se
nd message Transport.
se
nd(message); You should place the code in a try-catch block, as
se
tting up the message and
se
nding it can throw exceptions. Exerci
se
: Exerci
se
2. How to
se
nd your first message on page 23 Fetching messages For reading mail, you get a
se
ssion, get and connect to an appropriate store for your mailbox, open the appropriate folder, and get your messages. Also, don't forget to clo
se
the connection when done. String host = ...; String u
se
rname = ...; String password = ...; // Create empty properties Properties props = new Properties(); // Get
se
ssion
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, null); Fundamentals of the
Java
Mail API Page 13 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks // Get the store Store store =
se
ssion.getStore("pop3"); store.connect(host, u
se
rname, password); // Get folder Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); // Get directory Message message[] = folder.getMessages(); for (int i=0, n=message.length; i
se connection folder.clo
se
(fal
se
); store.clo
se
(); What you do with each message is up to you. The above code block just displays whom the message is from and the subject. Technically speaking, the list of from addres
se
s could be empty and the getFrom()[0] call could throw an exception. To display the whole message, you can prompt the u
se
r after
se
eing the from and subject fields, and then call the message's writeTo() method if the u
se
r wants to
se
e it. BufferedReader reader = new BufferedReader ( new InputStreamReader(System.in)); // Get directory Message message[] = folder.getMessages(); for (int i=0, n=message.length; i
se if ("QUIT".equals(line)) { break; } } Exerci
se
: Exerci
se
3. How to check for mail on page 25 Deleting messages and flags Deleting messages involves working with the Flags associated with the messages. There are different flags for different states, some system-defined and some u
se
r-defined. The predefined flags are defined in the inner class Flags.Flag and are listed below: * Flags.Flag.ANSWERED * Flags.Flag.DELETED * Flags.Flag.DRAFT * Flags.Flag.FLAGGED * Flags.Flag.RECENT * Flags.Flag.
SE
EN * Flags.Flag.U
SE
R Fundamentals of the
Java
Mail API Page 14 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Just becau
se
a flag exists doesn't mean the flag is supported by all mail
se
rvers or prov
id
ers. For instance, except for deleting messages, the POP protocol supports none of them. Checking for new mail is not a POP task but a task built into mail clients. To find out what flags are supported, ask the folder with getPermanentFlags(). To delete messages, you
se
t the message's DELETED flag: message.
se
tFlag(Flags.Flag.DELETED, true); Open up the folder in READ_WRITE mode first though: folder.open(Folder.READ_WRITE); Then, when you are done
process
ing all messages, clo
se
the folder, passing in a true value to expunge the deleted messages. folder.clo
se
(true); There is an expunge() method of Folder that can be u
se
d to delete the messages. However, it doesn't work for Sun's POP3 prov
id
er. Other prov
id
ers may or may not implement the capabilities. It will more than likely be implemented for IMAP prov
id
ers. Becau
se
POP only supports single access to the mailbox, you have to clo
se
the folder to delete the messages with Sun's prov
id
er. To un
se
t a flag, just pass fal
se
to the
se
tFlag() method. To
se
e if a flag is
se
t, check it with is
Se
t(). Authenticating your
se
lf You learned that you can u
se
an Authenticator to prompt for u
se
rname and password when needed, instead of passing them in as strings. Here you'll actually
se
e how to more fully u
se
authentication. Instead of connecting to the Store with the host, u
se
rname, and password, you configure the Properties to have the host, and tell the
Se
ssion about your custom Authenticator instance, as shown here: //
Se
tup properties Properties props = System.getProperties(); props.put("mail.pop3.host", host); //
Se
tup authentication, get
se
ssion Authenticator auth = new PopupAuthenticator();
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, auth); // Get the store Store store =
se
ssion.getStore("pop3"); store.connect(); You then subclass Authenticator and return a PasswordAuthentication object from the getPasswordAuthentication() method. The following is one such implementation, with a single field for both. (This isn't a Project Swing tutorial; just enter the two parts in the one field,
se
parated by a comma.) Fundamentals of the
Java
Mail API Page 15 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks import
java
x.mail.*; import
java
x.swing.*; import
java
.util.*; public class PopupAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication() { String u
se
rname, password; String result = JOptionPane.showInputDialog( "Enter 'u
se
rname,password'"); StringTokenizer st = new StringTokenizer(result, ","); u
se
rname = st.nextToken(); password = st.nextToken(); return new PasswordAuthentication(u
se
rname, password); } } Becau
se
the PopupAuthenticator relies on Swing, it will start up the event-handling thread for AWT. This basically requires you to add a call to System.exit() in your code to stop the program. Replying to messages The Message class includes a reply() method to configure a new Message with the proper recipient and subject, adding "Re: " if not already there. This does not add any content to the message, only copying the from or reply-to header to the new recipient. The method takes a boolean parameter indicating whether to reply to only the
se
nder (fal
se
) or reply to all (true). MimeMessage reply = (MimeMessage)message.reply(fal
se
); reply.
se
tFrom(new InternetAddress("pres
id
ent@whitehou
se
.gov")); reply.
se
tText("Thanks"); Transport.
se
nd(reply); To configure the reply-to address when
se
nding a message, u
se
the
se
tReplyTo() method. Exerci
se
: Exerci
se
4. How to reply to mail on page 27 Forwarding messages Forwarding messages is a little more involved. There is no single method to call, and you build up the message to forward by working with the parts that make up a message. A mail message can be made up of multiple parts. Each part is a BodyPart , or more specifically, a MimeBodyPart when working with MIME messages. The different body parts get combined into a container called Multipart or, again, more specifically a MimeMultipart . To forward a message, you create one part for the text of your message and a
se
cond part with the message to forward, and combine the two into a multipart. Then you add the multipart to a properly addres
se
d message and
se
nd it. That's es
se
ntially it. To copy the content from one message to another, just copy over its Fundamentals of the
Java
Mail API Page 16 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks DataHandler , a class from the
Java
Beans Activation Framework. // Create the message to forward Message forward = new MimeMessage(
se
ssion); // Fill in header forward.
se
tSubject("Fwd: " + message.getSubject()); forward.
se
tFrom(new InternetAddress(from)); forward.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Create your new message part BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.
se
tText( "Here you go with the original message:\n\n"); // Create a multi-part to combine the parts Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Create and fill part for the forwarded content messageBodyPart = new MimeBodyPart(); messageBodyPart.
se
tDataHandler(message.getDataHandler()); // Add part to multi part multipart.addBodyPart(messageBodyPart); // Associate multi-part with message forward.
se
tContent(multipart); //
Se
nd message Transport.
se
nd(forward); Working with attachments Attachments are resources associated with a mail message, usually kept outs
id
e of the message like a text file, spreadsheet, or image. As with common mail programs like Eudora and pine, you can attach resources to your mail message with the
Java
Mail API and get tho
se
attachments when you receive the message.
Se
nding attachments:
Se
nding attachments is quite like forwarding messages. You build up the parts to make the complete message. After the first part, your message text, you add other parts where the DataHandler for each is your attachment, instead of the shared handler in the ca
se
of a forwarded message. If you are reading the attachment from a file, your attachment data source is a FileDataSource . Reading from a URL, it is a URLDataSource . Once you have your DataSource, just pass it on to the DataHandler constructor, before finally attaching it to the BodyPart with
se
tDataHandler(). Assuming you want to retain the original filename for the attachment, the last thing to do is to
se
t the filename associated with the attachment with the
se
tFileName() method of BodyPart. All this is shown here: // Define message Message message = new MimeMessage(
se
ssion); message.
se
tFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.
se
tSubject("Hello
Java
Mail Attachment"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.
se
tText("Pardon
Id
eas"); Fundamentals of the
Java
Mail API Page 17 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.
se
tDataHandler(new DataHandler(source)); messageBodyPart.
se
tFileName(filename); multipart.addBodyPart(messageBodyPart); // Put parts in message message.
se
tContent(multipart); //
Se
nd the message Transport.
se
nd(message); When including attachments with your messages, if your program is a
se
rvlet, your u
se
rs must upload the attachment bes
id
es telling you where to
se
nd the message. Uploading each file can be handled with a form encoding type of multipart/form-data.
Note: Message size is limited by your SMTP
se
rver, not the
Java
Mail API. If you run into problems, cons
id
er increasing the
Java
heap size by
se
tting the ms and mx parameters. Exerci
se
: Exerci
se
5. How to
se
nd attachments on page 28 Getting attachments: Getting attachments out of your messages is a little more involved then
se
nding them becau
se
MIME has no simple notion of attachments. The content of your message is a Multipart object when it has attachments. You then need to
process
each Part, to get the main content and the attachment(s). Parts marked with a disposition of Part.ATTACHMENT from part.getDisposition() are clearly attachments. However, attachments can also come across with no disposition (and a non-text MIME type) or a disposition of Part.INLINE. When the disposition is either Part.ATTACHMENT or Part.INLINE, you can save off the content for that message part. Just get the original filename with getFileName() and the input stream with getInputStream(). Multipart mp = (Multipart)message.getContent(); for (int i=0, n=multipart.getCount(); i
se the file already exists, a number is added to the end of the filename until one is found that doesn't exist. // from saveFile() File file = new File(filename); Fundamentals of the
Java
Mail API Page 18 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks for (int i=0; file.exists(); i++) { file = new File(filename+i); } The code above covers the simplest ca
se
where message parts are flagged appropriately. To cover all ca
se
s, handle when the disposition is null and get the MIME type of the part to handle accordingly. if (disposition == null) { // Check if plain MimeBodyPart mbp = (MimeBodyPart)part; if (mbp.isMimeType("text/plain")) { // Handle plain } el
se
{ // Special non-attachment ca
se
s here of image/gif, text/html, ... } ... }
Process
ing HTML messages
Se
nding HTML-ba
se
d messages can be a little more work than
se
nding plain text message, though it doesn't have to be that much more work. It all depends on your specific requirements.
Se
nding HTML messages: If all you need to do is
se
nd the equivalent of an HTML file as the message and let the mail reader worry about fetching any embedded images or related pieces, u
se
the
se
tContent() method of Message, passing along the content as a String and
se
tting the content type to text/html. String htmlText = "
Hello
" + "
"; message.
se
tContent(htmlText, "text/html")); On the receiving end, if you fetch the message with the
Java
Mail API, there is nothing built into the API to display the message as HTML. The
Java
Mail API only
se
es it as a stream of bytes. To display the message as HTML, you must either u
se
the Swing JEditorPane or some third-party HTML viewer component. if (message.getContentType().equals("text/html")) { String content = (String)message.getContent(); JFrame frame = new JFrame(); JEditorPane text = new JEditorPane("text/html", content); text.
se
tEditable(fal
se
); JScrollPane pane = new JScrollPane(text); frame.getContentPane().add(pane); frame.
se
tSize(300, 300); frame.
se
tDefaultClo
se
Operation(JFrame.DISPO
SE
_ON_CLO
SE
); frame.show(); } Including images with your messages: On the other hand, if you want your HTML content message to be complete, with embedded images included as part of the message, you must treat the image as an attachment and reference the image with a special c
id
URL, where the c
id
is a reference to the Content-
ID
header of the image attachment. The
process
of embedding an image is quite similar to attaching a file to a message, the only Fundamentals of the
Java
Mail API Page 19 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks difference is you have to tell the MimeMultipart that the parts are related by
se
tting its subtype in the constructor (or with
se
tSubType()) and
se
t the Content-
ID
header for the image to a random string which is u
se
d as the src for the image in the img tag. The following demonstrates this completely. String file = ...; // Create the message Message message = new MimeMessage(
se
ssion); // Fill its headers message.
se
tSubject("Embedded Image"); message.
se
tFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Create your new message part BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "
Hello
" + "
id:memememe\">"; messageBodyPart.
se
tContent(htmlText, "text/html"); // Create a related multi-part to combine the parts MimeMultipart multipart = new MimeMultipart("related"); multipart.addBodyPart(messageBodyPart); // Create part for the image messageBodyPart = new MimeBodyPart(); // Fetch the image and associate to part DataSource fds = new FileDataSource(file); messageBodyPart.
se
tDataHandler(new DataHandler(fds)); messageBodyPart.
se
tHeader("Content-
ID
","memememe"); // Add part to multi-part multipart.addBodyPart(messageBodyPart); // Associate multi-part with message message.
se
tContent(multipart); Exerci
se
: Exerci
se
6. How to
se
nd HTML messages with images on page 29 Fundamentals of the
Java
Mail API Page 20 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 7.
Se
arching with
Se
archTerm Introduction The
Java
Mail API includes a filtering mechanism found in the
java
x.mail.
se
arch package to build up a
Se
archTerm . Once built, you then ask a Folder what messages match, retrieving an array of Message objects:
Se
archTerm st = ...; Message[] msgs = folder.
se
arch(st); There are 22 different clas
se
s available to help you build a
se
arch term. * AND terms (class AndTerm) * OR terms (class OrTerm) * NOT terms (class NotTerm) *
SE
NT DATE terms (class
Se
ntDateTerm) * CONTENT terms (class BodyTerm) * HEADER terms (FromTerm / FromStringTerm, RecipientTerm / RecipientStringTerm, SubjectTerm, etc..) Es
se
ntially, you build up a logical expression for matching messages, then
se
arch. For instance the following term
se
arches for messages with a (partial) subject string of ADV or a from field of friend@public.com. You might cons
id
er periodically running this query and automatically deleting any messages returned.
Se
archTerm st = new OrTerm( new SubjectTerm("ADV:"), new FromStringTerm("friend@public.com")); Message[] msgs = folder.
se
arch(st); Fundamentals of the
Java
Mail API Page 21 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 8. Exerci
se
s About the exerci
se
s The
se
exerci
se
s are designed to prov
id
e help according to your needs. For example, you might simply complete the exerci
se
given the information and the task list in the exerci
se
body; you might want a few hints; or you may want a step-by-step gu
id
e to successfully complete a particular exerci
se
. You can u
se
as much or as little help as you need per exerci
se
. Moreover, becau
se
complete solutions are also prov
id
ed, you can skip a few exerci
se
s and still be able to complete future exerci
se
s requiring the skipped ones. Each exerci
se
has a list of any prerequisite exerci
se
s, a list of skeleton code for you to start with, links to necessary API pages, and a text description of the exerci
se
goal. In addition, there is help for each task and a solutions page with links to files that compri
se
a solution to the exerci
se
. Exerci
se
1. How to
se
t up a
Java
Mail environment In this exerci
se
you will install Sun's
Java
Mail reference implementation. After installing, you will be introduced to the demonstration programs that come with the reference implementation. Task 1: Download the latest version of the
Java
Mail API implementation from Sun. Task 2: Download the latest version of the
Java
Beans Activation Framework from Sun. Task 3: Unzip the downloaded packages. You get a ZIP file for all
platform
s for both packages. Help for task 3: You can u
se
the jar tool to unzip the packages. Task 4: Add the mail.jar file from the
Java
Mail 1.2 download and the activation.jar file from the
Java
Beans Activation Framework download to your CLASSPATH. Help for task 4: Copy the files to your extension library directory. For Microsoft Windows, using the default installation copy, the command might look like the following: cd \
java
mail-1.2 copy mail.jar \
jdk
1.3\jre\lib\ext cd \jaf-1.0.1 copy activation.jar \
jdk
1.3\jre\lib\ext If you don't like copying the files to the extension library directory, detailed instructions are available from Sun for
se
tting your CLASSPATH on Windows NT. Task 5: Go into the demo directory that comes with the
Java
Mail API implementation and compile the msg
se
nd program to
se
nd a test message. Help for task 5:
java
c msg
se
nd.
java
Fundamentals of the
Java
Mail API Page 22 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Task 6: Execute the program passing in a from address with the -o option, your SMTP
se
rver with the -M option, and the to address (with no option). You'll then enter the subject, the text of your message, and the end-of-file character (CTRL-Z) to signal the end of the message input. Help for task 6: Be sure to replace the from address, SMTP
se
rver, and to address.
java
msg
se
nd -o from@address -M SMTP.
Se
rver to@address If you are not sure of your SMTP
se
rver, contact your system administrator or check with your Internet
Se
rvice Prov
id
er. Task 7: Check to make sure you received the message with your normal mail reader (Eudora, Outlook Express, pine, ...). Exerci
se
1. How to
se
t up a
Java
Mail environment: Solution Upon successful completion, the
Java
Mail reference implementation will be in your CLASSPATH. Exerci
se
2. How to
se
nd your first message In the last exerci
se
you
se
nt a mail message using the demonstration program prov
id
ed with the
Java
Mail implementation. In this exerci
se
, you'll create the program your
se
lf. For more help with exerci
se
s,
se
e About the exerci
se
s on page 22 . Prerequisites: * Exerci
se
1. How to
se
t up a
Java
Mail environment on page 22 Skeleton code: * MailExample.
java
Task 1: Starting with the skeleton code , get the system Properties. Help for task 1: Properties props = System.getProperties(); Task 2: Add the name of your SMTP
se
rver to the properties for the mail.smtp.host key. Fundamentals of the
Java
Mail API Page 23 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Help for task 2: props.put("mail.smtp.host", host); Task 3: Get a
Se
ssion object ba
se
d on the Properties. Help for task 3:
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, null); Task 4: Create a MimeMessage from the
se
ssion. Help for task 4: MimeMessage message = new MimeMessage(
se
ssion); Task 5:
Se
t the from field of the message. Help for task 5: message.
se
tFrom(new InternetAddress(from)); Task 6:
Se
t the to field of the message. Help for task 6: message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); Task 7:
Se
t the subject of the message. Help for task 7: message.
se
tSubject("Hello
Java
Mail"); Task 8:
Se
t the content of the message. Help for task 8: message.
se
tText("Welcome to
Java
Mail"); Task 9: U
se
a Transport to
se
nd the message. Help for task 9: Transport.
se
nd(message); Task 10: Compile and run the program, passing your SMTP
se
rver, from address, and to address on the command line. Fundamentals of the
Java
Mail API Page 24 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Help for task 10:
java
MailExample SMTP.
Se
rver from@address to@address Task 11: Check to make sure you received the message with your normal mail reader (Eudora, Outlook Express, pine, ...). Exerci
se
2. How to
se
nd your first message: Solution The following
Java
source file repre
se
nts a solution to this exerci
se
: * Solution/MailExample.
java
Exerci
se
3. How to check for mail In this exerci
se
, create a program that displays the from address and subject for each message and prompts to display the message content. For more help with exerci
se
s,
se
e About the exerci
se
s on page 22 . Prerequisites: * Exerci
se
1. How to
se
t up a
Java
Mail environment on page 22 Skeleton Code * GetMessageExample.
java
Task 1: Starting with the skeleton code , get or create a Properties object. Help for task 1: Properties props = new Properties(); Task 2: Get a
Se
ssion object ba
se
d on the Properties. Help for task 2:
Se
ssion
se
ssion =
Se
ssion.getDefaultInstance(props, null); Task 3: Get a Store for your e-mail protocol, either pop3 or imap. Help for task 3: Store store =
se
ssion.getStore("pop3"); Task 4: Connect to your mail host's store with the appropriate u
se
rname and password. Fundamentals of the
Java
Mail API Page 25 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Help for task 4: store.connect(host, u
se
rname, password); Task 5: Get the folder you want to read. More than likely, this will be the INBOX. Help for task 5: Folder folder = store.getFolder("INBOX"); Task 6: Open the folder read-only. Help for task 6: folder.open(Folder.READ_ONLY); Task 7: Get a directory of the messages in the folder. Save the message list in an array variable named message. Help for task 7: Message message[] = folder.getMessages(); Task 8: For each message, display the from field and the subject. Help for task 8: System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject()); Task 9: Display the message content when prompted. Help for task 9: System.out.println(message[i].getContent()); Task 10: Clo
se
the connection to the folder and store. Help for task 10: folder.clo
se
(fal
se
); store.clo
se
(); Task 11: Compile and run the program, passing your mail
se
rver, u
se
rname, and password on the command line. Answer YES to the messages you want to read. Just hit ENTER if you don't. If you want to stop reading your mail before making your way through all the messages, enter QUIT. Help for task 11:
java
GetMessageExample POP.
Se
rver u
se
rname password Fundamentals of the
Java
Mail API Page 26 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Exerci
se
3. How to check for mail: Solution The following
Java
source file repre
se
nts a solution to this exerci
se
. * Solution/GetMessageExample.
java
Exerci
se
4. How to reply to mail In this exerci
se
, create a program that creates a canned reply message and attaches the original message if it's plain text. For more help with exerci
se
s,
se
e About the exerci
se
s on page 22 . Prerequisites: * Exerci
se
3. How to check for mail on page 25 Skeleton Code: * ReplyExample.
java
Task 1: The skeleton code already includes the code to get the list of messages from the folder and prompt you to create a reply. Task 2: When answered affirmatively, create a new MimeMessage from the original message. Help for task 2: MimeMessage reply = (MimeMessage)message[i].reply(fal
se
); Task 3:
Se
t the from field to your e-mail address. Task 4: Create the text for the reply. Include a canned message to start. When the original message is plain text, add each line of the original message, prefix each line with the "> " characters. Help for task 4: To check for plain text messages, check the messages MIME type with mimeMessage.isMimeType("text/plain"). Task 5:
Se
t the message's content, once the message content is fully determined. Task 6:
Se
nd the message. Task 7: Compile and run the program, passing your mail
se
rver, SMTP
se
rver, u
se
rname, password, and from address on the command line. Answer YES to the messages you want to
se
nd replies. Just hit ENTER if you don't. If you want to stop going through your mail before Fundamentals of the
Java
Mail API Page 27 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks making your way through all the messages, enter QUIT. Help for task 7:
java
ReplyExample POP.
Se
rver SMTP.
Se
rver u
se
rname password from@address Task 8: Check to make sure you received the message with your normal mail reader (Eudora, Outlook Express, pine, ...). Exerci
se
4. How to reply to mail: Solution The following
Java
source file repre
se
nts a solution to this exerci
se
. * Solution/ReplyExample.
java
Exerci
se
5. How to
se
nd attachments In this exerci
se
, create a program that
se
nds a message with an attachment. For more help with exerci
se
s,
se
e About the exerci
se
s on page 22 . Prerequisites: * Exerci
se
2. How to
se
nd your first message on page 23 Skeleton Code: * AttachExample.
java
Task 1: The skeleton code already includes the code to get the initial mail
se
ssion. Task 2: From the
se
ssion, get a Message and
se
t its header fields: to, from, and subject. Task 3: Create a BodyPart for the main message content and fill its content with the text of the message. Help for task 3: BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.
se
tText("Here's the file"); Task 4: Create a Multipart to combine the main content with the attachment. Add the main content to the multipart. Help for task 4: Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); Fundamentals of the
Java
Mail API Page 28 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Task 5: Create a
se
cond BodyPart for the attachment. Task 6: Get the attachment as a DataSource. Help for task 6: DataSource source = new FileDataSource(filename); Task 7:
Se
t the DataHandler for the message part to the data source. Carry the original filename along. Help for task 7: messageBodyPart.
se
tDataHandler(new DataHandler(source)); messageBodyPart.
se
tFileName(filename); Task 8: Add the
se
cond part of the message to the multipart. Task 9:
Se
t the content of the message to the multipart. Help for task 9: message.
se
tContent(multipart); Task 10:
Se
nd the message. Task 11: Compile and run the program, passing your SMTP
se
rver, from address, to address, and filename on the command line. This will
se
nd the file as an attachment. Help for task 11:
java
AttachExample SMTP.
Se
rver from@address to@address filename Task 12: Check to make sure you received the message with your normal mail reader (Eudora, Outlook Express, pine, ...). Exerci
se
5. How to
se
nd attachments: Solution The following
Java
source file repre
se
nts a solution to this exerci
se
. * Solution/AttachExample.
java
Exerci
se
6. How to
se
nd HTML messages with images In this exerci
se
, create a program that
se
nds an HTML message with an image attachment where the image is displayed within the HTML message. Fundamentals of the
Java
Mail API Page 29 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks For more help with exerci
se
s,
se
e About the exerci
se
s on page 22 . Prerequisites: * Exerci
se
5. How to
se
nd attachments on page 28 Skeleton code: * logo.gif * HtmlImageExample.
java
Task 1: The skeleton code already includes the code to get the initial mail
se
ssion, create the main message, and fill its headers (to, from, subject). Task 2: Create a BodyPart for the HTML message content. Task 3: Create a text string of the HTML content. Include a reference in the HTML to an image (
) that is local to the mail message. Help for task 3: U
se
a c
id
URL. The content-
id
will need to be specified for the image later. String htmlText = "
Hello
" + "
id:memememe\">"; Task 4:
Se
t the content of the message part. Be sure to specify the MIME type is text/html. Help for task 4: messageBodyPart.
se
tContent(htmlText, "text/html"); Task 5: Create a Multipart to combine the main content with the attachment. Be sure to specify that the parts are related. Add the main content to the multipart. Help for task 5: MimeMultipart multipart = new MimeMultipart("related"); multipart.addBodyPart(messageBodyPart); Task 6: Create a
se
cond BodyPart for the attachment. Task 7: Get the attachment as a DataSource, and
se
t the DataHandler for the message part to the data source. Task 8:
Se
t the Content-
ID
header for the part to match the image reference specified in the HTML. Help for task 8: messageBodyPart.
se
tHeader("Content-
ID
","memememe"); Task 9: Add the
se
cond part of the message to the multipart, and
se
t the content of the Fundamentals of the
Java
Mail API Page 30 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks message to the multipart. Task 10:
Se
nd the message. Task 11: Compile and run the program, passing your SMTP
se
rver, from address, to address, and filename on the command line. This will
se
nd the images as an inline image within the HTML text. Help for task 11:
java
HtmlImageExample SMTP.
Se
rver from@address to@address filename Task 12: Check if your mail reader recognizes the message as HTML and displays the image within the message, instead of as a link to an external attachment file. Help for task 12: If your mail reader can't display HTML messages, cons
id
er
se
nding the message to a friend. Exerci
se
6. How to
se
nd HTML messages with images: Solution The following
Java
source files repre
se
nt a solution to this exerci
se
. * Solution/logo.gif * Solution/HtmlImageExample.
java
Fundamentals of the
Java
Mail API Page 31 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks
Se
ction 9. Wrapup In summary The
Java
Mail API is a
Java
package u
se
d for reading, composing, and
se
nding e-mail messages and their attachments. It lets you build standards-ba
se
d e-mail clients that employ various Internet mail protocols, including SMTP, POP, IMAP, and MIME, as well as related protocols such as NNTP, S/MIME, and others. The API div
id
es naturally into two parts. The first focu
se
s on
se
nding, receiving, and managing messages independent of the protocol u
se
d, whereas the
se
cond focu
se
s on specific u
se
of the protocols. The purpo
se
of this tutorial was to show how to u
se
the first part of the API, without attempting to deal with protocol prov
id
ers. The core
Java
Mail API consists of
se
ven clas
se
s --
Se
ssion, Message, Address, Authenticator, Transport, Store, and Folder --all of which are found in
java
x.mail, the top-level package for the
Java
Mail API. We u
se
d the
se
clas
se
s to work through a number of common e-mail-related tasks, including
se
nding messages, retrieving messages, deleting messages, authenticating, replying to messages, forwarding messages, managing attachments,
process
ing HTML-ba
se
d messages, and
se
arching or filtering mail lists. Finally, we prov
id
ed a number of step-by-step exerci
se
s to help illustrate the concepts pre
se
nted. Hopefully, this will help you add e-mail functionality to your
platform
-independent
Java
applications. Resources You can do much more with the
Java
Mail API than what's found here. The lessons and exerci
se
s found here can be supplemented by the following resources: * Download the
Java
Mail 1.2 API from the
Java
Mail API home page . * The
Java
Beans Activation Framework is required for versions 1.2 and 1.1.3 of the
Java
Mail API. * The
Java
Mail-interest mailing list is a Sun-hosted discussion forum for developers. * Sun's
Java
Mail FAQ addres
se
s the u
se
of
Java
Mail in applets and
se
rvlets, as well as prototol-specific questions. * Tutorial author John Zukowski maintains jGuru's
Java
Mail FAQ . * Want to
se
e how others are using
Java
Mail? Check out Sun's list of third-party products. * If you want more detail about
Java
Mail, read Rick Grehan's "How
Java
Mail keeps it simple" (Lotus Developer Network, June 2000). * Benoit Marchal shows how to u
se
Java
and XML to produce plain text and HTML newsletters in this two-part
se
ries, "Managing e-zines with
Java
Mail and XSLT" Part 1 (developerWorks, March 2001) and Part 2 (developerWorks, April 2001). * "Linking Applications with E-mail" (Lotus Developer Network, May 2000) discus
se
s how groupware can facilitate communication, collaboration, and coordination among applications. Fundamentals of the
Java
Mail API Page 32 Pre
se
nted by developerWorks, your source for great tutorials ibm.com/developerWorks Feedback Plea
se
let us know whether this tutorial was helpful to you and how we could make it better. We'd also like to hear about other tutorial topics you'd like to
se
e covered. Thanks! For questions about the content of this tutorial, contact the author John Zukowski ( jaz@zukowski.net ) Colophon This tutorial was written entirely in XML, using the developerWorks Toot-O-Matic tutorial generator. The Toot-O-Matic tool is a short
Java
program that u
se
s XSLT stylesheets to convert the XML source into a number of HTML pages, a zip file, JPEG heading graphics, and PDF files. Our ability to generate multiple text and
binary
formats from a single source file illustrates the power and flexibility of XML. Fundamentals of the
Java
Mail API Page 33
安装
java
的
jdk
时
要求
我
关闭
什么
java
platform
SE
binary
(
Process
Id
:
5240
) 的
解决
办法
先看
安装
的问题件是不是空文件夹, 不是的话换一个空文件夹就能
解决
, 要英文路径
安装
java
的
jdk
时
要求
关闭
java
platform
SE
binary
(
Process
Id
: 9152)
JDK
安装
报错:
java
(TM)
platform
SE
binary
(
Process
Id
:7676)
JDK
安装
报错处理
Java
安装
过程中出现
Java
Platform
SE
binary
(
Process
Id
:27109)
解决
办法
Win+X打开任务管理器。找到对应
Id
结束进程即可。
Java
50,527
社区成员
85,606
社区内容
发帖
与我相关
我的任务
Java
Java相关技术讨论
复制链接
扫一扫
分享
社区描述
Java相关技术讨论
java
spring boot
spring cloud
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章