JavaMail, sending emails from your Java application

The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with Java SE platform and is also included in the Java EE platform. Download JavaMail jar files from here. Full JavaMail API. For a full example with source code of JavaMail, click on “more” link down here.

[java]
package mailSender;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

//import SendMailUsingAuthentication.SMTPAuthenticator;

import java.util.*;
import java.io.*;

public class Sender {

private static final String SMTP_HOST_NAME = "mail.yourservername.com";
private static final String SMTP_AUTH_USER = "username";
private static final String SMTP_AUTH_PWD = "PaSsWoRd";

private static final String emailMsgTxt = "Message sent by JavaMail";
private static final String emailSubjectTxt = "Sent email by JavaMail";
private static final String emailFromAddress = "your_email@your_website.com";

// Add List of Email address to who email needs to be sent to
private static final String[] emailList = { "some_email1@some_website.com",
"some_email2@some_website.com", "some_email3@some_website.com" };

/**
* @param args
*/
public static void main(String[] args) {
Sender smtpMailSender = new Sender();

// Attempt #1, sending email without attachment
try {
smtpMailSender.sendEmail(emailList, emailSubjectTxt, emailMsgTxt,
emailFromAddress);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Attempt #2, sending email with attachment
try {
smtpMailSender.sendEmailWithAttachment(emailList, emailSubjectTxt,
emailMsgTxt, emailFromAddress,
"Logg utskiftning av deler 0B.xlsx");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// end of main()

public void sendEmail(String recipients[], String subject, String message,
String from) throws MessagingException {
boolean debug = false;

// Host SMTP address
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
// setting up authentication
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

// Session
session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

// From Addresses
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

// Making an array for multiple addresses
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Subject
msg.setSubject(subject);

// Setting msg content, here text/plain
msg.setContent(message, "text/plain");

// sending msg
Transport.send(msg);
}

public void sendEmailWithAttachment(String recipients[], String subject,
String messageText, String from, String fileAddress)
throws MessagingException {

// Get system properties
Properties props = System.getProperties();

// SMTP address
props.put("mail.smtp.host", SMTP_HOST_NAME);

//Session and authentication
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

// Defining message
MimeMessage msg = new MimeMessage(session);

// To address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

// Recipients list, Array of to addresses
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// setting subjct
msg.setSubject(subject); // "Hello JavaMail with Attachment"

// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();

// Setting message text
messageBodyPart.setText(messageText);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAddress);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAddress);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
msg.setContent(multipart);

// Send the message
Transport.send(msg);
}

private class SMTPAuthenticator extends javax.mail.Authenticator {

public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}

}
[/java]