Following program can be used to send a mail using java:
I am using gmail smtp server here
package com;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail {
// TODO: Change email ID and password
String d_email = "changeme@gmail.com"; // Give your complete mail ID here,
// like abc@gmail.com
String d_password = "changeme"; // Give your email ID password
String d_host = "smtp.gmail.com", d_port = "465";
String mail_to = "changeme@gmail.com";
String mail_subject = "Testing";
String mail_text = "Hey, this is the testing email.";
String[] filesToBeAttached = { "D:/Temp/two.pdf", "D:/Temp/one.zip",
"D:/Temp/three.pdf" };
public SendMail() {
Properties props = new Properties();
props.put("mail.debug", true);
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText(mail_text);
msg.setSubject(mail_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
mail_to));
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mail_text, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
for (int i = 0; i < this.filesToBeAttached.length; i++) {
messageBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(
this.filesToBeAttached[i]);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setFileName(fds.getName());
multipart.addBodyPart(messageBodyPart);
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
Transport.send(msg);
} catch (Exception mex) {
mex.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}
public static void main(String[] args) {
new SendMail();
}
}
I am using gmail smtp server here
package com;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail {
// TODO: Change email ID and password
String d_email = "changeme@gmail.com"; // Give your complete mail ID here,
// like abc@gmail.com
String d_password = "changeme"; // Give your email ID password
String d_host = "smtp.gmail.com", d_port = "465";
String mail_to = "changeme@gmail.com";
String mail_subject = "Testing";
String mail_text = "Hey, this is the testing email.";
String[] filesToBeAttached = { "D:/Temp/two.pdf", "D:/Temp/one.zip",
"D:/Temp/three.pdf" };
public SendMail() {
Properties props = new Properties();
props.put("mail.debug", true);
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText(mail_text);
msg.setSubject(mail_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
mail_to));
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mail_text, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
for (int i = 0; i < this.filesToBeAttached.length; i++) {
messageBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(
this.filesToBeAttached[i]);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setFileName(fds.getName());
multipart.addBodyPart(messageBodyPart);
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
Transport.send(msg);
} catch (Exception mex) {
mex.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}
public static void main(String[] args) {
new SendMail();
}
}