如何在spring mvc中向特定的电子邮件ID发送电子邮件

时间:2016-07-05 06:56:18

标签: java spring spring-mvc

1 个答案:

答案 0 :(得分:0)

 public static void main(String[] args) {  

 String to="Enter email id to whom you want to send your email";//change accordingly  

  //Get the session object  
  Properties props = new Properties();  
  props.put("mail.smtp.host", "smtp.gmail.com");  
  props.put("mail.smtp.socketFactory.port", "465");  
  props.put("mail.smtp.socketFactory.class",  
            "javax.net.ssl.SSLSocketFactory");  
  props.put("mail.smtp.auth", "true");  
  props.put("mail.smtp.port", "465");  

  Session session = Session.getDefaultInstance(props,  
   new javax.mail.Authenticator() {  
   protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication("Your login id","Your login password");//change accordingly  
   }  
  });  

  //compose message  
  try {  
   MimeMessage message = new MimeMessage(session);  
   message.setFrom(new InternetAddress("token.verify@gmail.com"));//change accordingly  
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
   message.setSubject("Hello");  
   message.setText("Testing......."); 


   //send message  
   Transport.send(message);  

   System.out.println("message sent successfully");  

  } catch (MessagingException e) {throw new RuntimeException(e);}  

 } 

以上代码可以帮助您向特定的电子邮件ID发送电子邮件..