gpt4 book ai didi

java - 使用 Spring 框架清空邮件

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:30 24 4
gpt4 key购买 nike

我正在尝试使用 Spring 框架发送一封简单的电子邮件,但我遇到了一个非常奇怪的问题:在 Junit 测试用例中一切正常,但是当我从应用程序调用该函数时,邮件具有正确的收件人,但它是空的! (在 Gmail 中我可以看到一个“无名”附件)。

这里是我的配置:

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>

测试用例

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

}

在 Junit 测试用例中,我加载相同的 spring 上下文,webapp 的唯一区别是数据库配置(通过 Tomcat 中的 JNDI)...但这不应该是问题..

我考虑过 Maven 中的库冲突(我读到过与 geronimo-mail 的冲突)...但似乎工作正常有什么建议吗?

再见卡罗

编辑:

这是两封已发送电子邮件的两份日志。 (前者为空白,后者为正确)

空白

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>

最佳答案

我会将默认编码设置为 UTF-8,因为不清楚单元测试和应用程序之间的默认编码是什么:

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

我的猜测是,根据 MimeMessageHelper 的调用判断,它可能在您的测试和应用程序运行时之间有所不同。在这种情况下,MimeMessageSmartMimeMessage,如 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:

Special subclass of the standard JavaMail MimeMessage, carrying a default encoding to be used when populating the message and a default Java Activation FileTypeMap to be used for resolving attachment types.

Created by JavaMailSenderImpl in case of a specified default encoding and/or default FileTypeMap. Autodetected by MimeMessageHelper, which will use the carried encoding and FileTypeMap unless explicitly overridden.

关于java - 使用 Spring 框架清空邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35406719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com