Java by API/javax.mail/Part

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

Part.ATTACHMENT

 
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
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@host/foldername");
    Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
    Folder folder = session.getFolder(server);
    if (folder == null) {
      System.out.println("Folder " + server.getFile() + " not found.");
      System.exit(1);
    }
    folder.open(Folder.READ_ONLY);
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
      System.out.println(messages[i].getSize() + " bytes long.");
      System.out.println(messages[i].getLineCount() + " lines.");
      String disposition = messages[i].getDisposition();
      if (disposition == null){
        ; // do nothing
      }else if (disposition.equals(Part.INLINE)) {
        System.out.println("This part should be displayed inline");
      } else if (disposition.equals(Part.ATTACHMENT)) {
        System.out.println("This part is an attachment");
        String fileName = messages[i].getFileName();
        System.out.println("The file name of this attachment is " + fileName);
      }
      String description = messages[i].getDescription();
      if (description != null) {
        System.out.println("The description of this message is " + description);
      }
    }
    folder.close(false);
  }
}
class MailAuthenticator extends Authenticator {
  public MailAuthenticator() {
  }
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username", "password");
  }
}





Part: getAllHeaders()

 
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
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@host/foldername");
    Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
    Folder folder = session.getFolder(server);
    if (folder != null) {
      folder.open(Folder.READ_ONLY);
      Message[] messages = folder.getMessages();
      for (int i = 0; i < messages.length; i++) {
        System.out.println((i + 1));
        Enumeration headers = messages[i].getAllHeaders();
        while (headers.hasMoreElements()) {
          Header h = (Header) headers.nextElement();
          System.out.println(h.getName() + ": " + h.getValue());
        }
      }
      folder.close(false);
    }
  }
}
class MailAuthenticator extends Authenticator {
  public MailAuthenticator() {
  }
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username", "password");
  }
}





Part.getDescription()

 
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
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@host/foldername");
    Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
    Folder folder = session.getFolder(server);
    if (folder == null) {
      System.out.println("Folder " + server.getFile() + " not found.");
      System.exit(1);
    }
    folder.open(Folder.READ_ONLY);
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
      System.out.println(messages[i].getSize() + " bytes long.");
      System.out.println(messages[i].getLineCount() + " lines.");
      String disposition = messages[i].getDisposition();
      if (disposition == null){
        ; // do nothing
      }else if (disposition.equals(Part.INLINE)) {
        System.out.println("This part should be displayed inline");
      } else if (disposition.equals(Part.ATTACHMENT)) {
        System.out.println("This part is an attachment");
        String fileName = messages[i].getFileName();
        System.out.println("The file name of this attachment is " + fileName);
      }
      String description = messages[i].getDescription();
      if (description != null) {
        System.out.println("The description of this message is " + description);
      }
    }
    folder.close(false);
  }
}
class MailAuthenticator extends Authenticator {
  public MailAuthenticator() {
  }
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username", "password");
  }
}





Part.INLINE

 
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
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@host/foldername");
    Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
    Folder folder = session.getFolder(server);
    if (folder == null) {
      System.out.println("Folder " + server.getFile() + " not found.");
      System.exit(1);
    }
    folder.open(Folder.READ_ONLY);
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
      System.out.println(messages[i].getSize() + " bytes long.");
      System.out.println(messages[i].getLineCount() + " lines.");
      String disposition = messages[i].getDisposition();
      if (disposition == null){
        ; // do nothing
      }else if (disposition.equals(Part.INLINE)) {
        System.out.println("This part should be displayed inline");
      } else if (disposition.equals(Part.ATTACHMENT)) {
        System.out.println("This part is an attachment");
        String fileName = messages[i].getFileName();
        System.out.println("The file name of this attachment is " + fileName);
      }
      String description = messages[i].getDescription();
      if (description != null) {
        System.out.println("The description of this message is " + description);
      }
    }
    folder.close(false);
  }
}
class MailAuthenticator extends Authenticator {
  public MailAuthenticator() {
  }
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username", "password");
  }
}





Part: setContent(Object obj, String type)

 
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);
  }
}





Part: writeTo(OutputStream out)

 
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);
  }
}