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

Explain preg_Match and preg_replace?

636


Which software is used to run php programs?

598


How to make a class in php?

619


What is an operator in php?

653


What is the Default syntax used in PHP?

667






What is the use of dual table in mysql?

595


Explain PHP looping?

628


What is curl php?

602


What are the data types in php?

619


What is the difference between the include() and require() functions?

999


Tell me how can we display information of a variable and readable by human with php?

647


What is php static function?

620


What is the apache?

600


What does the scope of variables mean?

618


How to create an empty array in php?

690