服务器之家

服务器之家 > 正文

Java的Spring框架中实现发送邮件功能的核心代码示例

时间:2020-04-12 16:34     来源/作者:dyyaries

Spring中已经封装了邮件操作类,通过spring配置文件可以便捷地注入到controller、action等地方。
下面是配置:

?
1
2
3
4
<!-- mail sender -->
  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
    p:host="${mail.host}" p:port="${mail.port}" 
    p:username="${mail.user}" p:password="${mail.passwd}"></bean>

       
mail配置

?
1
2
3
4
5
6
Email Configuration:
mail.host=smtp.163.com
mail.port=25
mail.user=xxxxxx@163.com
mail.passwd=xxxxxxxx
mail.from=xxxxxx@163.com

控制器中注入mailSender:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Controller
public class EmailController {
  private MailSender mailSender;
  @Value("${mail.from}")
  String emailFrom;
   
  @Autowired
  public void setMailSender(MailSender mailSender) {
    this.mailSender = mailSender;
  }
   
  @RequestMapping(value="/sendEmail",method=RequestMethod.POST)
  public ModelAndView sendEmail(@ModelAttribute("newEmail") ContactEmail newEmail,
      BindingResult bindResult,SessionStatus status){
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(newEmail.getTo());
    message.setFrom(emailFrom);
    message.setSubject(newEmail.getSubject());
    message.setText(newEmail.getContent());
    String result = "";
    try{
      mailSender.send(message);
      result = "Email was sent!";
    }catch(MailException e){
      result = "Sending email failed!<br/><hr/>"+e.getMessage();
    }
    ModelAndView view = new ModelAndView("emailResult");
    view.addObject("result", result);
    return view;
  }
}

mail form:

?
1
2
3
4
5
6
7
8
9
10
11
12
<form:form action="sendEmail.do" method="post" commandName="newEmail">
  <div>
    <p>   to:<form:input path="to" cssStyle="width:260px;"/>
      <form:errors path="to" cssStyle="color:red;"/></p>
    <p>subject:<form:input path="subject" cssStyle="width:260px;"/>
      <form:errors path="subject" cssStyle="color:red;"/></p>
    <p>content:<form:textarea path="content" rows="5" cols="60"></form:textarea><br/>
      <form:errors path="content" cssStyle="color:red;"/></p>
    <p><input type="submit" value="confirm and send"/></p>
  </div>
   
</form:form>

发送带附件的邮件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
try{
  JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
  
  Properties props = new Properties();
  props.put("mail.smtp.auth", "true");
  senderImpl.setHost("smtp.163.com");
  senderImpl.setUsername("zhangfl85");
  senderImpl.setPassword("851010");
  senderImpl.setJavaMailProperties(props);
  MimeMessage mimeMessge = senderImpl.createMimeMessage();
 
  MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessge,true);
   
  mimeMessageHelper.setTo("zhangfl85@126.com");
  mimeMessageHelper.setFrom("zhangfl85@163.com");
  mimeMessageHelper.setSubject("添加附件测试");
  mimeMessageHelper.setText("test",true);  FileSystemResource img = new FileSystemResource(new File("I:/liang.jpg"));
   mimeMessageHelper.addAttachment(MimeUtility.encodeWord("3M样品仓库标准送货单模板.jpg"),
,img);  
   senderImpl.send(mimeMessge);
 }catch(Exception e){
  e.printStackTrace();
 }

 

标签:

相关文章

热门资讯

沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部