java收发邮件
package org.business.wsclient; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; public class test { public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { JavaMailSenderImpl jms = new JavaMailSenderImpl(); jms.setHost("smtp.qq.com"); jms.setPort(465); jms.setUsername("1548703562@qq.com"); jms.setPassword("申请码"); Properties p = new Properties(); //p.put("mail.smtp.auth", "true"); p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getInstance(p); //开启调试模式 session.setDebug(true); jms.setSession(session); //发送简单文本邮件 SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("1548703562@qq.com"); message.setTo("winclpt@126.com"); message.setText("spring mail test!/r/n hello world"); message.setSubject("spring mail"); //发送复杂邮件 MimeMessage mime = jms.createMimeMessage(); mime.setFrom(new InternetAddress("1548703562@qq.com")); mime.setRecipient(RecipientType.TO, new InternetAddress("winclpt@126.com")); mime.setSubject("附件测试"); BodyPart b1 = new MimeBodyPart(); b1.setText("这是一个包含附件的复杂邮件"); BodyPart b2 = new MimeBodyPart(); DataSource ds = new FileDataSource("G:\\603.png"); b2.setDataHandler(new DataHandler(ds)); b2.setFileName("603.png"); Multipart mp = new MimeMultipart(); mp.addBodyPart(b1); mp.addBodyPart(b2); mime.setContent(mp); jms.send(mime); //pop3获取邮件 Properties props = new Properties(); props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.pop3.host", "pop.qq.com"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "pop3"); props.setProperty("mail.pop3.port", "995"); Session s = Session.getDefaultInstance(props); //s.setDebug(true); Store store = s.getStore("pop3"); store.connect("pop.qq.com","1548703562@qq.com","申请码"); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message msg[] = folder.getMessages(); System.out.println("共:"+msg.length); for(Message m : msg){ System.out.println(MimeUtility.decodeText(m.getSubject())); }
//imap收取邮件 Properties props = new Properties(); props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.imap.host", "imap.qq.com"); props.put("mail.store.protocol", "imap"); props.put("mail.transport.protocol", "imap"); props.setProperty("mail.imap.port", "993"); Session s = Session.getDefaultInstance(props); s.setDebug(true); Store store = s.getStore("imap"); store.connect("imap.qq.com","1548703562@qq.com","uclxexdmgrxdhfjb"); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); System.err.println(folder.getFullName());; Message msg[] = folder.getMessages(); // 打印不同状态的邮件数量 System.out.println("收件箱中共" + msg.length + "封邮件!"); System.out.println("收件箱中共" + folder.getUnreadMessageCount() + "封未读邮件!"); System.out.println("收件箱中共" + folder.getNewMessageCount() + "封新邮件!"); System.out.println("收件箱中共" + folder.getDeletedMessageCount() + "封已删除邮件!");*/ folder.close(false); store.close(); } }