方式一
- 说明:使用javamail接口实现,不要求为JavaWeb项目
1. QQ邮件设置
- 进入QQ邮箱,点击设置
- 开启P0P3/SMTP服务。并记录授权码(之后配置使用)
2. 依赖
<!-- 邮件 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
3. 工具类
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
*
* 邮件发送
* QQ邮箱---> 别的邮箱
*/
public class netEmailTest extends Thread {
private String mailAdr;// 邮箱
private String content;// 邮件的内容
private String subject;// 邮件的题目
public netEmailTest(String mailAdr, String subject, String content) {
super();
this.mailAdr = mailAdr;
this.subject = subject;
this.content = content;
}
public void run() {
super.run();
try {
sendMail(mailAdr, subject, content);
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMail(String mailAdr, String subject, String content) throws Exception {
// 加密的邮件套接字协议工厂
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
final Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// smtp登陆的账号、密码 ;需开启smtp登陆
//改 "发送者邮箱","授权码"
//改 "发送者邮箱","授权码"
//改 "发送者邮箱","授权码"
props.setProperty("mail.debug", "true");
props.put("mail.user", "发送者邮箱");
props.put("mail.password", "授权码");
// 特别需要注意,要将ssl协议设置为true,否则会报530错误
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
try {
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人
InternetAddress to = new InternetAddress(mailAdr);
message.setRecipient(Message.RecipientType.TO, to);
// 设置抄送
// InternetAddress cc = new InternetAddress("591566764@qq.com");
// message.setRecipient(RecipientType.CC, cc);
// 设置密送,其他的收件人不能看到密送的邮件地址
// InternetAddress bcc = new InternetAddress("mashen@163.com");
// message.setRecipient(RecipientType.CC, bcc);
// 设置邮件标题
message.setSubject(subject);
// 设置邮件的内容体
message.setContent(content, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
4. 测试
public static void main(String[] args) {
netEmailTest d = new netEmailTest("接收邮件的邮箱账号", "syp:", "我呵呵,啊打: <br/><br/>加油哦!!!!....");
d.start();
}
}
方式二
注意:springboot项目,spring框架下使用
1. QQ邮件设置
- 进入QQ邮箱,点击设置
- 开启P0P3/SMTP服务。并记录授权码(之后配置使用)
2. 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
3. 配置
- 说明:application.yaml文件编写
spring:
thymeleaf:
cache: false
mail:
username: 906336773@qq.com
# 授权码
password: xxxx
host: smtp.qq.com
# 加密验证
properties:
mail.smtp.ssl.enable: true
4. 工具类
public class EmailUtil {
@Autowired
JavaMailSenderImpl mailSender;
//简单的邮件发送
public void sentMail() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject("邮件的题目");
simpleMailMessage.setText("邮件的内容");
simpleMailMessage.setTo("906336773@qq.com");
simpleMailMessage.setFrom("906336773@qq.com");
mailSender.send(simpleMailMessage);
}
//较复杂的邮件发送
public void sentMail2() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("邮件的题目");
helper.setText("邮件的内容");
// 附件
helper.addAttachment("附件名",new File("C:\\xxxx"));
helper.addAttachment("附件名2",new File("C:\\xxxx"));
helper.setTo("906336773@qq.com");
helper.setFrom("906336773@qq.com");
mailSender.send(mimeMessage);
}
}