我正在尝试使用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("[email protected]"));
    generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("[email protected]"));
    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", "[email protected]", "foopsswd123()");
    transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
    transport.close();
}


奖金

我正在使用TomEE和iText进行PDF生成。我应该将动态生成的PDF保存到的文件夹放在哪里?

最佳答案

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的身份验证器

09-26 02:55