Spring 發送郵件

 public void sendEmail(String email, String message,String subject) throws SendFailedException {
  try {
   Properties prop = new Properties();
   prop.setProperty("mail.smtp.auth", "true");
   JavaMailSenderImpl mailSend = new JavaMailSenderImpl();
   mailSend.setHost("smtp.163.com");
   mailSend.setUsername("zhangsan");
   mailSend.setPassword("abc");//有效郵箱的密碼   mailSend.setJavaMailProperties(prop);

   SimpleMailMessage mailMessage = new SimpleMailMessage();
   mailMessage .setFrom("[email protected]");//一個有效的郵箱,郵件將從這個郵箱中發送出去
                                                                        ,注意與設置的Host(163郵箱)一致
   mailMessage .setTo(email);    //發給誰

   mailMessage .setSubject(subject);//設置郵件主題
   mailMessage .setText(message);//設置郵件內容
   mailSend.setDefaultEncoding("UTF-8");
   mailSend.send(message);//發送
  } catch (Exception ex) {
   logger.info(ex.toString());
   throw new SendFailedException();
  }
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章