1.public class emil
{
public static String file[] = {System.getProperty("user.dir")+"//test-output//index.html"};
public static String toAddress[]={"rameshkudikala83@gmail.com","ramesh.kudikala@NextSphere.com","jagadeeswara.urlana@nextsphere.com"};
public static void main(String[] args) throws Exception
{
Mail();
}
public static void Mail() throws Exception
{
if(EmailSender.SendMail("rameshkudikala83@gmail.com", "nani@11210", "Automation Scripts Report for Australia Market", toAddress, file)){
System.out.println("Email has been sent");
// logger.info("Email has been sent");
}else{
//logger.info("Error occured in sending email");
}
}
}
2. package Enroll;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
public class EmailSender extends Authenticator {
public static Properties props =null;
public String user=null;
public String pw=null;
public EmailSender(String username,String password){
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
public static boolean SendMail(String fromMail,String password,String message,String toMail[],String[] attachFiles) throws Exception
{
String host="smtp.gmail.com";
props= System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", fromMail);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props,new EmailSender(fromMail, password));
MimeMessage mimeMessage=new MimeMessage(session);
try{
mimeMessage.setFrom(new InternetAddress(fromMail));
//now get the address of reciepient
InternetAddress[] toAddress=new InternetAddress[toMail.length];
for(int i=0;i<toMail.length;i++){
toAddress[i]=new InternetAddress(toMail[i]);
}
for(int i=0;i<toAddress.length;i++){
mimeMessage.addRecipient(RecipientType.TO, toAddress[i]);
}
mimeMessage.setSubject("Automation Status");
/*MimeBodyPart attachPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();*/
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
/*String file = "D:/MonaVieWorkSpace/NorthAmericaCoreFeature_US/screenshots/ITR2/PersonalOrder/Ordersummary.png";
String filename="OrderSummaryScreenShot";
DataSource source = new FileDataSource(file);
System.out.println(file);
System.out.println(filename);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(file).getName());
multipart.addBodyPart(attachPart);
mimeMessage.setContent(multipart );
System.out.println("Attachment is added");*/
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Attachment has been added");
multipart.addBodyPart(attachPart);
}
}
/*mimeMessage.setText(message);*/
mimeMessage.setContent(multipart);
Transport transport=session.getTransport("smtp");
transport.connect(host, fromMail, password);
Transport.send(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
return true;
}catch(MessagingException me){
me.printStackTrace();
}
return false;
}
}
{
public static String file[] = {System.getProperty("user.dir")+"//test-output//index.html"};
public static String toAddress[]={"rameshkudikala83@gmail.com","ramesh.kudikala@NextSphere.com","jagadeeswara.urlana@nextsphere.com"};
public static void main(String[] args) throws Exception
{
Mail();
}
public static void Mail() throws Exception
{
if(EmailSender.SendMail("rameshkudikala83@gmail.com", "nani@11210", "Automation Scripts Report for Australia Market", toAddress, file)){
System.out.println("Email has been sent");
// logger.info("Email has been sent");
}else{
//logger.info("Error occured in sending email");
}
}
}
2. package Enroll;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
public class EmailSender extends Authenticator {
public static Properties props =null;
public String user=null;
public String pw=null;
public EmailSender(String username,String password){
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
public static boolean SendMail(String fromMail,String password,String message,String toMail[],String[] attachFiles) throws Exception
{
String host="smtp.gmail.com";
props= System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", fromMail);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props,new EmailSender(fromMail, password));
MimeMessage mimeMessage=new MimeMessage(session);
try{
mimeMessage.setFrom(new InternetAddress(fromMail));
//now get the address of reciepient
InternetAddress[] toAddress=new InternetAddress[toMail.length];
for(int i=0;i<toMail.length;i++){
toAddress[i]=new InternetAddress(toMail[i]);
}
for(int i=0;i<toAddress.length;i++){
mimeMessage.addRecipient(RecipientType.TO, toAddress[i]);
}
mimeMessage.setSubject("Automation Status");
/*MimeBodyPart attachPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();*/
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
/*String file = "D:/MonaVieWorkSpace/NorthAmericaCoreFeature_US/screenshots/ITR2/PersonalOrder/Ordersummary.png";
String filename="OrderSummaryScreenShot";
DataSource source = new FileDataSource(file);
System.out.println(file);
System.out.println(filename);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(file).getName());
multipart.addBodyPart(attachPart);
mimeMessage.setContent(multipart );
System.out.println("Attachment is added");*/
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Attachment has been added");
multipart.addBodyPart(attachPart);
}
}
/*mimeMessage.setText(message);*/
mimeMessage.setContent(multipart);
Transport transport=session.getTransport("smtp");
transport.connect(host, fromMail, password);
Transport.send(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
return true;
}catch(MessagingException me){
me.printStackTrace();
}
return false;
}
}
No comments:
Post a Comment