java发送邮件

下面是利用java代码发送文件方法:

controller层

package com.mail.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mail.service.MailService;



@Controller
@RequestMapping("/mail")
public class MailController {
	
	
	@Autowired
	private MailService ms;
	
	/**
	 * 发送邮件
	 * @param param
	 * 		  sMail:发送人邮箱
	 * 		  pass:发送人邮箱授权码
	 *        toMails:收件人的邮箱(list格式,单个就填一个)
	 *        title:邮件标题
	 *        content:邮件内容
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/sendEail")
	public Integer sendMail(@RequestBody JSONObject param ) {
		String sMail = param.getString("sMail");//发送邮箱
		String pass = param.getString("pass");//发送邮箱授权码
		List<String> toMails = (List<String>) param.get("toMails");//接收邮箱集合
		String title = param.getString("title");//邮件标题
		String content = param.getString("content");;//邮件内容
		return ms.sendMail(sMail, pass, toMails, title, content);
	}
}

service层

package com.mail.service;

import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;


@Service
public class MailService {
	
	
	/**   
	 * @param sMail:发送人邮箱
	 * @param pass:发送人邮箱授权码
	 * @param toMails:收件人的邮箱(list格式,单个就填一个)
	 * @param title:邮件标题
	 * @param content:邮件内容
	 * 		  mailType: 发件人邮箱服务器:smtp.*.com 例:163为smtp.163.com, qq为smtp.qq.com
	 * @return
	 */

	public Integer sendMail(String sMail, String pass, List<String> toMails, String title,
			String content) {
		//根据邮箱拼接邮箱服务器
		String mailboxType = sMail.substring(sMail.lastIndexOf("@")+1, sMail.lastIndexOf("."));
		String mailType = "smtp."+mailboxType+".com";
		//邮箱server
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		senderImpl.setHost(mailType);
		senderImpl.setUsername(sMail);
		senderImpl.setPassword(pass);
		senderImpl.setDefaultEncoding("UTF-8");
		Properties prop = new Properties();
		prop.put("mail.smtp.auth", "true");
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		senderImpl.setJavaMailProperties(prop);
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
		// 设置收件人,寄件人,发送邮件
		Integer re = 0;
		try {
			for (int i = 0; i < toMails.size(); i++) {
				messageHelper.setTo(toMails.get(i));
				messageHelper.setFrom(sMail);
				messageHelper.setSubject(title);
				messageHelper.setText(content, true);
				senderImpl.send(mailMessage);
				re = 1;
			}
			return re;
		} catch (MessagingException e) {
			e.printStackTrace();
			return 0;
		}
	}
}

依赖的jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值