How to attach multiple files to an email using JavaMail?

在 java 的世界裡要寄Email+附件還滿簡單的,sample code 如下:

import java.util.Properties;
import javax.activation.*;
import javax.mail.*;
public class MailProjectClass {
public static void main(String[] args) {
final String username = "[email protected]";
final String password = "your.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 {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("PFA");
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String file = "path of file to be attached";
String fileName = "attachmentName";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

需要增加多個附件的 sample code:

private static void addAttachment(Multipart multipart, String filename)
{
DataSource source = new FileDataSource(filename);
BodyPart messageBodyPart = new MimeBodyPart();        
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
}

Then from your main code, just call:

addAttachment(multipart, "file1.txt");
addAttachment(multipart, "file2.txt");

如果不想 create file 再用 DataSource 去選 file,可以用這一個 sample:

how to attach file without creating file
https://stackoverflow.com/questions/3556091/javamail-problem-how-to-attach-file-without-creating-file

import javax.activation.DataHandler;
import javax.activation.DataSource;
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
**mbp2.attachFile(filename);**
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);

MimeBodyPart mbp = new MimeBodyPart();
String data = "any ASCII data";
DataSource ds = new ByteArrayDataSource(data, "application/x-any");
mbp.setDataHandler(new DataHandler(ds));

 


Sample 2:

MimeBodyPart attachmentPart = new MimeBodyPart();
try {
DataSource ds = new ByteArrayDataSource(attachment.getBytes("UTF-8"), "application/octet-stream");
attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(ds));
} 
catch (Exception e) {
Logger.getLogger("Blina").log(Level.SEVERE, Misc.getStackTrace(e));
}
attachmentPart.setFileName(fileName);
multipart.addBodyPart(attachmentPart);
// Put parts in message
msg.setContent(multipart);

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *