package com.sxw.springboot;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailService {
/** 发信人 */
private String from;
/** 收信人 */
private String to;
/** 主题 */
private String subject;
/** 正文 */
private String body;
public MailService() {
}
public static void main(String[] args) {
MailService sender = new MailService();
sender.setFrom("354477427@qq.com");
sender.setTo("1274340854@qq.com");
// sender.setTo("sunxiaowei@intasect.net.cn");
sender.setSubject("SpringBoot学习");
sender.setBody("SpringBoot简单学习吗???!");
sender.sendMail();
}
/**
* 发送邮件.
* @return boolean - 发送结果
*/
public boolean sendMail() {
if (getBody() == null || getTo() == null || getFrom() == null
|| getSubject() == null) { return false; }
try {
Properties props = new Properties();
props.put("password", "***********"); //QQ邮箱的SMTP授权码 (不是QQ的密码)
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "465" );//qq的SMTP端口是465
Session mailSession = Session.getDefaultInstance(props);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(getFrom()));
msg.addRecipients(Message.RecipientType.TO, InternetAddress
.parse(getTo()));
msg.setSentDate(new Date());
msg.setSubject(getSubject());
msg.setText(getBody());
msg.saveChanges();
System.out.println("正在连接服务器。。。。");
Transport transport = mailSession.getTransport("smtp");
transport.connect(props.getProperty("mail.smtp.host"), props
.getProperty("username"), props.getProperty("password"));
System.out.println("正在发送邮件。。。。");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("发送完毕。。。。");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
return false;
}
return true;
}
/**
* @return Returns the body.
*/
public String getBody() {
return body;
}
/**
* @param body
* The body to set.
*/
public void setBody(String body) {
this.body = body;
}
/**
* @return Returns the from.
*/
public String getFrom() {
return from;
}
/**
* @param from
* The from to set.
*/
public void setFrom(String from) {
this.from = from;
}
/**
* @return Returns the subject.
*/
public String getSubject() {
return subject;
}
/**
* @param subject
* The subject to set.
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @return Returns the to.
*/
public String getTo() {
return to;
}
/**
* @param to
* The to to set.
*/
public void setTo(String to) {
this.to = to;
}
}
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailService {
/** 发信人 */
private String from;
/** 收信人 */
private String to;
/** 主题 */
private String subject;
/** 正文 */
private String body;
public MailService() {
}
public static void main(String[] args) {
MailService sender = new MailService();
sender.setFrom("354477427@qq.com");
sender.setTo("1274340854@qq.com");
// sender.setTo("sunxiaowei@intasect.net.cn");
sender.setSubject("SpringBoot学习");
sender.setBody("SpringBoot简单学习吗???!");
sender.sendMail();
}
/**
* 发送邮件.
* @return boolean - 发送结果
*/
public boolean sendMail() {
if (getBody() == null || getTo() == null || getFrom() == null
|| getSubject() == null) { return false; }
try {
Properties props = new Properties();
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//如果没有这行代码会出现这样的错误:Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1
props.put("username", "354477427@qq.com");props.put("password", "***********"); //QQ邮箱的SMTP授权码 (不是QQ的密码)
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "465" );//qq的SMTP端口是465
Session mailSession = Session.getDefaultInstance(props);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(getFrom()));
msg.addRecipients(Message.RecipientType.TO, InternetAddress
.parse(getTo()));
msg.setSentDate(new Date());
msg.setSubject(getSubject());
msg.setText(getBody());
msg.saveChanges();
System.out.println("正在连接服务器。。。。");
Transport transport = mailSession.getTransport("smtp");
transport.connect(props.getProperty("mail.smtp.host"), props
.getProperty("username"), props.getProperty("password"));
System.out.println("正在发送邮件。。。。");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("发送完毕。。。。");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
return false;
}
return true;
}
/**
* @return Returns the body.
*/
public String getBody() {
return body;
}
/**
* @param body
* The body to set.
*/
public void setBody(String body) {
this.body = body;
}
/**
* @return Returns the from.
*/
public String getFrom() {
return from;
}
/**
* @param from
* The from to set.
*/
public void setFrom(String from) {
this.from = from;
}
/**
* @return Returns the subject.
*/
public String getSubject() {
return subject;
}
/**
* @param subject
* The subject to set.
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @return Returns the to.
*/
public String getTo() {
return to;
}
/**
* @param to
* The to to set.
*/
public void setTo(String to) {
this.to = to;
}
}