2012-08-09 57 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

相關問題