了解
电子邮件要在网络上实现邮件功能,必须要有专门的邮件服务器。
这些邮件服务器类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中。
SMTP服务器地址:一般是 smtp.xxx.com,比如163邮箱是smtp.163.com,qq邮箱是smtp.qq.com。
使用Java发送 E-mail 十分简单,但是首先你应该准备 JavaMail API 和Java Activation Framework 。
得到两个jar包:
- mail.jar
- activation.jar
导入纯文本邮件
1.导包并获取权限
先导入上面说的2个jar包,然后从QQ邮箱中获取对应的权限:在qq邮箱设置-账户-开启POP3/SMTP服务可以获得授权码
2.编写代码
package com.yamobo;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com");//设置qq邮件服务器
prop.setProperty("mail.transport.protocol","smtp");//邮件发送协议
prop.setProperty("mail.smtp.auth","true");//需要验证服务器密码
//qq邮箱还需要设置SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true");
prop.put("mail.smtp.ssl.socketFactory",sf);
//使用JavaMail发送邮件的五个步骤
//1.创建定义整个应用程序所需的环境信息的session对象
Session session= Session.getDefaultInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication(){
//发件人邮件用户名,授权码
return new PasswordAuthentication("发件人qq邮箱地址","授权码");
}
});
//开启Session的debug模式,这样可以查看程序发送的Email运行状态
session.setDebug(true);
//2.通过session得到transport对象
Transport ts = session.getTransport();
//3.使用邮箱的用户名和授权码连上邮件服务器
ts.connect("smtp.qq.com","发件人qq邮箱地址","授权码");
//4.创建邮件
MimeMessage message = new MimeMessage(session);
//设置邮件的基本信息
指明发件人
message.setFrom(new InternetAddress("发件人qq邮箱地址"));
//指明收件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人qq邮箱地址"));
//标题
message.setSubject("纯文本测试");
//文本内容
message.setContent("收到了吗","text/html;charset=UTF-8");
//5.发送邮件
ts.sendMessage(message,message.getAllRecipients());
ts.close();
}
}
带图片和附件的复杂邮件发送
package com.yamobo;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class SendFileEmail {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com");
prop.setProperty("mail.transport.protocol","smtp");
prop.setProperty("mail.smtp.auth","true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
//发件人邮件用户名、授权码
return new PasswordAuthentication("@qq.com", "bbpuvjfyzeflbebf");
}
});
session.setDebug(true);
Transport ts = session.getTransport();
ts.connect("smtp.qq.com", "@qq.com", "bbpuvjfyzeflbebf");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("@qq.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("@qq.com"));
message.setSubject("带附件和带图片的的邮件");
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src/resources/bz.jpg")));
image.setContentID("bz.jpg");
//正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("不用啊<br/><img src='cid:bz.jpg'>","text/html;charset=UTF-8");
//附件1
MimeBodyPart attach = new MimeBodyPart();
attach.setDataHandler(new DataHandler(new FileDataSource("src/resources/1.txt")));
attach.setFileName("1.txt");
//描述关系:正文和图片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related");
//描述关系:正文和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
//代表正文的bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
//发送邮件
message.setContent(mp2);
message.saveChanges();ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
}