Java 如何从Play发送带有附件的电子邮件!框架1.2.4
试着去玩!框架发送带有附件的电子邮件。如果我不在邮件中添加附件,下面的代码可以正常工作。我已经尝试了Play的Mailer类和ApacheCommons类(如下所示),但在这两种情况下,页面都有一个微调器(Chrome),没有收到电子邮件Java 如何从Play发送带有附件的电子邮件!框架1.2.4,java,playframework,apache-commons-email,Java,Playframework,Apache Commons Email,试着去玩!框架发送带有附件的电子邮件。如果我不在邮件中添加附件,下面的代码可以正常工作。我已经尝试了Play的Mailer类和ApacheCommons类(如下所示),但在这两种情况下,页面都有一个微调器(Chrome),没有收到电子邮件 EmailAttachment attachment = new EmailAttachment(); attachment.setURL(new URL(base + "public/images/triangles.png")); attachment.s
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();
EmailAttachment attachment=新的EmailAttachment();
附件.setURL(新URL(base+“public/images/triangles.png”);
附件.setDisposition(EmailAttachment.attachment);
附件.试验说明(“试验”);
附件:设置名称(“测试”);
电子邮件地址=”test@test.com";
MultipartMail电子邮件=新的MultipartMail();
email.setDebug(true);
email.addTo(emailaddress);
电子邮件。setFrom(“测试”);
电子邮件。设置主题(“测试电子邮件”);
尝试
{
电子邮件。附上(附件);
}
捕获(EmailException-ex)
{
System.out.println(例如getMessage());
}
email.setMsg(“测试电子邮件”);
email.send();
我猜你已经看过了和
在我看来,我建议使用一个著名的库,里面有大量的文档和示例,比如和它们的
以下是一些教程,可以让您立即开始学习:
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();
}
}
}
嗯
参考文献: