Hari ini mau tulis tentang javamail, yakni mengirim file menggunakan javamail. Oke di postingan kemarin sudah dibahas sedikit mengenai javamail. Dan postingan kali ini dengan tambahan file yang akan dikirim. Langsung saja ke kode.
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
*
* @author user
*/
public class SendFileEmail {
public static void main(String[] args) {
SendFileEmail sf = new SendFileEmail();
sf.excute();
}
public void excute() {/**
*
* @author user
*/
public class SendFileEmail {
public static void main(String[] args) {
SendFileEmail sf = new SendFileEmail();
sf.excute();
}
// Recipient's email ID needs to be mentioned.
String to = "accountTo@gmail.com";
// Sender's email ID needs to be mentioned
String from = "accountFrom@officemail.com";
// Setup mail server
// Sender's email ID needs to be mentioned
String from = "accountFrom@officemail.com";
// Setup mail server
Properties properties = new Properties();
properties.put("mail.smtp.host", "mail.officemail.com");
properties.put("mail.smtp.auth", "true");
properties.put("mail.debug", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.host", "mail.officemail.com");
properties.put("mail.smtp.auth", "true");
properties.put("mail.debug", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.port", "465");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("accountFrom@officemail.com", "xxxxxx");
}
});
try {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("accountFrom@officemail.com", "xxxxxx");
}
});
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("Test Java Non File!");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Hello Ardi!!!");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "Text.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("Test Java Non File!");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Hello Ardi!!!");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "Text.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Dari code di atas nampak jelas jika ingin melakukan attachment file, kita bisa memanfaatkan class DataSource, BodyPart dan MimeBodyPart.
Cukup sekian dari saya. Kalau kurang jelas bisa dilihat pada di link ini.
Wasslama :senyum
0 komentar:
Posting Komentar