邮件发送

文章所用jar文件

链接:https://pan.baidu.com/s/1YaxhdkaCTC4TUDL-y9-ASQ 
提取码:30ow 

程序入口,发送工具类

package test;
 
import org.apache.commons.mail.EmailException;
 
/**
 * 邮箱发送工具类
 * @author Administrator
 *
 */
public class EmailUtil {
	public static void main(String[] args) throws EmailException {
		//自定义工具类
		EmailUtils EmailUtils = new EmailUtils();
		//返回0表示发送失败 返回1表示发送成功
		int o = EmailUtils.sendSimpleEmail("123465@qq.com", "名称", "Java send Email", "测试");
		if(o==0){
			System.out.println("发送失败");
		}else{
			System.out.println("发送成功");
		}
	}
}

发送简单邮件方法类

package test;
 
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
 
public class EmailUtils{
	/**
	 * 发送简单邮件方法
	 * @param emailAddress
	 * 发送地址
	 * @param sendName
	 * 发件人名
	 * @param title
	 * 邮件标题
	 * @param message
	 * 邮件内容
	 * @return
	 * 返回0表示发送失败 返回1表示发送成功
	 * @throws EmailException
	 */
	public int sendSimpleEmail(String emailAddress, String sendName, String title,  String message) throws EmailException{
		//要发送的服务器 地址
		String hostName="smtp.qq.com";//对方邮箱是什么类型,这里是QQ邮箱的服务器地址
		//要发送的服务器 邮箱账号
		String userName="自己的邮箱地址";
		//要发送的服务器 邮箱密码
		String password="自己的邮箱密码";
		//要发送的端口   587   465   25
		int smtpPort=587;
		//是否使用ssl加密协议
		boolean SSL=true;
		try {
			SimpleEmail email = new SimpleEmail();
			//smtp address
			email.setHostName(hostName);
			//smtp port
			email.setSmtpPort(smtpPort);
			//账号 密码
			email.setAuthenticator(new DefaultAuthenticator(userName, password));
			//设置发件人地址
			email.setFrom(userName);
			//设置邮件标题
			email.setSubject(title);
			//设置邮件主体
			//email.setMsg(message);
			email.setContent(message, "text/html;charset=UTF-8");//解决乱码
			//设置收件人地址
			email.addTo(emailAddress);
			//设置是否使用SSL加密
			email.setSSL(SSL);
			//发送邮件
			email.send();
		} catch (Exception e) {
			//异常
			return 0;
		}
		//正常
		return 1;
	}
}

邮件发送控制器

package test;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class SendEmailServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 前端form表单post请求--防止请求提交乱码.
		request.setCharacterEncoding("utf-8");
		// 获取前端传入的参数
		try {
			// 邮件发送处理
			SendEmailUtil.sendMail("2249119735@qq.com", "邮件测试");
			// 将发送信息保存到session中
			request.getSession().setAttribute("send_msg", "邮件发送成功!");
			// 页面跳转到发送结果页面
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.getSession().setAttribute("send_msg", "邮件发送失败!");
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}

核心发送方法

package test;
 
import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendEmailUtil {
	public static boolean sendMail(String emailAddress, String emailMsg) {
		// 谁发送
		String from = "1535765656@qq.com";
		// 发给谁
		String to = emailAddress;
		// 发送者的用户名和密码(邮箱登录用)
		final String username = "1535765656@qq.com"; // 此处填写发送的邮箱名
		final String password = "ypvjnbyqelcsihag"; // 此处填写登录的邮箱密码
		// 定义properties对象,设置环境信息
		Properties properties = new Properties();
		/*
		 * mail.smtp.host :指定连接的邮件服务器的主机名。如:163邮箱就填写smtp.163.com 
		 * 若在本地测试的话,需要在本地安装smtp服务器
		 */
		properties.setProperty("mail.smtp.host", "smtp.qq.com");
		// mail.smtp.auth:指定客户端是否要向邮件服务器提交验证
		properties.setProperty("mail.smtp.auth", "true");
		/*
		 * mail.transport.protocol:指定邮件发送协议:smtp。smtp:发邮件;pop3:收邮件
		 * mail.store.protocol:指定邮件接收协议
		 */
		properties.setProperty("mail.transport.protocol", "smtp");
		// 获取session对象
		Session session = Session.getInstance(properties);
		// 当设置为true,JavaMail AP就会将其运行过程和邮件服务器的交互命令信息输出到console中,用于JavaMail的调试
		session.setDebug(true);
		try {
			// 创建邮件对象
			MimeMessage message = new MimeMessage(session);
			// 设置邮件发送方
			message.setFrom(new InternetAddress(from));
			// 设置邮件发送的主题<邮件标题>
			message.setSubject("邮件发送设置");
			// 设置邮件发送的内容
			message.setContent(emailMsg,"text/html;charset=utf-8");
			Transport transport=session.getTransport();
			// 连接邮件服务器,“”中填写邮件服务器主机名
			properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			transport.connect("smtp.qq.com",465, username, password);
			transport.sendMessage(message,new Address[]{new InternetAddress(to)});
			transport.close();
			return true;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

22 岁糟老头子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值