너무어려웡
502
2017-04-09 21:43:49
7
666

이메일 전송 관련 질문ㅠㅠ


controller

----------------------------------------------------------------------------------------------------------------------------

@RequestMapping("/membership_lose_pwd.do")

public String lose_pwd(MembershipVo vo) {

MembershipVo pwd_vo=dao.select_lose_pwd(vo);

EmailVo lose_pwd = new EmailVo();

lose_pwd.setSubject("회원님의 비밀번호를 확인하세요");

lose_pwd.setReceiver(vo.getEmail());

lose_pwd.setContent("회원님의 비밀번호는 "+pwd_vo.getPwd()+"입니다.");

EmailSender.SendEmail(lose_pwd);

  

return "redirect:/membership_lose_pwd_form.do";

}

----------------------------------------------------------------------------------------------------------------------------

EmailSender.java

----------------------------------------------------------------------------------------------------------------------------

package util;

 


import org.springframework.mail.MailSender;

import org.springframework.mail.SimpleMailMessage;


import vo.EmailVo;



 

public class EmailSender

{

private static MailSender mailSender;

 

public void setMailSender(MailSender mailSender) {

this.mailSender = mailSender;

}

 

public static void SendEmail(EmailVo lose_pwd) {

 

SimpleMailMessage message = new SimpleMailMessage();

 

message.setFrom("abc12345@gmail.com")  ;

message.setTo(lose_pwd.getReceiver());

message.setSubject(lose_pwd.getSubject());

message.setText(lose_pwd.getContent());

mailSender.send(message);

}

}

----------------------------------------------------------------------------------------------------------------------------
servlet-context.xml

----------------------------------------------------------------------------------------------------------------------------

    <beans:bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >

        <beans:property name="host" value="smtp.gmail.com" />

        <beans:property name="port" value="25" />

        <beans:property name="defaultEncoding" value="utf-8"/>

        <beans:property name="username" value="abc12345@gmail.com"   />

        <beans:property name="password" value="12345atct" />

       

        <beans:property name="javaMailProperties">

            <beans:props>

                <beans:prop key="mail.smtp.starttls.enable">true</beans:prop>

                <beans:prop key="mail.smtp.auth">true</beans:prop>

                <beans:prop key="mail.smtps.debug">true</beans:prop>

            </beans:props>

        </beans:property>    

    </beans:bean>

    

    <beans:bean id="email" class="vo.EmailVo"></beans:bean>

    <beans:bean id="emailSender" class="util.EmailSender">

   <beans:property name="mailSender" ref="mailSender"></beans:property>

  </beans:bean>

----------------------------------------------------------------------------------------------------------------------------

EmailVo.java

----------------------------------------------------------------------------------------------------------------------------

package vo;


public class EmailVo {

String subject;

String content;

String receiver;

public EmailVo() {

// TODO Auto-generated constructor stub

}


public EmailVo(String subject, String content, String receiver) {

super();

this.subject = subject;

this.content = content;

this.receiver = receiver;

}

public String getSubject() {

return subject;

}

public void setSubject(String subject) {

this.subject = subject;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public String getReceiver() {

return receiver;

}

public void setReceiver(String receiver) {

this.receiver = receiver;

}

}

-------------------------------------------------------------------------------------------------------------------------------

jsp에서 id랑 email 입력 받으면 MembershipVo에서 pwd를 찾아서  EmailVo에 넣어서 보내는 식으로 하고 있는데 자꾸

HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

type Exception report

message Request processing failed; nested exception is java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

java.lang.NullPointerException
	util.EmailSender.SendEmail(EmailSender.java:29)
	com.ic.atct.MembershipController.lose_pwd(MembershipController.java:206)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.36 logs.


Apache Tomcat/8.0.36


mailSender.send(message); 에서 계속 오류가 나서 질문을 했었는데

mailsender에 주입이 안 되어있고 static 사용하면 안된다는 댓글이 있어서 static을 지워봤는데

컨트롤러 EmailSender.SendEmail(lose_pwd); 이 구문에서


 Cannot make a static reference to the non-static method SendEmail(EmailVo) from the type EmailSender


라는 말이 나오는데 ㅠ 어떻게 해결해야 할까요...ㅠㅠ

0
0
  • 답변 7

  • 아로마
    816
    2017-04-09 22:10:13 작성 2017-04-09 22:11:02 수정됨

    public static void SendEmail(EmailVo lose_pwd) {


    static 메소드 안에서는  static 변수만 사용 가능하니깐

    static 을 지우면 되겠죠.

    public void SendEmail(EmailVo lose_pwd) { } 

    0
  • 너무어려웡
    502
    2017-04-09 22:20:01

    그 부분의 static을 지우니깐 컨트롤러 쪽 EmailSender.SendEmail(lose_pwd); 구문에서 

    Cannot make a static reference to the non-static method SendEmail(EmailVo) from the type EmailSender

    라는 에러가 나옵니다ㅠ..

    0
  • 아로마
    816
    2017-04-09 22:30:54

    private MailSender mailSender; => static 제거

    public void SendEmail(EmailVo lose_pwd) { }  => static 제거


    다 제거 했는데도 오류가 나세요?





    0
  • 너무어려웡
    502
    2017-04-09 22:35:04 작성 2017-04-09 22:36:18 수정됨

    네 emailsend 부분

    ---------------------------------------------------------------------------------

    package util;

     

    import org.springframework.mail.MailSender;

    import org.springframework.mail.SimpleMailMessage;


    import vo.EmailVo;



     

    public class EmailSender

    {

    private  MailSender mailSender;

     

    public void setMailSender(MailSender mailSender) {

    this.mailSender = mailSender;

    }

     

    public  void SendEmail(EmailVo lose_pwd) {

     

    SimpleMailMessage message = new SimpleMailMessage();

     

    message.setFrom("abc12345@gmail.com");

    message.setTo(lose_pwd.getReceiver());

    message.setSubject(lose_pwd.getSubject());

    message.setText(lose_pwd.getContent());

    mailSender.send(message);

    }

    }

    ---------------------------------------------------------------------------------
    이렇게 수정했더니 컨트롤러에서 오류 나네요 ㅠㅠ
    0
  • 아로마
    816
    2017-04-09 22:49:45 작성 2017-04-09 22:51:31 수정됨
    0
  • 아로마
    816
    2017-04-09 22:59:16 작성 2017-04-09 22:59:53 수정됨


    컨트롤러의

    EmailSender.SendEmail(lose_pwd)

    이 코드 보니깐 (첫글자 대문자  - 클래스의미)

    의존성 주입으로 객체 생성이 아니라 그냥  static 이네요.


    xml 에서 의존성  주입을 했으면 어노테이션으로 

    @Autowired 또는  @Inject 으로 설정 을 하시던지.

    @Autowired 

    private EmailSender eamilSender ;

    아니면, 

     컨트롤를 xml에서 의존성 주입을 했다면

     생성자 또는 setter에서  EmailSender 를 주입 하셔야겠죠.

    0
  • 너무어려웡
    502
    2017-04-10 01:12:05

    다 해봤는데도 여전히 널포인트익셉션 나오네요 ㅠㅠ....

    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.