【问题标题】:How to send an email with an attachment from Play! Framework 1.2.4如何从 Play 发送带有附件的电子邮件!框架 1.2.4
【发布时间】:2012-08-09 20:59:17
【问题描述】:

尝试玩游戏!发送带有附件的电子邮件的框架。如果我不将附件添加到邮件中,下面的代码可以正常工作。我已经尝试过使用 Play 的 Mailer 类和 Apache Commons 类(如下所示),但在这两种情况下,页面都只是用微调器(Chrome)坐在那里,没有收到任何电子邮件。

EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL(base + "public/images/triangles.png"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("test");
attachment.setName("test");

emailaddress = "test@test.com";

MultiPartEmail email = new MultiPartEmail();
email.setDebug(true);
email.addTo(emailaddress);
email.setFrom("Testing <test@test.com>");
email.setSubject("Testing email");
try
{
    email.attach(attachment);
}
catch (EmailException ex)
{
    System.out.println(ex.getMessage());
}
email.setMsg("test email");
email.send();

【问题讨论】:

    标签: java playframework apache-commons-email


    【解决方案1】:

    我猜你已经看过Examples for Apache CommonsSending e-mail - Play! Framework 1.1

    IMO 我建议使用带有大量文档和示例的知名库,例如 JavaMail 和他们的 api

    这里有一些教程可以帮助您立即开始:

    使用 JavaMail 通过 gmail 发送带有附件的电子邮件的示例是:

    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class SendMailTLS {
    
    public static void main(String[] args) {
    
        final String username = "username@gmail.com";
        final String password = "password";
    
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
    
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
    
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
    
        try {
    
            // Define message
            MimeMessage message =
                    new MimeMessage(session);
            message.setFrom(
                    new InternetAddress(from));
            message.addRecipient(
                    Message.RecipientType.TO,
                    new InternetAddress(to));
            message.setSubject(
                    "Hello JavaMail Attachment");
    
            // create the message part 
            MimeBodyPart messageBodyPart =
                    new MimeBodyPart();
    
            //fill message
            messageBodyPart.setText("Hi");
    
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
    
            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            DataSource source =
                    new FileDataSource(fileAttachment);
            messageBodyPart.setDataHandler(
                    new DataHandler(source));
            messageBodyPart.setFileName(fileAttachment);
            multipart.addBodyPart(messageBodyPart);
    
            // Put parts in message
            message.setContent(multipart);
    
            // Send the message
            Transport.send(message);
    
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    }

    HTH

    参考资料:

    【讨论】:

    • 问题被证明是首先正确生成附件的问题 - 它在浏览器或 cURL 请求中工作正常,只是在尝试将其拉入字节数组时无法正常工作。由于您的回答对原始问题有帮助且准确,因此我将其标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2012-06-07
    相关资源
    最近更新 更多