邮件不使用javaMail发送

时间:2013-05-03 06:13:21

标签: spring smtp javamail

在我的应用程序中,我使用spring发送邮件。当我从任何gmail id发送邮件时它工作正常,但是当我从另一个smtp服务器发送邮件时,邮件没有发送。 我的gmail弹簧配置:

<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="diganta.XXXXX@gmail.com" />
    <property name="password" value="******" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.auth">true</prop>
        </props>
    </property>
</bean>

<bean id="requestToAcceptOrDeny" class="com.edfx.adb.mail.RequestToAcceptOrDeny">
    <property name="mailSender" ref="mailSender" />
</bean>
</beans>

但是当我将配置更改为:

<property name="host" value="mail.mycompany.co.in" />
<property name="port" value="25" />
<property name="username" value="XXX@mycompany.co.in" />    
<property name="password" value="abc123" />

然后没有异常生成,但邮件也没有发送,密码也没有检查,即给定密码是否正确。我无法理解我做错了什么。

任何人都可以帮助我吗?感谢

更新-1

16:29:23,524 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select user0_.ID as ID7_0_, user0_.CREATE_TIMESTAMP as CREATE2_7_0_, user0_.LAST_UPDATE_TIMESTAMP as LAST3_7_0_, user0_.VERSION as VERSION7_0_, user0_.ACTIVE as ACTIVE7_0_, user0_.DATE_OF_BIRTH as DATE6_7_0_, user0_.DATE_OF_JOINING as DATE7_7_0_, user0_.DATE_OF_RELEASE as DATE8_7_0_, user0_.FIRST_NAME as FIRST9_7_0_, user0_.FULL_NAME as FULL10_7_0_, user0_.HOME_PHONE as HOME11_7_0_, user0_.HOME_POSTAL_ADDRESS as HOME12_7_0_, user0_.LAST_NAME as LAST13_7_0_, user0_.MAIL as MAIL7_0_, user0_.MOBILE as MOBILE7_0_, user0_.ROLE as ROLE7_0_, user0_.USERNAME as USERNAME7_0_ from USER user0_ where user0_.ID=?

16:29:23,534 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG: JavaMail version 1.4.4

16:29:23,534 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG: successfully loaded resource: /META-INF/javamail.default.providers

16:29:23,544 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG: Tables of loaded providers

16:29:23,544 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}

16:29:23,544 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}

16:29:23,544 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map

16:29:23,594 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Sending email ....

16:29:23,594 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]

16:29:23,604 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: useEhlo true, useAuth true

16:29:23,604 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: trying to connect to host "mail.mycompany.co.in", port 25, isSSL false

16:29:24,634 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 220 mail.mycompany.co.in ESMTP Postfix

16:29:24,644 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: connected to host "mail.mycompany.co.in", port: 25
16:29:24,644 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 

16:29:24,644 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) EHLO EDFX-DESKTOP-28

16:29:24,865 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250-mail.mycompany.co.in

16:29:24,865 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250-PIPELINING

16:29:24,865 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250-SIZE 10240000

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250-VRFY

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250-ETRN

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250-ENHANCEDSTATUSCODES

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250-8BITMIME

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250 DSN

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Found extension "PIPELINING", arg ""

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Found extension "SIZE", arg "10240000"

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Found extension "VRFY", arg ""

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Found extension "ETRN", arg ""

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Found extension "8BITMIME", arg ""

16:29:24,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Found extension "DSN", arg ""

16:29:24,895 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: use8bit false

16:29:24,895 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) MAIL FROM:<Temp@EDFX-DESKTOP-28>

16:29:25,135 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250 2.1.0 Ok

16:29:25,135 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) RCPT TO:<diganta.xxx@gmail.com>

16:29:25,365 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250 2.1.5 Ok

16:29:25,365 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP: Verified Addresses

16:29:25,365 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DEBUG SMTP:   diganta.xxxx@gmail.com

16:29:25,365 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) DATA

16:29:25,615 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 354 End data with <CR><LF>.<CR><LF>

16:29:25,615 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Date: Fri, 3 May 2013 16:29:24 +0530 (IST)

16:29:25,615 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) To: diganta.xxxx@gmail.com

16:29:25,615 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Message-ID: <628313650.2.1367578764895.JavaMail.Temp@EDFX-DESKTOP-28>

16:29:25,615 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Subject: Request to Remove Rounak Dey's Activity's Manager privilege from

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1)  activity EFXL13001

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) MIME-Version: 1.0

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Content-Type: multipart/mixed; 

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1)   boundary="----=_Part_0_263230409.1367578763564"

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) ------=_Part_0_263230409.1367578763564

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Content-Type: multipart/related; 

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1)   boundary="----=_Part_1_1514490209.1367578763584"

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) ------=_Part_1_1514490209.1367578763584

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Content-Type: text/html; charset=us-ascii

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Content-Transfer-Encoding: 7bit

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) <html><body><a href=http://localhost:8080/ActivityDatabase/req/AcceptOrDenyPermision.xhtml?activityId=EFXL13001&uniqueId=7fe18301-0bde-48da-a7d1-2bbe43b95e98&emailId=diganta.xxxx@gmail.com>Accept Or Deny request..</a></body></html>

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) ------=_Part_1_1514490209.1367578763584--

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) ------=_Part_0_263230409.1367578763564--

16:29:25,625 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) .

16:29:25,855 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 250 2.0.0 Ok: queued as 164B322AB7

16:29:25,855 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) QUIT

16:29:26,046 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) 221 2.0.0 Bye

3 个答案:

答案 0 :(得分:0)

您的服务器不支持STARTTLS,除非您通过SSL连接,否则不会让您进行身份验证。如果不进行身份验证,则不会在域外传递邮件。假设您使用的是新版本的JavaMail,请将属性“mail.smtp.ssl”设置为“true”。

答案 1 :(得分:0)

    package com.karthik.spring;    

    import java.io.IOException;
    import java.io.InputStream;

    import javax.mail.internet.MimeMessage;
    import javax.servlet.http.HttpServletRequest;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.io.InputStreamSource;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.mail.javamail.MimeMessagePreparator;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.commons.CommonsMultipartFile;

    @Controller
    @RequestMapping("/sendEmail.do")
    public class SendEmailAttachController {
        @Autowired
        private JavaMailSender mailSender;

        @RequestMapping(method = RequestMethod.POST)
        public String sendEmail(HttpServletRequest request,
                final @RequestParam CommonsMultipartFile attachFile) {

            // Input here
            final String emailTo = request.getParameter("mailTo");
            final String subject = request.getParameter("subject");
            final String yourmailid = request.getParameter("yourmail");
            final String message = request.getParameter("message");

            // Logging
            System.out.println("emailTo: " + emailTo);
            System.out.println("subject: " + subject);
            System.out.println("Your mail id is: "+yourmailid);
            System.out.println("message: " + message);
            System.out.println("attachFile: " + attachFile.getOriginalFilename());

            mailSender.send(new MimeMessagePreparator() {

                @Override
                public void prepare(MimeMessage mimeMessage) throws Exception {
                    MimeMessageHelper messageHelper = new MimeMessageHelper(
                            mimeMessage, true, "UTF-8");
                    messageHelper.setTo(emailTo);
                    messageHelper.setSubject(subject);
                    messageHelper.setReplyTo(yourmailid);
                    messageHelper.setText(message);

                    // Attachment with mail
                    String attachName = attachFile.getOriginalFilename();
                    if (!attachFile.equals("")) {

                        messageHelper.addAttachment(attachName, new InputStreamSource() {

                            @Override
                            public InputStream getInputStream() throws IOException {
                                return attachFile.getInputStream();
                            }
                        });
                    }

                }

            });

            return "Result";
        }
    }

**spring-mvc.xml**

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.karthik.spring" />

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="587" />
        <property name="username" value="xxxx@gmail.com" />
        <property name="password" value="xxxx" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- max upload size in bytes -->
        <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

        <!-- max size of file in memory (in bytes) -->
        <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Exception">Error</prop>
            </props>
        </property>
    </bean> 
</beans>

答案 2 :(得分:0)

我也遇到了与OP相同的情况。我使用Spring邮件发送邮件和测试与gmail帐户工作完美,然后当我申请我的公司邮件时,它没有抛出任何异常,但没有收到邮件。 我花了几个小时的谷歌搜索,看到调试日志和尝试许多配置,但没有工作。 然后我用javax.mail编写一个java程序来测试,一切正常。我意识到区别在于我设置的java程序来自邮件帐户(与spring config中的username属性相同)。 例如。

  

消息消息=新的MimeMessage(会话);

     

message.setFrom(new InternetAddress(“XXX@mycompany.co.in”));

解决了这个问题。