简体   繁体   中英

Send mail not working in Spring/Maven/Java 6 environment

I am using java 6.0, Spring 3.0 & Maven. I am facing a strange problem.

I am trying to send mail from my application, but not able to do so. I checked with debug on, the logs seems fine - no exception/error but the mail doesn't trigger.

Relevant code:

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class JavaEmail 
{
    private final String SMTP_AUTH_USER = "admin@myorg.com";
    private final String SMTP_AUTH_PWD  = "secret";

    public void sendMain(String strFrom, String strTo, String strSubject, String    
strContent) throws MessagingException 
    {
        Message message = new MimeMessage(getSession());

        message.addRecipient(RecipientType.TO, new InternetAddress(strTo));
        message.addFrom(new InternetAddress[] { new InternetAddress(strFrom)});

        message.setSubject(strSubject);
        message.setContent(strContent, "text/plain");

        try {
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Session getSession() {
        Authenticator authenticator = new Authenticator();

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.submitter", 
authenticator.getPasswordAuthentication().getUserName());
        properties.setProperty("mail.smtp.auth", "true");

        properties.setProperty("mail.smtp.host", "smtp.myorg.com");
        properties.setProperty("mail.smtp.port", "25");
        properties.setProperty("mail.debug", "true");

        return Session.getInstance(properties, authenticator);
    }

    private class Authenticator extends javax.mail.Authenticator 
    {
        private PasswordAuthentication authentication;

        public Authenticator() 
        {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            authentication = new PasswordAuthentication(username, password);
        }

        protected PasswordAuthentication getPasswordAuthentication() 
        {
            return authentication;
        }
    }


    public static void main (String [] args) throws MessagingException {

        JavaEmail email = new JavaEmail();

        email.sendMain("myid@myorg.com", "myid@myorg.com", "Say Hi ..", "Body");

    }

}

POM (relevant):

<dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.1</version>
        </dependency>

Logs:

DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.myorg.com", port 25, isSSL false
220 droutbound.logix.in ESMTP
DEBUG SMTP: connected to host "smtp.myorg.com", port: 25

EHLO ABCDE1234
250-droutbound.logix.in
250-8BITMIME
250-SIZE 26214400
250-STARTTLS
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "26214400"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNl1cm25hbW2U6
c2NjYWRta1W5Ae3mVuc42Fy5Lmlu
334 UGFz7c63dv2cmQ6
WmVuc22Fy5MT7IzIw==
235 #2.0.0 OK Authenticated
DEBUG SMTP: use8bit false
MAIL FROM:&lt;address@myorg.com&gt; AUTH=address@myorg.com
250 sender &lt;address@myorg.com&gt; ok
RCPT TO:&lt;address@myorg.com&gt;
250 recipient &lt;address@myorg.com&gt; ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   address@myorg.com
DATA
354 go ahead

Body
.
250 ok:  Message 325177010 accepted
QUIT
221 droutbound.logix.in

I have tried with other sample programs as well - with or without spring configurations. No error. BUT no mail as well.

IMP - same program(s) work fine if I use them in my other project which doesn't use maven - but otherwise have same configurations. In those logs there are some more lines in between ..

**DATA
354 go ahead
From: address@myorg.com
To: address@myorg.com
Message-ID: &lt;12694833.01327427956033.JavaMail.myid@ABCDE12345&gt;
Subject: Say Hi ..
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Body
.**

I am stuck. Is this due to Maven? Please suggest.

Actually, it was more than required jars that were causing issue.

On extracting my war - I noticed there are below jars:

geronimo-activation_1.1_spec-1.0.1.jar, geronimo-javamail_1.4_spec-1.2.jar, geronimo-jta_1.1_spec-1.1.jar, geronimo-stax-api_1.0_spec-1.0.1.jar etc.

There is no entry for them in my pom - these are being included in my lib due to some other dependency. So, below did work ..

<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/web.xml,WEB-INF/lib/geronimo-activation_1.1_spec-   
        1.0.1.jar, WEB-INF/lib/geronimo-javamail_1.4_spec-1.2.jar
</packagingExcludes>
<warName>SCTM_Retailer</warName>
</configuration>
</plugin>

Thanks Ralph & Raghuram.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM