public class SendEmail {
public static void main(String[] args) {
//authentication info
final String username = "mailing@gmail.com";
final String password = "pass";
String fromEmail = "mailing@gmail.com";
String toEmail = "mailing@yahoo.com";
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
//Start our mail message
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(fromEmail));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
msg.setSubject("Subject Line");
Multipart emailContent = new MimeMultipart();
//Text body part
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("My multipart text");
//Attachment body part.
MimeBodyPart pdfAttachment = new MimeBodyPart();
pdfAttachment.attachFile("/Users/hmidi/Downloads/BigData.pdf");
//Attach body parts
emailContent.addBodyPart(textBodyPart);
emailContent.addBodyPart(pdfAttachment);
//Attach multipart to message
msg.setContent(emailContent);
Transport.send(msg);
System.out.println("Sent message");
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} `I am trying to develop a web application with JavaEE.I would like to add an automatic email sending functionality as after registration but I have no idea. if anyone could help me, i would be very grateful.