2012-08-09 69 views
2

试图获得玩!用附件发送电子邮件的框架。下面的代码工作正常,如果我不附加到消息。我已经尝试过使用Play的Mailer类和Apache Commons类(如下所示),但在这两种情况下,页面都只有一个微调器(Chrome),并且不会收到任何电子邮件。如何使用Play中的附件发送电子邮件!框架1.2.4

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

emailaddress = "[email protected]"; 

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

回答

6

即时猜测你已经有了一看Examples for Apache CommonsSending e-mail - Play! Framework 1.1

国际海事组织我建议使用一个知名的图书馆加载文件和例如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 = "[email protected]"; 
    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

参考文献:

+0

版本被谈到了将在第一正确地产生所述附件的问题地方 - 它工作正常从浏览器或cURL请求,而不是试图将其拉入字节阵列时。既然你的答案对于原始问题是有帮助和准确的,我会把它标记为可接受的。 – 2012-08-09 15:23:20

相关问题