title: Sending Javamail in Spring MVC
body: <p>I am trying to send email using JavaMail in my Spring MVC application but I get the following error when i try sending my email. Address. </p>
  
  <p>Errors:</p>
  
  <pre><code>&gt;         DEBUG: setDebug: JavaMail version 1.4.3
  &gt;     DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun
  &gt; Microsystems, Inc]
  &gt;     DEBUG SMTP: useEhlo true, useAuth true
  &gt;     DEBUG SMTP: trying to connect to host "smtp.poczta.onet.pl", port 465, isSSL true
  &gt;     220 smtp.poczta.onet.pl ESMTP
  &gt;     DEBUG SMTP: connected to host "smtp.poczta.onet.pl", port: 465
  &gt;     
  &gt;     EHLO Piotr-Komputer
  &gt;     250-smtp.poczta.onet.pl
  &gt;     250-PIPELINING
  &gt;     250-SIZE 90000000
  &gt;     250-ETRN
  &gt;     250-AUTH PLAIN LOGIN
  &gt;     250-AUTH=PLAIN LOGIN
  &gt;     250-ENHANCEDSTATUSCODES
  &gt;     250 8BITMIME
  &gt;     DEBUG SMTP: Found extension "PIPELINING", arg ""
  &gt;     DEBUG SMTP: Found extension "SIZE", arg "90000000"
  &gt;     DEBUG SMTP: Found extension "ETRN", arg ""
  &gt;     DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
  &gt;     DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
  &gt;     DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  &gt;     DEBUG SMTP: Found extension "8BITMIME", arg ""
  &gt;     DEBUG SMTP: Attempt to authenticate
  &gt;     DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
  &gt;     AUTH LOGIN
  &gt;     334 VXNlcm5hbWU6
  &gt;     aWJhbmtwc2tAb25ldC5ldQ==
  &gt;     334 UGFzc3dvcmQ6
  &gt;     MWFzZGZnaGo=
  &gt;     235 2.7.0 Authentication successful
  &gt;     DEBUG SMTP: use8bit false
  &gt;     MAIL FROM:&lt;Piotr@Piotr-Komputer&gt;
  &gt;     250 2.1.0 Ok
  &gt;     RCPT TO:&lt;ibankpsk@onet.eu&gt;
  &gt;     504 5.5.2 &lt;Piotr@Piotr-Komputer&gt;: Sender address rejected: need fully-qualified address
  &gt;     DEBUG SMTP: Invalid Addresses
  &gt;     DEBUG SMTP:   ibankpsk@onet.eu
  &gt;     DEBUG SMTP: Sending failed because of invalid destination addresses
  &gt;     RSET
  &gt;     250 2.0.0 Ok
  &gt;     javax.mail.SendFailedException: Invalid Addresses;
  &gt;       nested exception is:
  &gt;       com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 &lt;Piotr@Piotr-Komputer&gt;: Sender address rejected: need fully-qualified
  &gt; address
  &gt;     
  &gt;       at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1607)
  &gt;       at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:887)
  &gt;       at pl.piotr.ibank.mail.Mail.send(Mail.java:58)
  &gt;       at pl.piotr.ibank.controller.MenuController.sendEmail(MenuController.java:85)
  &gt;       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  &gt;       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  &gt;       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  &gt;       at java.lang.reflect.Method.invoke(Unknown Source)
  &gt;       at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
  &gt;       at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
  &gt;       at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
  &gt;       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
  &gt;       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
  &gt;       at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
  &gt;       at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
  &gt;       at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
  &gt;       at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
  &gt;       at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
  &gt;       at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
  &gt;       at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
  &gt;       at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  &gt;       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  &gt;       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  &gt;       at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
  &gt;       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  &gt;       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
  &gt;       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
  &gt;       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
  &gt;       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
  &gt;       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  &gt;       at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
  &gt;       at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
  &gt;       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
  &gt;       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
  &gt;       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  &gt;       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  &gt;       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  &gt;       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  &gt;       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  &gt;       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  &gt;       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  &gt;       at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  &gt;       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  &gt;       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
  &gt;       at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
  &gt;       at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
  &gt;       at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
  &gt;       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  &gt;       at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  &gt;       at java.lang.Thread.run(Unknown Source)
  &gt;     Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 &lt;Piotr@Piotr-Komputer&gt;: Sender address rejected: need fully-qualified
  &gt; address
  &gt;     
  &gt;       at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1505)
  &gt;       ... 68 more
  &gt;     Error:Invalid Addresses
  </code></pre>
  
  <p>My contact form:</p>
  
  <pre><code>    &lt;div id="contact"&gt;
      &lt;h1&gt;Wyślij wiadomość&lt;/h1&gt;
      &lt;form action="/sendEmail" method="post"&gt;
          &lt;fieldset&gt;
              &lt;label for="name"&gt;Dane&lt;/label&gt; &lt;input type="text" id="name"
                  name="name" placeholder="Imie i nazwisko" /&gt; &lt;label for="email"&gt;Email:&lt;/label&gt;
              &lt;input type="email" id="email" name="email"
                  placeholder="Podaj swój email" /&gt; &lt;label for="message"&gt;Wiadomość:&lt;/label&gt;
              &lt;textarea id="message" name="message"
                  placeholder="Wpisz tekst swojej wiadomości"&gt;&lt;/textarea&gt;
              &lt;input type="submit" value="Wyślij" /&gt;
          &lt;/fieldset&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>    @RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
  public String sendEmail(HttpServletRequest request) {
      String name = request.getParameter("name");
      String email = request.getParameter("email");
      String message = request.getParameter("message");
      Mail mail = new Mail(name, email, message);
      try {
          mail.send();
      } catch (MessagingException e) {
          System.out.println("Mamy błąd");
      }
      return "contact";
  }
  </code></pre>
  
  <p>And my Mail class using JavaMail.</p>
  
  <pre><code>    package pl.piotr.ibank.mail;
  
  import java.util.Properties;
  
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  public class Mail {
  
      private static final String HOST = "smtp.poczta.onet.pl";
      private static final int PORT = 465;
      // Adres email osby która wysyła maila
      private static final String FROM = "ibankpsk@onet.eu";
      // Hasło do konta osoby która wysyła maila
      private static final String PASSWORD = "1asdfghj";
      // Adres email osoby do której wysyłany jest mail
      private String to = "ibankpsk@onet.eu";
      // Temat wiadomości
      private String subject = "Kontakt";
      // Treść wiadomości
      private String content;
  
      public Mail(String AddressTo) {
          to = AddressTo;
      }
  
      public Mail(String name, String email, String text) {
          content = "Imie i nazwisko: " + name + "\nEmail: " + email
                  + "\n\nTreść: \n" + text;
      }
  
      public void send() throws MessagingException {
  
          Properties props = new Properties();
          props.put("mail.transport.protocol", "smtps");
          props.put("mail.smtps.auth", "true");
  
          // Inicjalizacja sesji
          Session mailSession = Session.getDefaultInstance(props);
  
          // ustawienie debagowania
          mailSession.setDebug(true);
  
          // Tworzenie wiadomości email
          MimeMessage message = new MimeMessage(mailSession);
          message.setSubject(subject);
          message.setContent(content, "text/plain; charset=ISO-8859-2");
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  
          Transport transport = mailSession.getTransport();
          transport.connect(HOST, PORT, FROM, PASSWORD);
  
          // wysłanie wiadomości
          transport.sendMessage(message,
                  message.getRecipients(Message.RecipientType.TO));
          transport.close();
  
      }
  </code></pre>
  
  <p>}</p>