Java by API/javax.mail/Session — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:10, 31 мая 2010

Session: getFolder(URLName name)

 
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.URLName;
public class Main {
  public static void main(String[] args) throws Exception {
    URLName server = new URLName("protocol://username:password@host/foldername");
    Session session = Session.getDefaultInstance(new Properties(), null);
    Folder folder = session.getFolder(server);
    if (folder == null) {
      System.out.println("Folder " + server.getFile() + " not found.");
      System.exit(1);
    }
    folder.open(Folder.READ_ONLY);
    // Get the messages from the server
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
      System.out.println("------------ Message " + (i + 1) + " ------------");
      messages[i].writeTo(System.out);
    }
    folder.close(false);
  }
}





Session: getInstance(Properties arg0, Authenticator arg1)

 
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Main {
  public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put("mail.host", "mail.cloud9.net");
    Session mailConnection = Session.getInstance(props, null);
    Message msg = new MimeMessage(mailConnection);
    Address a = new InternetAddress("a@a.ru", "A a");
    Address b = new InternetAddress("fake@jexp.ru");
    msg.setContent("Mail contect", "text/plain");
    msg.setFrom(a);
    msg.setRecipient(Message.RecipientType.TO, b);
    msg.setSubject("subject");
    Transport.send(msg);
  }
}





Session: getStore(String name)

 
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
public class Main {
  public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    String host = "yourserver.edu";
    String provider = "pop3";
    Session session = Session.getDefaultInstance(props, new MailAuthenticator());
    Store store = session.getStore(provider);
    store.connect(host, null, null);
    Folder inbox = store.getFolder("INBOX");
    if (inbox == null) {
      System.out.println("No INBOX");
      System.exit(1);
    }
    inbox.open(Folder.READ_ONLY);
    Message[] messages = inbox.getMessages();
    for (int i = 0; i < messages.length; i++) {
      System.out.println("Message " + (i + 1));
      messages[i].writeTo(System.out);
    }
    inbox.close(false);
    store.close();
  }
}
class MailAuthenticator extends Authenticator {
  public MailAuthenticator() {
  }
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username", "password");
  }
}