- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 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
的调用判断,它可能在您的测试和应用程序运行时之间有所不同。在这种情况下,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:
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/
我尝试阅读有关 Spring BOM、Spring Boot 和 Spring IO 的文档。 但是没有说明,我们应该如何一起使用它们? 在我的项目中,我们已经有了自己的 Parent POM ,所以
我正在开发的很酷的企业应用程序正在转向 Spring。这对所有团队来说都是非常酷和令人兴奋的练习,但也是一个巨大的压力源。我们所做的是逐渐将遗留组件移至 Spring 上下文。现在我们有一个 huuu
我正在尝试使用 @Scheduled 运行 Spring 批处理作业注释如下: @Scheduled(cron = "* * * * * ?") public void launchMessageDi
我对这两个概念有点困惑。阅读 Spring 文档,我发现,例如。 bean 工厂是 Spring 容器。我还读到“ApplicationContext 是 BeanFactory 的完整超集”。但两者
我们有一个使用 Spring BlazeDS 集成的应用程序。到目前为止,我们一直在使用 Spring 和 Flex,它运行良好。我们现在还需要添加一些 Spring MVC Controller 。
假设我有一个类(class) Person带属性name和 age ,它可以像这样用 Spring 配置: 我想要一个自定义的 Spring 模式元素,这很容易做到,允许我在我的 Sp
如何在 Java 中以编程方式使用 Spring Data 创建 MongoDB 复合索引? 使用 MongoTemplate 我可以创建一个这样的索引:mongoTemplate.indexOps(
我想使用 spring-complex-task 执行我的应用程序,并且我已经构建了复杂的 spring-batch Flow Jobs,它执行得非常好。 你能解释一下spring批处理流作业与spr
我实现了 spring-boot 应用程序,现在我想将它用作非 spring 应用程序的库。 如何初始化 lib 类,以便 Autowiring 的依赖项按预期工作?显然,如果我使用“new”创建类实
我刚开始学习 spring cloud security,我有一个基本问题。它与 Spring Security 有何不同?我们是否需要在 spring boot 上构建我们的应用程序才能使用 spr
有很多人建议我使用 Spring Boot 而不是 Spring 来开发 REST Web 服务。我想知道这两者到底有什么区别? 最佳答案 总之 Spring Boot 减少了编写大量配置和样板代码的
您能向我解释一下如何使用 Spring 正确构建 Web 应用程序吗?我知道 Spring 框架的最新版本是 4.0.0.RELEASE,但是 Spring Security 的最新版本是 3.2.0
我如何才能知道作为 Spring Boot 应用程序的一部分加载的所有 bean 的名称?我想在 main 方法中有一些代码来打印服务器启动后加载的 bean 的详细信息。 最佳答案 如spring-
我有一个使用 Spring 3.1 构建的 RESTful API,也使用 Spring Security。我有一个 Web 应用程序,也是一个 Spring 3.1 MVC 应用程序。我计划让移动客
升级到 Spring 5 后,我在 Spring Rabbit 和 Spring AMQP 中遇到错误。 两者现在都设置为 1.5.6.RELEASE 有谁知道哪些版本应该与 Spring 5 兼容?
我现在已经使用 Spring Framework 3.0.5 和 Spring Security 3.0.5 多次了。我知道Spring框架使用DI和AOP。我还知道 Spring Security
我收到错误 Unable to Location NamespaceHandler when using context:annotation-config running (java -jar) 由
在 Spring 应用程序中嵌入唯一版本号的策略是什么? 我有一个使用 Spring Boot 和 Spring Web 的应用程序。 它已经足够成熟,我想对其进行版本控制并在运行时看到它显示在屏幕上
我正在使用 spring data jpa 进行持久化。如果存在多个具有相同名称的实体,是否有一种方法可以将一个实体标记为默认值。类似@Primary注解的东西用来解决多个bean的依赖问题 @Ent
我阅读了 Spring 框架的 DAOSupport 类。但是我无法理解这些 DAOSuport 类的优点。在 DAOSupport 类中,我们调用 getXXXTemplate() 方法来获取特定的
我是一名优秀的程序员,十分优秀!