Empty mail with Spring framework

I am trying to send a simple email using the Spring structure, but I have a very strange problem: everything works fine in the Junit test case, but when I call the function from the application, the mail has the correct recipients, but it is empty! (in Gmail, I see the attachment "without a name").

Here is my configuration:

MailerServiceImpl

@Override
public void sendMail(String recipient, String content,
        String attachmentFileName, byte[] attachment) throws Exception {

    MimeMessage message = mailSender.createMimeMessage();


    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(simpleMailMessage.getFrom());
    helper.setTo(recipient);
    helper.setSubject(simpleMailMessage.getSubject());
    helper.setText(String.format(simpleMailMessage.getText(), content));

    if (attachment != null && attachmentFileName != null) {
        helper.addAttachment(attachmentFileName, new ByteArrayResource(
                attachment));
    }
    logger.log(Level.INFO, "MimeMessageHelper Created");
    try {
        mailSender.send(message);
        logger.log(Level.INFO, "Email sent to: "
                + message.getAllRecipients().toString());
    } catch (MailException e) {
        logger.log(Level.SEVERE, "Error while sending mail", e);
        throw new EipException("Errore email", e);
    }

}
private JavaMailSender mailSender;

public void setMailSender(JavaMailSender mailSender) {
    this.mailSender = mailSender;
}

private SimpleMailMessage simpleMailMessage;

public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
    this.simpleMailMessage = simpleMailMessage;
}

applicationContext.xml

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="192.168.209.208" />
    <property name="port" value="25" />
    <property name="username" value="xxx" />
    <property name="password" value="xxx" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">false</prop>
            <prop key="mail.smtp.starttls.enable">false</prop>
        </props>
    </property>
</bean>
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="info@test.com" />
    <property name="to" value="" />
    <property name="subject" value="Info" />
    <property name="text">
        <value><![CDATA[%s]]></value>
    </property>
</bean>

Test case

public class TaskServiceTest extends AbstractApplicationContextTest {

@Autowired 
MailerService mailerService;

@Test
public void testMail(){

    String testoEmail = "test mail";


    String[] indirizzi = {"xxx@example.com"};

    try {
        mailerService.sendMail(indirizzi, testoEmail, null, null);
    } catch (Exception e) {
        fail();
    }

}

}

In a Junit test case, I load the same Spring context, the only difference in webapp is the database configuration (via JNDI in Tomcat) ... but this should not be a problem.

I thought about the library conflict in Maven (I read about the conflict with geronimo-mail) ... but it seems to work fine Any suggestion?

Bye carlo

Edit:

. ( - , )

DEBUG SMTP: trying to connect to host "192.168.209.208", port 25,     isSSL false
220 hostname ESMTP Postfix (Debian/GNU)
DEBUG SMTP: connected to host "192.168.209.208", port: 25

EHLO MASLNN112
250-hostname
250-PIPELINING 
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10240000"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<xxx@example.com>
250 2.1.0 Ok
RCPT TO:<xxx@example.com>
250 2.1.5 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   xxx@example.com
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Tue, 16 Feb 2016 11:49:21 +0100 (CET)
From: xxx@example.com
To: xxx@example.com
Message-ID: <839107267.2.1455619761727.JavaMail.carlo@MASLNN112>
Subject: Integrazione Enel - Comunicazione
MIME-Version: 1.0
Content-Type: multipart/mixed; 
boundary="----=_Part_0_1452882369.1455619761619"

.
250 2.0.0 Ok: queued as AB2047A5251
QUIT
221 2.0.0 Bye

DEBUG SMTP: trying to connect to host "192.168.209.208", port 25, isSSL false
220 hostname ESMTP Postfix (Debian/GNU)
DEBUG SMTP: connected to host "192.168.209.208", port: 25

EHLO MASLNN112
250-hostname
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10240000"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<xxx@example.com>
250 2.1.0 Ok
RCPT TO:<xxx@example.com>
250 2.1.5 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   xxx@example.com
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Tue, 16 Feb 2016 11:45:51 +0100 (CET)
From: xxx@example.com
To: xxx@example.com
Message-ID: <563330706.2.1455619551945.JavaMail.carlo@MASLNN112>
Subject: Integrazione Enel - Comunicazione
MIME-Version: 1.0
Content-Type: multipart/mixed; 
boundary="----=_Part_0_967231573.1455619550687"

------=_Part_0_967231573.1455619550687
Content-Type: multipart/related; 
boundary="----=_Part_1_309604348.1455619550701"

------=_Part_1_309604348.1455619550701
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

test mail.
------=_Part_1_309604348.1455619550701--

------=_Part_0_967231573.1455619550687--
.
250 2.0.0 Ok: queued as DFC317A5251
QUIT
221 2.0.0 Bye

2:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.arpav</groupId>
<artifactId>eip2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Enel Integrator Process 2</name>

<properties>
    <java.version>1.7</java.version>
    <drools.version>6.2.0.Final</drools.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hibernate.version>4.2.6.Final</hibernate.version>
    <hibernate.core.version>4.2.6.Final</hibernate.core.version>
    <hibernate.validator.version>4.2.0.Final</hibernate.validator.version>
    <slf4j.version>1.6.4</slf4j.version>
    <jboss.javaee.version>1.0.0.Final</jboss.javaee.version>
    <logback.version>1.0.9</logback.version>
    <h2.version>1.3.161</h2.version>
    <btm.version>2.1.4</btm.version>
    <junit.version>4.10</junit.version>
    <spring.version>3.2.12.RELEASE</spring.version>
    <sitemesh.version>3.0.0</sitemesh.version>
    <log4j.version>1.2.17</log4j.version>
    <jackson.version>1.9.9</jackson.version>
</properties>


<dependencies>


    <!-- test dependencies -->

    <!-- <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency> -->

    <!-- Junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>javassist</artifactId>
                <groupId>org.javassist</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jboss-logging</artifactId>
                <groupId>org.jboss.logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.core.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <artifactId>javassist</artifactId>
                <groupId>org.javassist</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jboss-logging</artifactId>
                <groupId>org.jboss.logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>${h2.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- <dependency>
        <groupId>org.codehaus.btm</groupId>
        <artifactId>btm</artifactId>
        <version>${btm.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency> -->

    <!-- Spring -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency> -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Servlet & Jsp -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <!-- Servlet Spec -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- Sitemesh -->
    <dependency>
        <groupId>org.sitemesh</groupId>
        <artifactId>sitemesh</artifactId>
        <version>${sitemesh.version}</version>
    </dependency>

    <!-- Log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>

    <!-- Hibernate -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.openejb</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0-4</version>
        <scope>provided</scope>
    </dependency>

    <!-- Richieste http -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.2</version>
    </dependency>

    <!-- SDEE -->
    <dependency>
        <groupId>it.enel</groupId>
        <artifactId>SDEE</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

    <!-- Invio emails -->

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

</dependencies>
<repositories>
    <repository>
        <id>springsource-milestones</id>
        <name>SpringSource Milestones Proxy</name>
        <url>https://oss.sonatype.org/content/repositories/springsource-milestones</url>
    </repository>

    <!-- <repository> <id>jboss-public-repository-group</id> <name>JBoss Public 
        Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public/</url> 
        <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> 
        <snapshots> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </snapshots> 
        </repository> -->
</repositories>

<build>
    <finalName>eip2</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

+4
2

. pom.xml provided javax.mail, :

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
        <scope>provided</scope> <------------ here
    </dependency>

, , maven documentation :

: , , , JDK . , - Java Enterprise Edition API- Servlet API- Java EE , - . .

, classpath .

@bphilipnyc @Rai .

0

UTF-8, , unit test :

MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");

, , , MimeMessageHelper. MimeMessage SmartMimeMessage, JavaMailSenderImpl MimeMessage :

/**
 * This implementation creates a SmartMimeMessage, holding the specified
 * default encoding and default FileTypeMap. This special defaults-carrying
 * message will be autodetected by {@link MimeMessageHelper}, which will use
 * the carried encoding and FileTypeMap unless explicitly overridden.
 * @see #setDefaultEncoding
 * @see #setDefaultFileTypeMap
 */
public MimeMessage createMimeMessage() {
    return new SmartMimeMessage(getSession(), getDefaultEncoding(), getDefaultFileTypeMap());
}

SmartMimeMessage:

protected String getDefaultEncoding(MimeMessage mimeMessage) {
    if (mimeMessage instanceof SmartMimeMessage) {
        return ((SmartMimeMessage) mimeMessage).getDefaultEncoding();
    }
    return null;
}

SmartMimeMessage JavaDoc:

JavaMail MimeMessage, , Java Activation FileTypeMap, .

JavaMailSenderImpl / default FileTypeMap. MimeMessageHelper, FileTypeMap, .

+1

Source: https://habr.com/ru/post/1628815/


All Articles