在Spring MVC中发送Javamail

时间:2014-11-23 12:52:45

标签: java spring javamail

我正在尝试使用JavaMail在我的Spring MVC应用程序中发送电子邮件,但是当我尝试发送电子邮件时出现以下错误。地址。

错误:

>         DEBUG: setDebug: JavaMail version 1.4.3
>     DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun
> Microsystems, Inc]
>     DEBUG SMTP: useEhlo true, useAuth true
>     DEBUG SMTP: trying to connect to host "smtp.poczta.onet.pl", port 465, isSSL true
>     220 smtp.poczta.onet.pl ESMTP
>     DEBUG SMTP: connected to host "smtp.poczta.onet.pl", port: 465
>     
>     EHLO Piotr-Komputer
>     250-smtp.poczta.onet.pl
>     250-PIPELINING
>     250-SIZE 90000000
>     250-ETRN
>     250-AUTH PLAIN LOGIN
>     250-AUTH=PLAIN LOGIN
>     250-ENHANCEDSTATUSCODES
>     250 8BITMIME
>     DEBUG SMTP: Found extension "PIPELINING", arg ""
>     DEBUG SMTP: Found extension "SIZE", arg "90000000"
>     DEBUG SMTP: Found extension "ETRN", arg ""
>     DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
>     DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
>     DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
>     DEBUG SMTP: Found extension "8BITMIME", arg ""
>     DEBUG SMTP: Attempt to authenticate
>     DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
>     AUTH LOGIN
>     334 VXNlcm5hbWU6
>     aWJhbmtwc2tAb25ldC5ldQ==
>     334 UGFzc3dvcmQ6
>     MWFzZGZnaGo=
>     235 2.7.0 Authentication successful
>     DEBUG SMTP: use8bit false
>     MAIL FROM:<Piotr@Piotr-Komputer>
>     250 2.1.0 Ok
>     RCPT TO:<ibankpsk@onet.eu>
>     504 5.5.2 <Piotr@Piotr-Komputer>: Sender address rejected: need fully-qualified address
>     DEBUG SMTP: Invalid Addresses
>     DEBUG SMTP:   ibankpsk@onet.eu
>     DEBUG SMTP: Sending failed because of invalid destination addresses
>     RSET
>     250 2.0.0 Ok
>     javax.mail.SendFailedException: Invalid Addresses;
>       nested exception is:
>       com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <Piotr@Piotr-Komputer>: Sender address rejected: need fully-qualified
> address
>     
>       at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1607)
>       at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:887)
>       at pl.piotr.ibank.mail.Mail.send(Mail.java:58)
>       at pl.piotr.ibank.controller.MenuController.sendEmail(MenuController.java:85)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>       at java.lang.reflect.Method.invoke(Unknown Source)
>       at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
>       at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
>       at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
>       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
>       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
>       at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
>       at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
>       at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
>       at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
>       at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
>       at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
>       at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
>       at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
>       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
>       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
>       at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
>       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
>       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
>       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
>       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
>       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
>       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
>       at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
>       at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
>       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
>       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
>       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
>       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
>       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
>       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
>       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
>       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
>       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
>       at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
>       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
>       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
>       at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
>       at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
>       at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
>       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
>       at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
>       at java.lang.Thread.run(Unknown Source)
>     Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <Piotr@Piotr-Komputer>: Sender address rejected: need fully-qualified
> address
>     
>       at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1505)
>       ... 68 more
>     Error:Invalid Addresses

我的联系表格:

    <div id="contact">
    <h1>Wyślij wiadomość</h1>
    <form action="/sendEmail" method="post">
        <fieldset>
            <label for="name">Dane</label> <input type="text" id="name"
                name="name" placeholder="Imie i nazwisko" /> <label for="email">Email:</label>
            <input type="email" id="email" name="email"
                placeholder="Podaj swój email" /> <label for="message">Wiadomość:</label>
            <textarea id="message" name="message"
                placeholder="Wpisz tekst swojej wiadomości"></textarea>
            <input type="submit" value="Wyślij" />
        </fieldset>
    </form>
</div>

我的控制器:

    @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";
}

我的Mail类使用JavaMail。

    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();

    }

}

2 个答案:

答案 0 :(得分:0)

您永远不会在邮件中设置发件人地址,因此JavaMail会从您的计算机配置中获取它,但您的计算机没有正确配置为具有完全限定的域名,可能是因为它使用了DHCP获取其IP地址。

添加对message.setFrom的调用,它会更好用。

此外,您使用的是旧版本的JavaMail,因此您发布的调试输出包含您的密码,其形式是任何人都可以解码的。您可能希望立即更改密码。并升级到newer version of JavaMail

答案 1 :(得分:0)

你需要设置&#34;来自&#34;地址也是例如&#34; setFrom(新的InternetAddress(&#34; xyz@go.com));&#34; ,这就是它出错的原因。