package com.z0ltan.mail.exchange;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.URLDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmail {
private static final int MAX_SMTP_SIZE = 5; // MB
public static void main(String[] args) {
String fromAddress = "user@address.com";
String smtpAddress = "10.0.45.1";
int smtpPort = 25;
String attachmentLink
= "http://localhost:9999/examples/servlets/ultimate.pdf";
String[] toAddress = { "timmy.jose@hp.com", "zoltan.jose@gmail.com" };
String subject = "Report file and link";
// First create the Properties object and use it to obtain the Session
// object
Properties props = new Properties();
props.put("mail.smtp.host", smtpAddress);
props.put("mail.smtp.port", smtpPort);
// props.put("mail.smtp.auth", true);
Session session = Session.getInstance(props, null);
session.setDebug(true);
// Create the Message
final MimeMessage reportMessage = new MimeMessage(session);
if (subject != null) {
try {
reportMessage.setSubject(subject, "UTF-8");
} catch (MessagingException e) {
e.printStackTrace();
}
}
// Create the addresses and add to message body
try {
Address from = new InternetAddress(fromAddress);
reportMessage.setFrom(from);
Address[] to = new InternetAddress[toAddress.length];
for (int i = 0; i < toAddress.length; i++) {
to[i] = new InternetAddress(toAddress[i]);
}
reportMessage.addRecipients(Message.RecipientType.TO, to);
// Add the attachment and the attachment link to the e-mail body
BodyPart messageBodyPart = new MimeBodyPart();
BodyPart attachBodyPart = null;
Multipart multipart = new MimeMultipart();
if (isAttachmentSmallerThanMaxSmtpSize(attachmentLink)) {
attachBodyPart = new MimeBodyPart();
DataSource source
= new URLDataSource(new URL(attachmentLink));
attachBodyPart.setDataHandler(new DataHandler(source));
String file = attachmentLink.substring(attachmentLink
.lastIndexOf('/'));
attachBodyPart.setFileName(file);
multipart.addBodyPart(attachBodyPart);
messageBodyPart.setContent(attachmentLink,
"text/plain;charset=UTF-8");
} else {
String messageAndAttachment = "Note: The report file: "
+ attachmentLink.substring(attachmentLink
.lastIndexOf('/') + 1)
+ " was not attached since it exceeds the user "
+ "configured Maximum SMTP Attachment size."
+ "\n\n" + attachmentLink;
messageBodyPart.setContent(messageAndAttachment,
"text/plain;charset=UTF-8");
}
multipart.addBodyPart(messageBodyPart);
reportMessage.setContent(multipart);
reportMessage.saveChanges();
// Send the e-mail
(new Thread() {
public void run() {
try {
Transport.send(reportMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}).start();
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
private static boolean isAttachmentSmallerThanMaxSmtpSize(
String attachmentLink) {
boolean status = false;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(
attachmentLink).openConnection();
int sizeInMB = connection.getContentLength() / (1024 * 1024);
if (sizeInMB > MAX_SMTP_SIZE) {
status = false;
} else {
status = true;
}
} catch (Exception ex) {
status = false;
}
return status;
}
}