import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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 MailSender {
public final String mailServerAddress;
public final int mailServerPort;
public final String senderAddress;
public MailSender(String mailServerAddress, int mailServerPort, String senderAddress) {
this.senderAddress = senderAddress;
this.mailServerAddress = mailServerAddress;
this.mailServerPort = mailServerPort;
}
public void sendMail(String to[], String cc[], String bcc[], String replyTo[], String subject, String body, String[] attachments) {
if (to == null || to.length <= 0) {
System.out.println("sendMail To address is NULL for email");
}
if (subject == null || subject.length() <= 0) {
System.out.println("sendMail Subject is NULL for email");
}
if (body == null || body.length() <= 0) {
System.out.println("sendMail Body is NULL for email");
}
Properties props = new Properties();
// Specify the desired SMTP server and port
props.put("mail.smtp.host", mailServerAddress);
props.put("mail.smtp.port", Integer.toString(mailServerPort));
props.put("mail.smtp.auth", "true");
//TODO can we create session only once, with some session validation
Session session = Session.getInstance(props, null);
// create a new MimeMessage object (using the Session created above)
Message message = new MimeMessage(session);
StringBuilder addresses = new StringBuilder();
addresses.append("FROM:").append(senderAddress);
try {
message.setFrom(new InternetAddress(senderAddress));
// TO:
InternetAddress[] toAddresses = new InternetAddress[to.length];
addresses.append(" TO:");
for (int i = 0; i < to.length; i++) {
toAddresses[i] = new InternetAddress(to[i]);
addresses.append(to[i] + ";");
}
message.setRecipients(Message.RecipientType.TO, toAddresses);
// CC:
if (cc != null && cc.length > 0) {
InternetAddress[] ccAddresses = new InternetAddress[cc.length];
addresses.append(" CC:");
for (int i = 0; i < cc.length; i++) {
ccAddresses[i] = new InternetAddress(cc[i]);
addresses.append(cc[i] + ";");
}
message.setRecipients(Message.RecipientType.CC, ccAddresses);
}
// BCC:
if (bcc != null && bcc.length > 0) {
InternetAddress[] bccAddresses = new InternetAddress[bcc.length];
addresses.append(" BCC:");
for (int i = 0; i < bcc.length; i++) {
bccAddresses[i] = new InternetAddress(bcc[i]);
addresses.append(bcc[i] + ";");
}
message.setRecipients(Message.RecipientType.BCC, bccAddresses);
}
// ReplyTo:
if (replyTo != null && replyTo.length > 0) {
InternetAddress[] replyToAddresses = new InternetAddress[replyTo.length];
addresses.append(" REPLYTO:");
for (int i = 0; i < replyTo.length; i++) {
replyToAddresses[i] = new InternetAddress(replyTo[i]);
addresses.append(replyTo[i] + ";");
}
message.setReplyTo(replyToAddresses);
}
// Subject:
message.setSubject(subject);
addresses.append(" SUBJECT:").append(subject);
// Body:
Multipart multipart = new MimeMultipart();
MimeBodyPart mimeBody = new MimeBodyPart();
mimeBody.setText(body);
multipart.addBodyPart(mimeBody);
// Attachments:
if (attachments != null && attachments.length > 0) {
for (String attachment : attachments) {
MimeBodyPart mimeAttachment = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
mimeAttachment.setDataHandler(new DataHandler(source));
mimeAttachment.setFileName(attachment);
multipart.addBodyPart(mimeAttachment);
}
}
message.setContent(multipart);
// Send
//Transport.send(message);
String username = "amol@postmaster";
String password = "amol";
Transport tr = session.getTransport("smtp");
tr.connect(mailServerAddress, username, password);
message.saveChanges(); // don't forget this
tr.sendMessage(message, message.getAllRecipients());
tr.close();
System.out.println("sendmail success " + addresses);
} catch (AddressException e) {
System.out.println("sendMail failed " + addresses);
e.printStackTrace();
} catch (MessagingException e) {
System.out.println("sendMail failed " + addresses);
e.printStackTrace();
}
}
public static void main(String s[]) {
if (s.length < 3) {
System.out.println("Usage: MailSender RelayAddress SendersAddress ToAddress [ AttachmentFileName ]");
System.exit(-1);
}
int k = 0;
String relay = s[k++];
String sender = s[k++];
String[] toAddresses = new String[] {s[k++]};
String[] attachmentFileName = new String[0];
if (s.length == 4) {
attachmentFileName = new String[] {s[k++]};
}
MailSender mailSender = new MailSender(relay, 25, sender);
String[] mailTo = toAddresses;
String[] mailCC = new String[] {};
String[] mailBCC = new String[] {};
String[] replyTo = new String[] {};
String mailSubject = "Test Mail";
String mailBody = "Mail sent using test utility";
mailSender.sendMail(mailTo, mailCC, mailBCC, replyTo, mailSubject, mailBody, attachmentFileName);
}
}