SMTP是一种提供可靠且有效的电子邮件传输的协议。
SMTP协议:SMTP是建立在FTP文件传输服务上的一种邮件服务,主要用于系统之间的邮件信息传递,并提供有关来信的通知。SMTP独立于特定的传输子系统,且只需要可靠有序的数据流信道支持,SMTP的重要特性之一是其能跨越网络传输邮件,即“SMTP邮件中继”。使用SMTP,可实现相同网络处理进程之间的邮件传输,也可通过中继器或网关实现某处理进程与其他网络之间的邮件传输。
QQ邮箱和网易邮箱均可通过SMTP协议实现发邮件,具体设置如下: 如何开启QQ邮箱SMTP协议 如何获取QQ邮箱授权码 如何开启网易邮箱SMTP协议 如何获取网易邮箱授权码
发送邮件代码如下:
- pom文件中引包
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
- 定义邮件类
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data
@Slf4j
public class MailSender {
private String mailServerHost;
private String mailServerPort;
private String sendEmailAddress;
private String sendEmailName;
private String sendEmailPassword;
private String receiveEmailAddress;
private String ccEmailAddress;
private String emailSubject;
private String emailType;
private String emailContent;
}
- 定义SMTP邮件发送工具类
import com.alibaba.fastjson.JSONObject;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
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.MimeMessage;
import java.util.Properties;
@Component
@Slf4j
public class SendEmail {
@SneakyThrows
public void sendEmail(MailSender mailSender) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.setProperty("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.host", mailSender.getMailServerHost());
props.put("mail.smtp.port", mailSender.getMailServerPort());
props.put("mail.user", mailSender.getSendEmailAddress());
props.put("mail.password", mailSender.getSendEmailPassword());
Session session = Session.getInstance(props, new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(mailSender.getSendEmailAddress(), mailSender.getSendEmailPassword());
}
});
MimeMessage message = new MimeMessage(session);
if (mailSender.getSendEmailName() == null) {
message.setFrom(new InternetAddress(mailSender.getSendEmailAddress()));
} else {
message.setFrom(new InternetAddress(mailSender.getSendEmailAddress(), mailSender.getSendEmailName(), "UTF-8"));
}
String[] receive = mailSender.getReceiveEmailAddress().split(",");
message.setRecipient(Message.RecipientType.TO, new InternetAddress(receive[0]));
if (receive.length > 1) {
for (int i = 1; i < receive.length; i++) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive[i]));
}
}
message.setSubject(mailSender.getEmailSubject());
if ("html".equals(mailSender.getEmailType())) {
message.setText(mailSender.getEmailContent(), "UTF-8", "html");
} else {
message.setText(mailSender.getEmailContent(), "UTF-8");
}
Transport.send(message);
}
}
- QQ邮箱发送
@Autowired
private SendEmail sendEmail;
MailSender qqMailSender = new MailSender();
qqMailSender.setMailServerHost("smtp.qq.com");
qqMailSender.setMailServerPort("465");
sendEmail.sendEmail(qqMailSender);
- 网易邮箱发送
@Autowired
private SendEmail sendEmail;
MailSender wyMailSender = new MailSender();
wyMailSender.setMailServerHost("smtp.163.com");
wyMailSender.setMailServerPort("465");
sendEmail.sendEmail(wyMailSender);
- SMTP邮箱发送
@Autowired
private SendEmail sendEmail;
MailSender smtpMailSender = new MailSender();
sendEmail.sendEmail(smtpMailSender);
|