Java Code to send email
package com;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
/********
* This class is used to utility that sends e-mail messages.
* @author TechHards
*******/
public class EmailSender {
/*********
* Sends an e-mail with attachments.
* @param host address of the server
* @param port port number of the server
* @param userName email address used to send mails
* @param password password of the email account
* @param toAddress email address to send
* @param subject title of the email
* @param message content of the email
* @param attachFiles an array of file paths
* @throws AddressException
* @throws MessagingException
********/
public void sendEmail(String host, String port, String userName, String password,
String toAddress, String subject, String message, String[] attachFiles)
throws AddressException, MessagingException {
// sets SMTP properties----
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "false");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new SMTPAuthenticator(userName, password);
//Authenticator auth = new SMTPAuthenticator("finance", password);
System.out.println("Authenticator reached"+password);
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
addAttachment_fnc(multipart, filePath);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
/******
* You can add a file as an attachment to the email's content
* @param p_multipart
* @param p_filePath
* @throws MessagingException
*****/
private void addAttachment_fnc(Multipart p_multipart, String p_filePath) throws MessagingException {
MimeBodyPart l_attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(p_filePath);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(p_filePath).getName());
p_multipart.addBodyPart(l_attachPart);
}
/**
* This class is used to provides authentication information.
* @author TechHards
*
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {
private String userName;
private String password;
public SMTPAuthenticator(String p_userName, String p_password) {
this.userName = p_userName;
this.password = p_password;
System.out.println("#SMTPAuthenticator reached.User:"+p_userName);
System.out.println("SMTPAuthenticator reached.pwd:"+p_password);
}
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("# PassAuthentication reached point");
return new PasswordAuthentication(userName, password);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------Call Java Program to send email
-----------------------------------------------------------------------------------------------------------------------------
package com;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import com.EmailSender.*;
import com.EmailSender;
public class TechHardsEmailSender {
public static void main(String[] args) throws AddressException, MessagingException {
String host = "mailgate";
String port = "25";
String mailFrom = "techhards.admin@outlook.com";
String mailTo = "ebs@dilmahtea.com";
//#String mailTo = "techhards.admin@gmail.com";
String password = "Engineer";
String userName = "Engineer.admin";
String subject = "News email";
String bodyMessage = "<html><b>Hello</b><br/><i>This is an HTML email with an attachment</i></html>";
EmailSender sender = new EmailSender();
String[] fileAttachment = null ;//{"/tmp/a.pdf"};
sender.sendEmail(host, port, mailFrom, password, mailTo, subject, bodyMessage, fileAttachment);
}
}
--###############################################################################---
No comments:
Post a Comment