package mail; import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.NoSuchProviderException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Store; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import javax.mail.internet.MimeMessage.RecipientType; /******************************************************************************* * @author hanwesley java Mail exaple */ public class Email { private static String context; public void setContext(String context) { this.context = context; } public static String getContext() { return context; } public ArrayList<MimeBodyPart> getAttachmentMultipart(String[] ss) throws Exception { ArrayList<MimeBodyPart> parts = null; if (ss != null && ss.length > 0) { int filesize = 0; for (String s : ss) { File f = new File(s); filesize += f.length(); if (filesize > 1 * 1024 * 1024) { throw new Exception("attarch maxed!"); } parts = new ArrayList<MimeBodyPart>(); for (int i = 0; i < ss.length; i++) { MimeBodyPart mbp = new MimeBodyPart(); FileDataSource fds = new FileDataSource(ss[i]); mbp.setDataHandler(new DataHandler(fds)); mbp.setFileName(MimeUtility.encodeText(fds.getName(), "GB2312", "B")); parts.add(mbp); } } } return parts; } public Session getSession() { Properties prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); prop.put("mail.smtp.host", "smtp.163.com"); prop.put("mail.smtp.port", 25); prop.put("mail.from", "[email protected]"); prop.put("mail.smtp.auth", "true"); Session session = null;// JavaMail核心类 一个会话 session = Session.getDefaultInstance(prop, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "123456"); } }); // session=Session.getDefaultInstance(prop,null); session.setDebug(true); return session; } public void sendMessage(Message msg) throws Exception { Transport transport = getSession().getTransport(); try { // msg.setc transport.connect(); // transport.connect("smtp.163.com", 25, // "[email protected]","123456"); transport.send(msg); } catch (MessagingException e) { e.printStackTrace(); } finally { try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } } public void receiveMessage() throws Exception{ Session session=getSession(); Store store=session.getStore("pop3"); store.connect("pop.163.com", "[email protected]", "123456"); Folder inbox=store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); Message msg=inbox.getMessage(1); System.out.println(msg.getSubject()); } public void testSendMessager() throws Exception { Session session = getSession(); Message msg = new MimeMessage(session); msg.setFrom(); msg.setSubject("hello,world"); String s = "[email protected],[email protected]"; msg.setRecipients(RecipientType.TO, InternetAddress.parse(s)); msg.setRecipient(RecipientType.CC, new InternetAddress( "[email protected]")); // msg.setText(""); msg.setSentDate(new Date()); String[] arr = new String[] { "d:\\two.txt" }; Multipart mp = new MimeMultipart("mixed"); MimeBodyPart mbp = new MimeBodyPart(); mbp.setContent("it's a JavaMail example!<br/>hehe", "text/html;charset=GB2312"); mp.addBodyPart(mbp); ArrayList<MimeBodyPart> mbparr = getAttachmentMultipart(arr); for (MimeBodyPart mb : mbparr) { mp.addBodyPart(mb); } msg.setContent(mp); sendMessage(msg); } public static void main(String[] args) throws Exception { Email email = new Email(); email.receiveMessage(); } }