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
The left association operator % is used in PHP for?
What is session in php why it is use?
Can php use gettext?
What is use of header() function in php?
Explain the different types of errors in php.
What is the default time in seconds for which session data is considered valid?
What is the importance of parser in php?
How can I increase my website session?
How can you submit a form without a submit button?
Which variable declarations within a class is invalid in php?
Is it possible to protect special characters in a query string?
How to create a table using php?
What is use of mysqli_query in php?
What are the uses of explode() and implode() functions?
What does explode do in php?