Oct 8, 2014

email service by spring



First, need a dependency.

You can add dependency using mouse right click to pom.xml  then typing mail.

Or this way.

Add this code.

  1.     <!-- java mail -->
  2.     <dependency>
  3.         <groupId>javax.mail</groupId>
  4.         <artifactId>mail</artifactId>
  5.         <version>1.4</version>
  6.     </dependency>

Second, add package folder and java file Email.java, EmailSender.java.




Email.java is setter and getter, EmailSender.java is sender.

This is Email.java

  1. public class Email {
  2.     private String subject;
  3.     private String content;
  4.     private String receiver;
  5.      
  6.     public String getReceiver() {
  7.         return receiver;
  8.     }
  9.     public void setReceiver(String receiver) {
  10.         this.receiver = receiver;
  11.     }
  12.     public String getSubject() {
  13.         return subject;
  14.     }
  15.     public void setSubject(String subject) {
  16.         this.subject = subject;
  17.     }
  18.     public String getContent() {
  19.         return content;
  20.     }
  21.     public void setContent(String content) {
  22.         this.content = content;
  23.     }
  24. }

Email.java consist of subject, content and receiver.

This is EmailSender.java

  1. import javax.mail.MessagingException;
  2. import javax.mail.internet.InternetAddress;
  3. import javax.mail.internet.MimeMessage;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.mail.MailException;
  6. import org.springframework.mail.javamail.JavaMailSender;
  7.    
  8.     public class EmailSender  {
  9.          
  10.         @Autowired
  11.         protected JavaMailSender  mailSender;
  12.         public void SendEmail(Email email) throws Exception {
  13.              
  14.             MimeMessage msg = mailSender.createMimeMessage();
  15.             try {
  16.                 msg.setSubject(email.getSubject());
  17.                 msg.setText(email.getContent());
  18.                 msg.setRecipients(MimeMessage.RecipientType.TO , InternetAddress.parse(email.getReceiver()));
  19.                
  20.             }catch(MessagingException e) {
  21.                 System.out.println("MessagingException");
  22.                 e.printStackTrace();
  23.             }
  24.             try {
  25.                 mailSender.send(msg);
  26.             }catch(MailException e) {
  27.                 System.out.println("MailException발생");
  28.                 e.printStackTrace();
  29.             }
  30.         }
  31. }

EmailSender getting content, receiver and subject from Email.java then Sending using JavaMailSender.

This is Controller code.

  1.    @Autowired
  2.    private EmailSender emailSender;
  3.    @Autowired
  4.    private Email email;
  5.     @RequestMapping("/sendpw.do")
  6.     public ModelAndView sendEmailAction (@RequestParam Map<String, Object> paramMap, ModelMap model) throws Exception {
  7.         ModelAndView mav;
  8.         String id=(String) paramMap.get("id");
  9.         String e_mail=(String) paramMap.get("email");
  10.         String pw=mainService.getPw(paramMap);
  11.         System.out.println(pw);
  12.         if(pw!=null) {
  13.             email.setContent("비밀번호는 "+pw+" 입니다.");
  14.             email.setReceiver(e_mail);
  15.             email.setSubject(id+"님 비밀번호 찾기 메일입니다.");
  16.             emailSender.SendEmail(email);
  17.             mav= new ModelAndView("redirect:/login.do");
  18.             return mav;
  19.         }else {
  20.             mav=new ModelAndView("redirect:/logout.do");
  21.             return mav;
  22.         }
  23.     }

This code getting id and e-mail from jsp.

And check the information. 

If id and e-mail is right getting password from database.

This is bean setting.

  1. <!-- gmail -->
  2.     <bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
  3.         <property name="host" value="smtp.gmail.com" />
  4.         <property name="port" value="587" />
  5.         <property name="defaultEncoding" value="utf-8"/>
  6.         <property name="username" value="your id@gmail.com" />
  7.         <property name="password" value="your password" />
  8.        
  9.         <property name="javaMailProperties">
  10.             <props>
  11.                 <prop key="mail.smtp.starttls.enable">true</prop>
  12.                 <prop key="mail.smtp.auth">true</prop>
  13.                
  14.                
  15.             </props>
  16.         </property>
  17.        
  18.     </bean>
  19.     <bean id="email" class="spring.board.email.Email">
  20.     </bean>
  21.     <bean id="emailSender" class="spring.board.email.EmailSender">
  22.     </bean>

Line 2~18 : mailSender is bean for JavaMailSender.

The bean consist of protocol, port, username and password.

Detail setting for Gmail is here .

Line 19~22 is bean for  email and emailSender.

Running program

Redbox is link searching password.

If you click to link.


You can typing id and e-mail then click to button.



Go to my mailbox.

Arrived mail.



You can see your password.

If e-mail is wrong you can see this.









No comments:

Post a Comment