JAVA MAIL试用

1、发送邮件:
public class SendMsg {
   public static void main1(String[] args) {
    String to = "[email protected]";
    String to2 = "[email protected]";
    String from = "[email protected]";
    String host = "smtp.163.com";

    Properties props = new Properties();
    props.put( "mail.smtp.host", host);
    props.put( "mail.smtp.auth", "true");
    Authenticator auth = new Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication( "test", "test");
      }
    };
    Session session = Session.getInstance(props, auth);
    session.setDebug( true);
     try {
      MimeMessage msg = new MimeMessage(session);
      msg.setFrom( new InternetAddress(from));
      InternetAddress[] address = { new InternetAddress(to), new InternetAddress(to2) };
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject( "JavaMail API测试");
      msg.setSentDate( new Date());

      MimeBodyPart body = new MimeBodyPart();
      body.setText( "这里是邮件的正文");

      MimeBodyPart attach1 = new MimeBodyPart();
      attach1.attachFile( new File( "build.xml"));
      MimeBodyPart attach2 = new MimeBodyPart();
      attach2.attachFile( new File( "F://FtpList.rar"));

      Multipart mp = new MimeMultipart();
      mp.addBodyPart(body);
      mp.addBodyPart(attach1);
      mp.addBodyPart(attach2);
      msg.setContent(mp);
      Transport.send(msg);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
查看发送结果:
 
2、接受邮件:
public class GetMessage {
   public static void main(String[] args) {
    String host = "pop3.163.com";
    Properties props = System.getProperties();
    Session session = Session.getDefaultInstance(props, null);
     // session.setDebug(true);
     try {
      Store store = session.getStore( "pop3");
      store.connect(host, "test", "test");
      Folder folder = store.getDefaultFolder();
      folder = folder.getFolder( "INBOX");
      folder.open(Folder.READ_ONLY);
      System.out.println(folder.getNewMessageCount());
      System.out.println(folder.getDeletedMessageCount());
      Message[] msgs = folder.getMessages();
      System.out.println(msgs.length);
       for ( int i = 0; i < msgs.length; i++) {
         if (msgs[i].getSubject().startsWith( "JavaMail")) {
          System.out.println(getStrMsg(msgs[i]));
           break;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

   private static String getStrMsg(Message msg) throws MessagingException, IOException {
    StringBuffer sb = new StringBuffer(128);
    sb.append( "FROM:");
    Address[] froms = msg.getFrom();
     for (Address ad : froms) {
      sb.append(ad);
    }
    sb.append( "\nTO:");
    Address[] tos = msg.getRecipients(Message.RecipientType.TO);
     for (Address ad : tos) {
      sb.append(ad);
    }
    sb.append( "\nSUBJECT:" + msg.getSubject());
    sb.append( "\nSEND DATE:" + msg.getSentDate());
    Object content = msg.getContent();
     if (content instanceof Multipart) {
      BodyPart part = ((Multipart) content).getBodyPart(0);
      String contentType = part.getContentType();
      sb.append( "\nCONTENT TYPE:" + contentType);
       if (contentType.startsWith( "text")) {
        sb.append( "\nCONTENT:");
        BufferedReader reader = new BufferedReader( new InputStreamReader(part
            .getInputStream()));
        String line = reader.readLine();
         while (line != null) {
          sb.append( "\n" + line);
          line = reader.readLine();
        }
      }
    }
     return sb.toString();
  }
}
 
运行结果:
getDefaultFolder ''
0
0
77
FROM:[email protected]
TO:[email protected]
SUBJECT:JavaMail API测试
SEND DATE:Sun Apr 19 00:01:59 CST 2009
CONTENT TYPE:text/plain; charset=GBK
CONTENT:
这里是邮件的正文

你可能感兴趣的:(职场,休闲)