程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java的Spring框架中完成發送郵件功效的焦點代碼示例

Java的Spring框架中完成發送郵件功效的焦點代碼示例

編輯:關於JAVA

Java的Spring框架中完成發送郵件功效的焦點代碼示例。本站提示廣大學習愛好者:(Java的Spring框架中完成發送郵件功效的焦點代碼示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java的Spring框架中完成發送郵件功效的焦點代碼示例正文


Spring中曾經封裝了郵件操作類,經由過程spring設置裝備擺設文件可以便捷地注入到controller、action等處所。
上面是設置裝備擺設:

<!-- 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設置裝備擺設

Email Configuration: 
mail.host=smtp.163.com 
mail.port=25 
[email protected] 
mail.passwd=xxxxxxxx 
[email protected] 

掌握器中注入mailSender:

@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:

<form:form action="sendEmail.do" method="post" commandName="newEmail"> 
  <div> 
    <p>   to:<form:input path="to" css/> 
      <form:errors path="to" css/></p> 
    <p>subject:<form:input path="subject" css/> 
      <form:errors path="subject" css/></p> 
    <p>content:<form:textarea path="content" rows="5" cols="60"></form:textarea><br/> 
      <form:errors path="content" css/></p> 
    <p><input type="submit" value="confirm and send"/></p> 
  </div> 
   
</form:form> 

發送帶附件的郵件:

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("[email protected]");
  mimeMessageHelper.setFrom("[email protected]");
  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();
 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved