javamail - 发件、收件(SSL连接)

时间:2022-03-23 13:52:14

需要的包:mail.jar  jsse.jar(说明:jsse.jar是jre自带的,如果jdk1.5及以下需要Java Activation Framework框架的activation.jar)

【发件】

import java.net.URL;
import java.security.Security;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; //发送一封图文加附件的邮件
public class SendMail {
public static void main(String[] args) {
try {
//设置SSL连接、邮件环境
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.qq.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
// props.put("mail.smtp.host", "smtp.qq.com");
// props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
// props.put("mail.smtp.socketFactory.fallback", "false");
// props.put("mail.smtp.port", "465");
// props.put("mail.smtp.socketFactory.port", "465");
// props.put("mail.smtp.auth", "true");
//建立邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator() {
//身份认证
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
//建立邮件对象
MimeMessage message = new MimeMessage(session);
//设置邮件的发件人、收件人、主题
//附带发件人名字
// message.setFrom(new InternetAddress("from_mail@qq.com", "optional-personal"));
message.setFrom(new InternetAddress("from_mail@qq.com"));
message.setRecipients(Message.RecipientType.TO, "to_mail@qq.com");
message.setSubject("通过javamail发出!!!");
//文本部分
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("图<img src='cid:myimg'/>文加附件邮件测试", "text/html;charset=UTF-8");
//内嵌图片部分
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setDataHandler(new DataHandler(new FileDataSource("imagePath")));//图片路径
imagePart.setContentID("myimg");
//图文整合,关联关系
MimeMultipart mmp1 = new MimeMultipart();
mmp1.addBodyPart(textPart);
mmp1.addBodyPart(imagePart);
mmp1.setSubType("related");
MimeBodyPart textImagePart = new MimeBodyPart();
textImagePart.setContent(mmp1);
//附件部分
MimeBodyPart attachmentPart = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("filePath"));//文件路径
String fileName = dh.getName();
attachmentPart.setDataHandler(dh);
attachmentPart.setFileName(fileName);
//图文和附件整合,复杂关系
MimeMultipart mmp2 = new MimeMultipart();
mmp2.addBodyPart(textImagePart);
mmp2.addBodyPart(attachmentPart);
mmp2.setSubType("mixed");
//将以上内容添加到邮件的内容中并确认
message.setContent(mmp2);
message.saveChanges();
//发送邮件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}

【收件】

import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Properties; import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility; //查看收件箱邮件
public class Inbox {
public static void main(String[] args) {
//设置SSL连接、邮件环境
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.pop3.host", "smtp.qq.com");
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.setProperty("mail.pop3.auth", "true");
// props.put("mail.pop3.host", "smtp.qq.com");
// props.put("mail.pop3.socketFactory.class", SSL_FACTORY);
// props.put("mail.pop3.socketFactory.fallback", "false");
// props.put("mail.pop3.port", "995");
// props.put("mail.pop3.socketFactory.port", "995");
// props.put("mail.pop3.auth", "true");
//建立邮件会话
Session session = Session.getDefaultInstance(props, null);
//设置连接邮件仓库的环境
URLName url = new URLName("pop3", "pop.qq.com", 995, null, "username", "password");
Store store = null;
Folder inbox = null;
try {
//得到邮件仓库并连接
store = session.getStore(url);
store.connect();
//得到收件箱并抓取邮件
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
Message[] messages = inbox.getMessages();
inbox.fetch(messages, profile);
//打印收件箱邮件部分信息
int length = messages.length;
System.out.println("收件箱的邮件数:" + length);
System.out.println("-------------------------------------------\n");
for (int i = 0; i < length; i++) {
String from = MimeUtility.decodeText(messages[i].getFrom()[0].toString());
InternetAddress ia = new InternetAddress(from);
System.out.println("发件人:" + ia.getPersonal() + '(' + ia.getAddress() + ')');
System.out.println("主题:" + messages[i].getSubject());
System.out.println("邮件大小:" + messages[i].getSize());
System.out.println("邮件发送时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(messages[i].getSentDate()));
System.out.println("-------------------------------------------\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inbox.close(false);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
store.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}