Receive email features with Javamail
JavaMail, as the name suggests, providing the developer to process an email related programming interface. We can develop applications similar to Microsoft Outlook based on JavaMail. This article describes how to install JavaMail, send and accept emails using JavaMail.
JavaMail is an optional package, so if you need to use, you need to download from java.sun.com. The latest version is javamail1.3.2. You need to support JavaBean Activation Framework when using JavaMail, so you also need to download JAF. Installing JavaMail just need to add them to the classpath, if you don't want to modify classpath, you can directly copy their JAR package directly under Java_home / lib / ext. This is installed in JavaMail.
The core class for handling emails in the JavaMail package is: Session, Message, Address, Authenticator, Transport, Store, Folder, etc. Session defines a basic mail session that needs to read information similar to mail server, username, and password from Properties. The creation is as follows: Properties Props = New Properties (); // fill props with any information. // fill props with any information. Bind.
// define message mimeMessage message = new mimeMessage (session);
We can define similar to email senders, mail recipients, email headings, and email content.
// set the from address message.setfrom (new internetdress (from));
// set the to address message.addrecipient (message.recipienttype.to, new internetdress (to));
// set the Subject Message.SetSubject ("Hello JavaMail");
// set the content message.setText ("Welcome to JavaMail";
After constructing Message, we can send the first email, and provide the Transport class in Javamail to send emails, just call the Send method. The default is to send mail using the SMTP protocol.
// send message transport.send (Message);
Store and Folder class are what we need to use when receiving emails. On the same way, what protocol we need to tell when receiving mail, such as POP3, and then we need to connect to the mail server, by calling the store's Connect () method. After connecting to the Store, we can get Folder, make sure you have already opened the folder before receiving Message, then call the Folder's getMessages () method. Get Message []. Don't forget to close the Folder and Store after receiving.
// Get The Store Store = session.getStore ("POP3"); // Connect To Store Store.connect (Host, UserName, Password); // Get Folder Folder = Store.getFolder ("Inbox"); / / Open read-only folder.open (Folder.READ_ONLY); BufferedReader reader = new BufferedReader (new InputStreamReader (System.in)); // Get directory Message message [] = folder.getMessages (); the following code MailExample and GetMessageExample The functionality of sending and receiving mail is achieved, providing you with reference. Have time you can also read JavaMail's Tutorial to java.sun.com. // Send mail import java.util.properties; import javax.mail. *; Import javax.mail.internet. *;
Public class mailexample {public static void main (string args []) throws exception {string host = args [0]; string from = args [1]; string to = args [2];
// Get System Properties Properties PROPS = System.getProperties ();
// setup mail server props.put ("mail.smtp.host", host);
// Get session session session = session.getDefaultInstance (Props, NULL);
// define message mimeMessage message = new mimeMessage (session);
// set the from address message.setfrom (new internetdress (from));
// set the to address message.addrecipient (message.recipienttype.to, new internetdress (to));
// set the Subject Message.SetSubject ("Hello JavaMail");
// set the content message.setText ("Welcome to JavaMail";
// sent message transport.send (message);}}
// Receive mail import java.io. *; Import java.util.properties; import javax.mail. *;
public class GetMessageExample {public static void main (String args []) throws Exception {String host = "263.net"; String username = "username"; // your username String password = "password"; // your password // Create Empty Properties Properties PROPS = New Properties ();
// Get session session session = session.getDefaultInstance (Props, NULL);
// Get The Store Store = session.getStore ("POP3"); // Connect To Store Store.connect (Host, UserName, Password); // Get Folder Folder = Store.getFolder ("Inbox"); / / Open read-only folder.open (Folder.READ_ONLY); BufferedReader reader = new BufferedReader (new InputStreamReader (System.in)); // Get directory Message message [] = folder.getMessages (); for (int i = 0 , n = message.length; i // Display from Field and Subject System.Out.println (i ":" Message [i] .GetFrom () [0] "/ t" message [i] .getsubject ()); System.out .println ("do you want to read message? [YES to read / quit to end]"); string line = reader.readline (); if ("yes" .Equals (line)) {// display Message content system.out.println (Message [i] .getContent ()); } else IF ("quit" .Equals (line)) {breaf;}} // Close Connection Folder.Close (False); Store.Close ();}} Article entry: Mingjava Editor: mingjava Forward from http://www.j2medev.com website