How can we send mail using JavaScript?

Answer Posted / muralidhar k

public void sendMail(String from, String to, String
subject, String messageBody, String[] attachments) throws
Exception
{
Properties props = System.getProperties();

props.put(SMTPHost, SMTPServer);
Session session = Session.getDefaultInstance
(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient
(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart
();
messageBodyPart.setText(messageBody);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
addAtachments(attachments, multipart);
message.setContent(multipart);
Transport.send(message);
}

protected void addAtachments(String[] attachments,
Multipart multipart) throws Exception
{
for(int i = 0; i<= attachments.length-1;
i++)
{
String filename = attachments[i];
MimeBodyPart attachmentBodyPart =
new MimeBodyPart();
DataSource source = new
FileDataSource(filename);
attachmentBodyPart.setDataHandler
(new DataHandler(source));
if (i==0)
{

attachmentBodyPart.setFileName("ResultLog.txt");
}
if (i==1)
{

attachmentBodyPart.setFileName("ErrorLog.txt");
}
multipart.addBodyPart
(attachmentBodyPart);
}
}

public static void Mail() throws Exception
{
try
{
String bv = BuildVersion;
DriverScript client = new
DriverScript();
String from
= "murali@gmail.com";
String subject = "REG : " +
ProjectName + " Regression Suite Execution for Build " +
bv ;
String message
= "Hello,\n\n" +
"Regression Suite Execution for " +
ProjectName + " has been completed for the build " +
BuildVersion + ".\n\n" +
"Please find the attached
Result Logs and Error Logs\n\n" +

"Thanks.\n\n" +
"Regards,\n" +
"ZVS - Automation Testing
Team || Zylog Systems Ltd\n" +
"---------------------------
-------------------------------------------------------" +
"\n" +
"Please do not reply to
this Email!! It is only an Automated Notification";

String ResultLog = TestLog
+ "ExecutionLog_" + DriverScript.GetDate() + ".txt";
String ErrorLog = TestLog
+ "ErrorLog_" + DriverScript.GetDate() + ".txt";


String[] filenames1 =
{ResultLog, ErrorLog};
String Rec =
No_Of_Recipients;
int Recipients =
Integer.parseInt(Rec);
if
(SendMail.equalsIgnoreCase("Y"))
{
for (int i=1;
i<=Recipients; i++)
{

String To
= "Recipient" + i;
String To1 =
DriverScript.Config(To);
client.sendMail
(from, To1, subject, message, filenames1);
}
}
}
catch(Exception e)
{
DriverScript.ErrorInfo
(e.getMessage());
e.printStackTrace();
}
}

Is This Answer Correct ?    17 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the methods of array in java?

487


Is php strongly typed?

605


What is ci in php?

537


How should a model be structured in mvc?

525


What is the different between count() and sizeof() in php?

516






What are sql injections, how do you prevent them and what are the best practices?

498


Is php better than python?

547


What is self in php?

553


Why do we use in php?

542


What are the different tables(engine) present in mysql, which one is default?

508


Write a query to find the 2nd highest salary of an employee from the employee table?

642


Which function would you use to merge two arrays in php?

573


What are some new features introduced in php7?

8375


How can we create a database using php?

594


How do you explain independent and dependent variables?

491