Java 邮件 邮件正文不正确
Java Mail Incorrect message body
我正在使用 Java 邮件 api 发送邮件。但它似乎在不同的模块中工作不同。当我从模块 A 调用方法 sendmail 时,我收到了预期的邮件。但是当我从模块 B 调用相同的方法时,我收到格式不正确的消息。这是我的代码
public void sendmail(String toAddress, String subject, String messageBody, String fileName, String attachmentContent) throws MDRException {
try{
log.debug(new LogEntry(EventType.PROCESS, EventStatus.IN_PROGRESS, "Sending E-MAIL notification to :"+toAddress +" subject :"+ subject+" message :"+ messageBody));
MimeMessage message = getMimeMessage(toAddress, subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageBody,"text/html; charset=utf-8" );
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = null;
try {
source = new ByteArrayDataSource(attachmentContent, "text/plain");
} catch (IOException e) {
log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE,"Error occurred while creating email attachment"),e);
}
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.addHeader("Content-Transfer-Encoding", "8bit");
Transport.send(message);
log.debug(new LogEntry(EventType.PROCESS, EventStatus.IN_PROGRESS, "E-Mail sent successfully to :"+toAddress +" subject :"+ subject+" message :"+ messageBody));
}catch( MessagingException e){
log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE, "Error while sending mail"),e);
throw new MDRException(MDRError.SENDING_NOTIFICATION_ERROR, e);
}
}
public MimeMessage getMimeMessage(String toAddress,String subject) throws MDRException{
String to = toAddress;
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", smtpHost);
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try{
message.addHeader("format", "flowed");
message.addHeader("Content-Transfer-Encoding", "8bit");
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
}catch( MessagingException e){
log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE, "Error while creating mime message"),e);
throw new MDRException(MDRError.SENDING_NOTIFICATION_ERROR, e);
}
return message;
}
我从模块 A 和 B 调用 sendmail 方法的代码。
@Autowired
SendEmailNotification sendEmailNotification;
.
.
.
.
@Test
public void testEmail() throws Exception {
String msgBody = "<html><body><h1> CSV attachment message</h1></body></html>";
String attachmentText = "Source Id, Metadata Text";//messageFormat1(docDetails);
sendEmailNotification.sendmail("test@test.com", "documents failed to post", msgBody, "failedMEssage.txt",attachmentText);
}
收到模块A的邮件
收到来自模块 B 的邮件
问题已解决。 Maven 依赖性存在问题。在我的模块 B 中,使用了 org.apache.axis2 依赖项,它具有 javax.mail 的另一个子依赖项和 diff。版本。我排除了这种依赖。如下;解决了这个问题。
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5.1</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>
我正在使用 Java 邮件 api 发送邮件。但它似乎在不同的模块中工作不同。当我从模块 A 调用方法 sendmail 时,我收到了预期的邮件。但是当我从模块 B 调用相同的方法时,我收到格式不正确的消息。这是我的代码
public void sendmail(String toAddress, String subject, String messageBody, String fileName, String attachmentContent) throws MDRException {
try{
log.debug(new LogEntry(EventType.PROCESS, EventStatus.IN_PROGRESS, "Sending E-MAIL notification to :"+toAddress +" subject :"+ subject+" message :"+ messageBody));
MimeMessage message = getMimeMessage(toAddress, subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageBody,"text/html; charset=utf-8" );
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = null;
try {
source = new ByteArrayDataSource(attachmentContent, "text/plain");
} catch (IOException e) {
log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE,"Error occurred while creating email attachment"),e);
}
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.addHeader("Content-Transfer-Encoding", "8bit");
Transport.send(message);
log.debug(new LogEntry(EventType.PROCESS, EventStatus.IN_PROGRESS, "E-Mail sent successfully to :"+toAddress +" subject :"+ subject+" message :"+ messageBody));
}catch( MessagingException e){
log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE, "Error while sending mail"),e);
throw new MDRException(MDRError.SENDING_NOTIFICATION_ERROR, e);
}
}
public MimeMessage getMimeMessage(String toAddress,String subject) throws MDRException{
String to = toAddress;
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", smtpHost);
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try{
message.addHeader("format", "flowed");
message.addHeader("Content-Transfer-Encoding", "8bit");
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
}catch( MessagingException e){
log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE, "Error while creating mime message"),e);
throw new MDRException(MDRError.SENDING_NOTIFICATION_ERROR, e);
}
return message;
}
我从模块 A 和 B 调用 sendmail 方法的代码。
@Autowired
SendEmailNotification sendEmailNotification;
.
.
.
.
@Test
public void testEmail() throws Exception {
String msgBody = "<html><body><h1> CSV attachment message</h1></body></html>";
String attachmentText = "Source Id, Metadata Text";//messageFormat1(docDetails);
sendEmailNotification.sendmail("test@test.com", "documents failed to post", msgBody, "failedMEssage.txt",attachmentText);
}
收到模块A的邮件
收到来自模块 B 的邮件
问题已解决。 Maven 依赖性存在问题。在我的模块 B 中,使用了 org.apache.axis2 依赖项,它具有 javax.mail 的另一个子依赖项和 diff。版本。我排除了这种依赖。如下;解决了这个问题。
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5.1</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>