我正在尝试使用TomEE在Web应用程序中使用javamail发送电子邮件。我的问题是,当我附加PDF等文件时,我所收到的文件被命名为“noname”。我也没有收到正文。
作为观察,如果我从“主”程序(在其他项目中)执行我的代码,则电子邮件将被完美地发送。这是代码:
public class EnviaCorreo{
static Properties mailServerProperties;
static Session getMailSession;
static MimeMessage generateMailMessage;
public void generateAndSendEmail() throws AddressException, MessagingException {
System.out.println("\n 1st ===> setup Mail Server Properties..");
mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtp.port", "587");
mailServerProperties.put("mail.smtp.auth", "true");
mailServerProperties.put("mail.smtp.starttls.enable", "true");
System.out.println("Mail Server Properties have been setup successfully..");
System.out.println("\n\n 2nd ===> get Mail Session..");
getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("xxx@gmail.com"));
generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("yyy@gmail.com"));
generateMailMessage.setSubject("Foo store has bought strawberries");
MimeMultipart multiParte = new MimeMultipart();
BodyPart adjunto = new MimeBodyPart();
adjunto.setDataHandler(new DataHandler(new FileDataSource("/home/foo/Desktop/FooProject/src/main/resources/fruit/6781430324446945.pdf")));
adjunto.setFileName("readme.pdf");
BodyPart texto = new MimeBodyPart();
texto.setText("Success!!");
multiParte.addBodyPart(texto);
multiParte.addBodyPart(adjunto);
generateMailMessage.setContent(multiParte, "text/html");
System.out.println("Mail Session has been created successfully..");
System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");
transport.connect("smtp.gmail.com", "yyy@gmail.com", "foopsswd123()");
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
}
我正在与TomEE和iText合作生成PDF。我应该将文件夹放在哪里,我将动态生成的PDF保存到?
答案 0 :(得分:1)
TomEE默认使用geronimo javamail(在tomee lib中)。您可以使用main中使用的版本替换它,并添加geronimo-locator和geronimo-registry http://repo1.maven.org/maven2/org/apache/geronimo/specs/geronimo-osgi-locator/1.1/geronimo-osgi-locator-1.1.jar 和http://repo1.maven.org/maven2/org/apache/geronimo/specs/geronimo-osgi-registry/1.1/geronimo-osgi-registry-1.1.jar以及libs
如果您不想更改版本,可以使用tomee.xml会话来注入会话。我知道对于gmail,你必须提供一个身份验证器,例如geronimo javamail