首页 > 编程语言 >Java实现发送邮件

Java实现发送邮件

时间:2022-12-21 14:40:40浏览次数:47  
标签:Java String import 发送 new msg mail public 邮件


Java实现发送邮件

摘要:在做Java开发、安卓开发、web开发时,经常遇到邮箱绑定、邮箱找回密码等功能,在使用绑定邮箱并依靠邮箱发送验证码时,需要利用Java实现发送邮件的功能。本人自主开发的诸多项目中也均有发送邮件功能,现整理为开源代码(以QQ邮箱为例):

(1)需要的jar包为 mail.jar

(2)需要在邮箱进行设置如图:以QQ邮箱为例,进入个人邮箱界面,点击设置;

Java实现发送邮件_发送邮件


点击账户,并向下找到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”

Java实现发送邮件_java开发_02


按下图设置开启项,并获取秘钥,将秘钥拷贝至自己的项目中(见下面的程序)

Java实现发送邮件_安卓开发_03


(3)创建类SendEmail,并编写如下程序。

package com.estudy.email;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
public static final String HOST = "smtp.exmail.qq.com";
public static final String PROTOCOL = "smtp";
public static final int PORT = 587;
public static final String FROM = "***@qq.com";//发件人的email
public static final String PWD = "*****";//发件人密码
public static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

/**
* 获取Session
* @return
*/
public static Session getSession() {
Properties props = new Properties();
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");

Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM, PWD);
}
};
//Session session = Session.getDefaultInstance(props , authenticator);
Session session = Session.getInstance(props, authenticator);
return session;
}

//向toEmail发送邮件
public void send(String toEmail , String content) {
Session session = getSession();
try {
System.out.println("--send--"+content);
// Instantiate a message
Message msg = new MimeMessage(session);

//Set message attributes
msg.setFrom(new InternetAddress(FROM));
InternetAddress[] address = {new InternetAddress(toEmail)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("这是邮件的标题");
msg.setSentDate(new Date());
msg.setContent(content, "text/html;charset=utf-8");//content作为参数,这是邮件的内容
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}

//向toEmails群发邮件
public void send(String[] toEmails , String content) {
Session session = getSession();
try {
System.out.println("--send--"+content);
// Instantiate a message
Message msg = new MimeMessage(session);

//Set message attributes
msg.setFrom(new InternetAddress(FROM));
InternetAddress[] address = new InternetAddress[toEmails.length];
for(int i=0;i<toEmails.length;i++){//将群发的所有邮箱地址构建群发队列数组
address[i] = new InternetAddress(toEmails[i]);
}
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("这里是邮件的标题");
msg.setSentDate(new Date());
msg.setContent(content , "text/html;charset=utf-8");

//Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
public static void main(String []args){
SendEmail sendEmail = new SendEmail();
sendEmail.send("***@qq.com", "测试邮件");
}
}

博客记录着学习的脚步,分享着最新的技术,非常感谢您的阅读,本博客将不断进行更新,希望能够给您在技术上带来帮助。

标签:Java,String,import,发送,new,msg,mail,public,邮件
From: https://blog.51cto.com/u_15919249/5959889

相关文章