您的位置:首页 > 编程语言 > Java开发

通过javamailsenderimpl发送邮件

2017-12-11 15:38 489 查看
参考文章:http://blog.csdn.net/qq_33556185/article/details/51028952

import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.Properties;

@Controller
@RequestMapping("/api")

public class SendMailController {
@RequestMapping(value = "/sendmsg", method = RequestMethod.GET)
@ResponseBody
public String sendMessage(@RequestParam String mail){

JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定mail server
senderImpl.setHost("smtp.qq.com");
senderImpl.setUsername("*******@qq.com"); // 根据自己的情况,设置username
senderImpl.setPassword("********"); // 根据自己的情况, 设置password,注意qq邮箱的话此处设置16位授权码,不是邮箱密码,切记!
//加认证机制
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.auth", true);
javaMailProperties.put("mail.smtp.starttls.enable", true);
javaMailProperties.put("mail.smtp.timeout", 5000);
senderImpl.setJavaMailProperties(javaMailProperties);

// 建立邮件消息
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 设置收件人,寄件人 用数组发送多个邮件
// String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
// mailMessage.setTo(array);
mailMessage.setTo("**********@qq.com");
mailMessage.setFrom("*********@qq.com ");
mailMessage.setSubject([b]" 测试简单文本邮件发送! ");
mailMessage.setText(" 测试我的简单邮件发送机制!! ");

// 发送邮件
senderImpl.send(mailMessage);

System.out.println(" 邮件发送成功.. ");
return "haHa";
}

//有附件的邮件发送
@RequestMapping(value = "/sendPht", method = RequestMethod.GET)
@ResponseBody
public String sendPht(@RequestParam String filePath)throws Exception{
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.qq.com");
javaMailSender.setUsername("**********@qq.com");
javaMailSender.setPassword("**********");

Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.auth", true);
javaMailProperties.put("mail.smtp.starttls.enable", true);
javaMailProperties.put("mail.smtp.timeout", 5000);
javaMailSender.setJavaMailProperties(javaMailProperties);

//使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容
MimeMessage msg = javaMailSender.createMimeMessage();
//创建MimeMessageHelper对象,处理MimeMessage的辅助类
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
//使用辅助类MimeMessage设定参数
helper.setFrom(javaMailSender.getUsername());
helper.setTo("************@qq.com");
helper.setSubject("Hello Attachment");
helper.setText("This is a mail with attachment");
//加载文件资源,作为附件
File file = new File(filePath);
if(!file.exists()){
return "文件不存在";
}
//加入附件
helper.addAttachment(file.getName(), file);
// 发送邮件
javaMailSender.send(msg);
return "HaHaHaHaHaHa";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐