2019独角兽企业重金招聘Python工程师标准>>>
项目要引入mail.jar包。发送文字邮件没什么可具体说的,直接看Demo。有两种方式实现,一种支持html,另一种不支持。还简单实现了添加图片附件。比较复杂的是接收邮件,特别是QQ邮箱,由于腾讯邮箱的安全机制,必须在QQ邮箱内设置开启STMP、POP等协议权限,过后会研究实现这个功能,再作补充
import java.io.IOException;
import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;public class SendMailDemo {public static void main(String[] args) throws MessagingException, IOException {/* **************** 发送邮件 *************** */final Properties properties = System.getProperties();properties.put("mail.smtp.auth", "true");properties.put("mail.smtp.host", "smtp.126.com");//host的协议根据自己的邮箱决定properties.put("mail.user", "邮箱帐号@126.com");properties.put("mail.password", "邮箱密码");//认证信息Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication(){String account = properties.getProperty("mail.user");String password = properties.getProperty("mail.password");return new PasswordAuthentication(account, password);}};//获取邮件sessionSession session = Session.getInstance(properties, authenticator);MimeMessage mimeMessage = new MimeMessage(session);//发送人InternetAddress from = new InternetAddress(properties.getProperty("mail.user"));mimeMessage.setFrom(from);//接收人InternetAddress accept = new InternetAddress("接收邮箱帐号@qq.com");/*** Type参数说明* Message.RecipientType.TO 收件人* Message.RecipientType.CC 抄送* Message.RecipientType.BCC 密送*/mimeMessage.setRecipient(RecipientType.BCC, accept);//主题mimeMessage.setSubject("this is a test mail to Lin");//设置正文方式一:直接添加,支持html//mimeMessage.setContent("请访问我们的官网", "text/html;charset=utf-8");//设置正文方式二:multipart,支持但是不支持html(可添加附件)Multipart multipart = new MimeMultipart();BodyPart messageBodyPart = new MimeBodyPart();messageBodyPart.setText("请访问我们的官网");multipart.addBodyPart(messageBodyPart);//添加附件/*messageBodyPart = new MimeBodyPart();String filename = "G:/MyImages/海贼王5.jpeg?s=#34;;DataSource source = new FileDataSource(filename);messageBodyPart.setDataHandler(new DataHandler(source));messageBodyPart.setFileName(filename);multipart.addBodyPart(messageBodyPart);*/mimeMessage.setContent(multipart);//发送邮件Transport.send(mimeMessage);/* **************** 接收邮件 *************** */final Properties recevieprop = System.getProperties();recevieprop.put("mail.pop3.host", "pop.126.com");recevieprop.put("mail.user", "邮箱帐号@126.com");recevieprop.put("mail.password", "邮箱密码");//recevieprop.put("mail.pop3.ssl.enable", "true");//QQ邮箱需要设置sslStore store = session.getStore("pop3");store.connect(recevieprop.getProperty("mail.pop3.host"), recevieprop.getProperty("mail.user"), recevieprop.getProperty("mail.password"));Folder folder = store.getFolder("INBOX");folder.open(Folder.READ_ONLY); //int size = folder.getMessageCount();Message[] messages = folder.getMessages();System.out.println(messages.length);}}