Download javax.mail jar




















You can create a charset provider for the "bad" charset name that simply redirects to an existing charset provider; see the following code. Obviously you could get significantly more clever and redirect all unknown charsets to "us-ascii", for instance. A: Open the folder, process all the messages in the folder, and check whether new messages have arrived the message count has increased. If so, loop. If not, add the listener, then wait for new messages e.

Part of the key to understanding how this works is understanding when the server is allowed to notify the client of new messages, and when JavaMail will see those notifications. Between getting the number of messages and adding the listener, JavaMail won't see any notifications of new messages.

By the time you do an operation that will allow JavaMail to see the notification, the listener will be in place. The following code illustrates the general approach. Many details have been omitted. Q: How do I reply to a message? A: To reply to a message, use the reply method on the Message object. This method will return a new object with the headers set appropriately for a reply.

You'll need to supply the content of the message yourself. Q: How do I forward a message? A: The approach used to forward a message depends on how you want to present the forwarded message. It's straightforward to create a new MimeMessage, address it appropriately, and attach an existing message as an attachment to the new message. You'll also want to include appropriate error checking and test for null values.

A: There are a number of demo programs included with the distribution that show how to send HTML mail. If you want to send a simple message that has HTML instead of plain text, see the sendhtml. If you want to send an HTML file as an attachment, see the sendfile. Q: How do I send mail with formatted text using different fonts and colors? See above. Q: How do I send mail with both plain text as well as HTML text so that each mail reader can choose the format appropriate for it?

You'll need to construct the plain and html parts yourself to have appropriate content. See RFC for details of the structure of such a message. A: The simplest approach is to send HTML text with image tags that reference images on a public web site. In this approach the images aren't actually included in the message, and so won't be visible if the user is not connected to the Internet when they read the message. Note also that some mailers will refuse to display images that are on remote sites.

Another approach is to include the image data inline in the html content of the message, for example:. A simple example follows:. Q: What's the difference between the Transport methods send and sendMessage?

A: The send method is a static method and can be used without needing an instance of a Transport object. It is intended for the common, simple case of sending a single message using the default transport. Internally, the send method will first call the saveChanges method on the message. It will then create an appropriate new Transport object, call the Transport's connect method, call the Transport's sendMessage method to actually send the message, call the Transport's close method, and finally abandon the new instance of the Transport object to be collected by the garbage collector.

Actually, it's rather more complicated than that, but that's the general idea. As you can see, the static send convenience method is built on the more general per-instance sendMessage method. There are a number of reasons for an application to use the sendMessage method directly. The most common reasons are to improve performance by sending more than one message during a single connection, or to manually manage the connection so as to provide authentication information.

The package javadocs for the com. The easiest is to replace the call Transport. A: You should call trans. As described above , the send method is a static convenience method that acquires its own Transport object and creates its own connection to use for sending; it does not use the connection associated with any Transport object through which it is invoked. Q: I modified this message, but the headers do not reflect the changes. A: You should call saveChanges after you create a new message or modify an existing message.

This will cause the headers to be reset and reflect your changes. Note that the Transport. So if all you are doing is sending the modified message, you can skip calling saveChanges yourself. Q: I set a particular value for the Message-ID header of my new message, but when I send this message that header is rewritten. A: A new value for the Message-ID field is set when the saveChanges method is called usually implicitly when a message is sent , overwriting any value you set yourself.

If you need to set your own Message-ID and have it retained, you will have to create your own MimeMessage subclass, override the updateMessageID method and use an instance of this subclass. A: You probably set some content for your message using the setContent Object o, String type method. If not, you will get the UnsupportedDataTypeException.

See the JAF documents for more information. In most cases the simplest workaround is to use a ByteArrayDataSource as described above. A: The mail. If this property if absent, the message's From attribute is used. If multiple threads need to send mail simultaneously, and each needs to set the From attribute, each thread should use its own Session object with its own Properties object.

The mail. Alternatively, each thread can use the com. SMTPMessage class. The setEnvelopeFrom method on that class can be used to set this value. With this approach, all threads can use the same Session. Q: I want to repeatedly send messages, to a different set of recipients each time.

But invoking Transport. This is suboptimal in this case, so how do I get around this? A: Create an instance of the appropriate Transport object, connect to it and invoke the sendMessage method repeatedly. If that call fails to return any data, no name is sent in the HELO command. Check your JDK and name server configuration to ensure that that call returns the correct data. You may also set the "mail. You can enable use of this command by setting the Session property "mail. Also, the javadocs for the com.

Note that your server is most likely also going to want you to authenticate, as described in this entry. A: There is no end-to-end address verification on the Internet. Often a message will need to be forwarded to several mail servers before reaching one that can determine whether or not it can deliver the message.

If a failure occurs in one of these later steps, the message will typically be returned to the sender as undeliverable. A successful "send" indicates only that the mail server has accepted the message and will try to deliver it. Q: When a message can't be delivered, a failure message is returned. How can I detect these "bounced" messages? RFC discusses this problem in depth, including numerous examples. In Internet email, the existence of a particular mailbox or user name can only be determined by the ultimate server that would deliver the message.

The message may pass through several relay servers that are not able to detect the error before reaching the end server. Typically, when the end server detects such an error, it will return a message indicating the reason for the failure to the sender of the original message. There are many Internet standards covering such Delivery Status Notifications but a large number of servers don't support these new standards, instead using ad hoc techniques for returning such failure messages.

This makes it very difficult to correlate a "bounced" message with the original message that caused the problem.

Note that this problem is completely independent of JavaMail. There are a number of techniques and heuristics for dealing with this problem - none of them perfect.

Q: When I construct an InternetAddress object, why don't I get an exception if the address is illegal? A: The InternetAddress class only checks the syntax of the address. As discussed above , the InternetAddress class is not able to determine whether the address actually exists as a legal address.

It is not even possible to verify the host name if the application is running behind a firewall or isn't currently connected to the Internet. Q: When I try to send a message, why do I get javax. SendFailedException: Unable to relay for my-address?

A: This is not a JavaMail problem. This is an error reply from your SMTP mail server. It indicates that your mail server is not configured to allow you to send mail through it. Typically, mail servers for an organization will be configured to allow mail from within the organization to be sent to other addresses within the organization, or to addresses external to the organization. It will also typically allow mail coming from an address external to an organization to be sent to addresses within the orgnaization.

What it will typically not allow is mail coming from an address external to the organization to be sent relayed to another address also external to the organization. The configuration of the mail server determines whether such relaying is allowed, and which addresses are considered internal vs. Often mail servers will require you to authenticate before they will relay messages.

Q: When I try to send a message to for example Yahoo, why do I get an error that says "connection refused"? A: The host you're trying to connect to is most likely not running a mail server. If you're trying to connect to a web mail service such as Yahoo, you can't usually use the web host name e. Instead, you'll need to learn the name of the host running the required mail server; contact your web mail provider for this information.

Note that some web mail providers don't offer this service, instead allowing you to access your mail only through a browser. If you're not trying to connect to a web mail account, but instead are trying to connect to a host on your local network, then most likely the host you're trying to connect to is not running a mail server. Sometimes this will occur if you forget to set for example the "mail. Thus, attempts to connect to "localhost" on Windows machines will usually fail with a "connection refused" error.

Q: When sending mail, why does it fail with an exception that includes a message something like To send mail, first check your mail with a valid POP account? A: In order to prevent their use to send spam, some mail servers will require you to have a valid POP3 account and require you to login to that account before they'll let you send mail through that mail server. This is simple to handle in JavaMail. When you know that you're dealing with such a mail server, make sure you connect to your POP3 Store on that mail server before sending mail.

Q: I want to delete messages on a POP3 server. A: The expunge method is not supported by the POP3 provider. Instead, after marking the messages to be deleted by setting the DELETED flag on those messages, close the folder with the expunge flag set to true.

That is, invoke folder. A: This is possible with the POP3 provider. See the com. The com. A: The POP3 protocol provides no way to determine whether a folder has new messages. A: The POP3 protocol does not provide information about when a message was received.

It may be possible to guess at the received date by looking at some message headers such as the Received header, but this is not very reliable. A: Most likely you have more than one version of pop3.

A: The error usually looks like:. NoSuchFieldError: contentStream at com. As above , you've mixed versions of the POP3 provider and javax. You probably have an older version of javax.

A: You can't. POP3 servers only support a single mailbox per user. See this item for more information about local store providers. A: Your POP3 server is broken. It then subtracts the two values to determine the size of the message body. If the server reports the size of the entire message incorrectly, you may get a negative number.

You can set the property "mail. This can cause all sorts of strange failures in JavaMail. One solution is to disable use of the TOP command, as described above.

Another approach that works in some cases os to tell JavaMail to forget about the headers it retrieved using the TOP command after retrieving the entire message using the RETR command. To do this, set the property "mail. Q: Can I use JavaMail in servlets? A: Yes, see the Installation and Configuration section above for more details. No installation or configuration is necessary when using JavaMail in a Java EE product, it's just there! Q: My web application uses JavaMail to attach and send a file that the user specifies, but my application can't find the file.

A: If the user species the filename in an HTML form in the browser, the filename is normally the name of a file on the user's machine, not on the server. The file will need to be uploaded to the server before JavaMail can access it.

Q: Can I use JavaMail in applets? Q: What are the security implications of using JavaMail in an applet? A: One of the biggest issues with using JavaMail in applets is the default applet security restrictions.

These restrictions only allow applets to connect to the host from which they were loaded. Thus, for such applets to use JavaMail, the mail server will need to be located on the same machine as the web server from which the applet is loaded. You can find more information on the applet security model here. Q: Ok, maybe I really don't want to use an applet, what should I do instead? A: In general, we recommend use of a web application to collect a mail message and send it using JavaMail.

The demo servlet included in the JavaMail download package illustrates this approach. How do I send feedback or comments? Where is javax. Where can I find a version of JavaMail for my favorite operating system? What is IMAP? What is SMTP? What is MIME? What is POP3? How do I store mail messages on my local disk?

Where do I find documentation on the protocol providers? Is JavaMail Y2K compliant? The answer to my question isn't here, where else should I look? I get a ClassNotFoundException for a javax. Does JavaMail include all the necessary mail servers?

Where can I get the necessary mail servers? What host name, user name, or password should I use? How do I configure JavaMail to work through my proxy server? When connecting to my mail server over SSL I get an exception like "unable to find valid certification path to requested target". While trying to run my program on Linux I get a very strange error message and the program fails.

How do I configure Tomcat to allow me to use JavaMail in my web application? When using JavaMail in my servlet, it is unable to find any of the JavaMail classes. My servlet can find the JavaMail classes, but JavaMail complains that it can't find a service provider for "smtp" or "imap" or address type "rfc". How do I access Gmail with JavaMail? How do I delete a message in Gmail? How do I access Yahoo! How do I access Outlook. Where can I find some example programs that show how to use JavaMail?

What are some of the most common mistakes people make when using JavaMail? How do I send a message with an attachment? How do I tell if a message has attachments? How do I read a message with an attachment and save the attachment? How do I find the main message body in a message that has attachments?

How do I find the attachments in a message? How do I get all the unread messages in a folder? Should I use the isMimeType method, or should I use instanceof on the object returned by the getContent method, when deciding how to process a message part? How do I manually set the MIME type for an attached file, for example in the case where the filename doesn't have an obvious extension?

How do I create or process calendar appointments? When should I use Session. What is "disconnected support"? The writeTo method generates message text with lines that are neither the canonical MIME representation of the data i.

Can I use the JavaMail APIs to add new user accounts to my mail server, remove user accounts from my mail server, or change the passwords for user accounts on my mail server? Why doesn't the MimeMessage class implement Serializable so that I can serialize a message to disk and read it back later? How do I write a Service Provider? I'm having trouble logging into my Microsoft Exchange server, even though I'm sure I'm using the correct username and password, what could I be doing wrong?

How do I encode a binary file before sending it and how do I decode it when I receive it? If I don't need to encode and decode attachments myself, when should I use the MimeUtility methods? I have data that's already encoded in e. Even though JavaMail does all the encoding and decoding for me, I need to manually control the encoding for some body parts. Why do I get an error such as java. How do I debug problems connecting to my mail server?

How do I debug problems connecting to my mail server using SSL? How do I debug problems with Java security permissions? Does the IMAP provider cache the retrieved data? I want to move messages between folders.

Retrieving large message bodies seems inefficient at times. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Java. More related articles in Java. We use cookies to ensure you have the best browsing experience on our website. Start Your Coding Journey Now! Or, post a question on Stack Overflow using the javamail tag. From time to time snapshot releases of the next version of JavaMail under development are published to the java.

These snapshot releases have received only minimal testing, but may provide previews of bug fixes or new features under development. For example, you can download the javax. Be sure to scroll to the bottom and choose the jar file with the most recent time stamp. The latest release includes support for JavaMail on Android. See the Android page for details. The use of OAuth2 authentication with JavaMail is described here.

The following pages provide hints and tips for using JavaMail on particular operating systems or environments:. See Build Instructions for instructions on how to download and build the most recent JavaMail source code.



0コメント

  • 1000 / 1000