Mengirim email dengan File attachment menggunakan java……
import java.util.***********dst***
public class SendMail {
String host;
String from;
String to;
String subject;
String message;
String localPath;
public SendMail(String host, String from, String to, String subject, String message, String PathToSend) {
this.host = host;
this.from = from;
this.to = to;
this.subject = subject;
this.message = message;
this.localPath = PathToSend;
}
public void diIt(){
MimeMessage msg;
BodyPart messageBodyPart;
Multipart multipart;
DataSource source;
try {
Properties prop = System.getProperties();
prop.put(“mail.smtp.host”, host);
Session ses1 = Session.getDefaultInstance(prop, null);
msg = new MimeMessage(ses1);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
// Create the message part
messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(message);
multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
File dir = new File(this.localPath);
String [] localfile = dir.list();
for(int i=0, len = localfile.length; i<len; i++){
messageBodyPart = new MimeBodyPart();
File fl = new File(new File(localPath),localfile[i]);
if(fl.isFile()){
source = new FileDataSource(fl);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
}
}
msg.setContent(multipart);
msg.setSentDate(new Date());
Transport.send(msg);
for(int i=0, len = localfile.length; i<len; i++){
File fl = new File(new File(localPath),localfile[i]);
if(fl.isFile()){
this.moveToHistory(fl);
}
}
dir = null;
localfile = null;
System.out.println(“Send Process End”);
} catch (javax.mail.MessagingException me) {
me.printStackTrace();
} finally{
source = null;
multipart = null;
messageBodyPart = null;
msg = null;
}
}
public void moveToHistory(File file) {
int len = 0, pos = 0;
String filename = file.getName();
String temp = filename;
pos = temp.lastIndexOf(“.”);
temp = temp.substring(pos);
String name = filename.substring(0, pos);
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat(“yyyyMMdd.HHmmss.S”);
String dateSTR = df.format(date);
name += “.” + dateSTR + “.” + temp;
try {
File history = new File(new File(this.localPath),”history”);
if (!history.exists()) {
history.mkdirs();
}
//File file = new File(new File(this.localPath), filename);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(history, name)));
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buffer = new byte[1000];
while ((len = bis.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}
bos.flush();
bos.close();
bis.close();
fis.close();
file = null;
} catch (Exception e) {
e.printStackTrace();
}finally{
File fl = new File(this.localPath + File.separator + filename);
boolean deleted = fl.delete();
if (!(deleted)) {
System.err.println(“Could not delete file ” + fl.getName());
}
}
}
}