Вобщем вот пример как я работаю: Код | public class send { public static void main(String[] args) throws Exception { // CREATE CLIENT INSTANCES MailClient AntonPechsherov = new MailClient("Anton.Pesherov", "localhost"); MailClient ShipilovAndrey = new MailClient("Shipilov.Andrey", "localhost"); MailClient BragnikovKonstantin = new MailClient("Bragnikov.konstantin", "localhost");
// CLEAR EVERYBODY'S INBOX //redClient.checkInbox(MailClient.CLEAR_MESSAGES); // greenClient.checkInbox(MailClient.CLEAR_MESSAGES); // blueClient.checkInbox(MailClient.CLEAR_MESSAGES); Thread.sleep(500); // Let the server catch up
// SEND A COUPLE OF MESSAGES TO BLUE (FROM RED AND GREEN) AntonPechsherov.sendMessage( "[email protected]", "Testing blue from red", "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); ShipilovAndrey.sendMessage( "[email protected]", "Testing blue from green", "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); Thread.sleep(500); // Let the server catch up
// LIST MESSAGES FOR BLUE (EXPECT MESSAGES FROM RED AND GREEN) BragnikovKonstantin.checkInbox(MailClient.SHOW_MESSAGES); } }
|
Код | import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.internet.*;
public class MailClient extends Authenticator { public static final int SHOW_MESSAGES = 1; public static final int CLEAR_MESSAGES = 2; public static final int SHOW_AND_CLEAR = SHOW_MESSAGES + CLEAR_MESSAGES;
protected String from; public Session session; protected PasswordAuthentication authentication;
public MailClient(String user, String host) { this(user, host, false); }
public MailClient(String user, String host, boolean debug) { from = user + '@' + host; authentication = new PasswordAuthentication(user, "Glist-04"); Properties props = new Properties(); props.put("mail.user", user); props.put("mail.host", host); props.put("mail.debug", debug ? "true" : "false"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "smtp"); session = Session.getInstance(props, this); }
public PasswordAuthentication getPasswordAuthentication() { return authentication; }
public void sendMessage(String to, String subject, String content) throws MessagingException { System.out.println("SENDING message from " + from + " to " + to); System.out.println(); MimeMessage msg = new MimeMessage(session); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject); msg.setText(content); Transport.send(msg); }
public void checkInbox(int mode) throws MessagingException, IOException { try { if (mode == 0) return; boolean show = (mode & SHOW_MESSAGES) > 0; boolean clear = (mode & CLEAR_MESSAGES) > 0; String action = (show ? "Show" : "") + (show && clear ? " and " : "") + (clear ? "Clear" : ""); System.out.println(action + " INBOX for " + from); Store store = session.getStore(); store.connect(); Folder root = store.getDefaultFolder(); Folder inbox = root.getFolder("inbox"); inbox.open(Folder.READ_WRITE); Message[] msgs = inbox.getMessages(); if (msgs.length == 0 && show) { System.out.println("No messages in inbox"); } for (int i = 0; i < msgs.length; i++) { MimeMessage msg = (MimeMessage)msgs[i]; if (show) { //System.out.println("From:"+msg.getFrom()); // System.out.println(" From: " + msg.getFrom()[0]);//[color=red]ЗДЕСЬ[/color] System.out.println(" Subject: " + msg.getSubject()); System.out.println(" Content: " + msg.getContent()); } if (clear) { msg.setFlag(Flags.Flag.DELETED, true); } } inbox.close(true); store.close(); System.out.println(); }catch(NullPointerException NPE){ NPE.printStackTrace(); } } }
|
\При чтение INBOX ,FROM всегда NULL , что не правильно сделанно.. ПС:пример скопи пастен
--------------------
Нехорошо блин!!!
|