import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args) throws IOException
{
// http://www.tutorialspoint.com/java/java_sending_email.htm
String host = "smtp.gmail.com";
final String from = "xxxxx@gmail.com";
final String pass = "xxxx";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String to = "xxxx@xxxx.com";
// Get the default Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress("testwidgets11@gmail.com"));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
String header = "<HTML><BODY><table border=\"2\">";
String footer = "</table></BODY></HTML>";
BufferedReader br = new BufferedReader(
new FileReader("c:\\source.html"));
String line;
String htmlSource = "";
while ((line=br.readLine())!=null) {
htmlSource = htmlSource.concat(line);
// System.out.println(line);
}
System.out.println(htmlSource);
String table = "<table border=\"1\"><tr><td style=\"background-color:red;\">Failed</td><td>Class</td><td>Test</td><td>Reason</td></tr><tr><td style=\"background-color:yellow;\">Skipped</td><td>Class</td><td>Test</td><td>Reason</td></tr></table>";
table = htmlSource;
message.setContent(header+table+footer, "text/html" );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Wednesday, May 23, 2012
Tuesday, May 22, 2012
Send mail with Authenticator Object to session property
// http://www.tutorialspoint.com/java/java_sending_email.htm
String host = "smtp.gmail.com";
final String from = "xxxxxx@gmail.com";
final String pass = "xxxxxxx";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String to = "xxxxxx@gmail.com";
// Get the default Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress("testwidgets11@gmail.com"));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
String host = "smtp.gmail.com";
final String from = "xxxxxx@gmail.com";
final String pass = "xxxxxxx";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String to = "xxxxxx@gmail.com";
// Get the default Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress("testwidgets11@gmail.com"));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
Send mail with smtps attached to Transport
String host = "smtp.gmail.com";
String from = "xxxxxx@gmail.com";
String pass = "xxxxx";
System.out.println("Water2");
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
emailId = "mathew";
String[] to = {emailId+"@cxxxx.com"};/*, emailId}; */ // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i=0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("Result of running "+testName);
// message.setText("Check out the attached test report");
// http://www.jguru.com/faq/view.jsp?EID=30251
/* For Body Part */
MimeBodyPart messageBodyPart =
new MimeBodyPart();
messageBodyPart.setText("Check out the attached test report");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
String fileAttachment = System.getProperty("user.dir")+"/test-output/emailable-report.html";
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName("Test-Report");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
/*------*/
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
String from = "xxxxxx@gmail.com";
String pass = "xxxxx";
System.out.println("Water2");
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
emailId = "mathew";
String[] to = {emailId+"@cxxxx.com"};/*, emailId}; */ // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i=0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("Result of running "+testName);
// message.setText("Check out the attached test report");
// http://www.jguru.com/faq/view.jsp?EID=30251
/* For Body Part */
MimeBodyPart messageBodyPart =
new MimeBodyPart();
messageBodyPart.setText("Check out the attached test report");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
String fileAttachment = System.getProperty("user.dir")+"/test-output/emailable-report.html";
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName("Test-Report");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
/*------*/
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Friday, October 28, 2011
Check mails using Java
Instead of automating logging into gmail and checking each mail use the below code to get the contents of required mail
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "xxxxx@gmail.com", "xxxxxx");
System.out.println("Store : "+store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
System.out.println("Messge count "+inbox.getUnreadMessageCount());
Message messages[] = inbox.search(new SubjectTerm("Flags.Flag (JavaMail API documentation)"), inbox.getMessages());
Date date = new Date();
System.out.println("Date "+date);
for (Message message : messages) {
System.out.println("Seen Flag "+message.isSet(Flags.Flag.SEEN));
System.out.println("Messge Send Date "+message.getSentDate());
if(!message.isSet(Flags.Flag.SEEN)){
if(date.compareTo(message.getSentDate())<=0){
String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(message.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
System.out.println(buffer);
}
}
}
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
}
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "xxxxx@gmail.com", "xxxxxx");
System.out.println("Store : "+store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
System.out.println("Messge count "+inbox.getUnreadMessageCount());
Message messages[] = inbox.search(new SubjectTerm("Flags.Flag (JavaMail API documentation)"), inbox.getMessages());
Date date = new Date();
System.out.println("Date "+date);
for (Message message : messages) {
System.out.println("Seen Flag "+message.isSet(Flags.Flag.SEEN));
System.out.println("Messge Send Date "+message.getSentDate());
if(!message.isSet(Flags.Flag.SEEN)){
if(date.compareTo(message.getSentDate())<=0){
String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(message.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
System.out.println(buffer);
}
}
}
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
}
Selenium test report as mail attachment
Consider a situation where an user wants to mail the reports after all the automation tests have been done. Maven has provided with a plugin maven-postman-plugin. It is available in the maven repo.
<plugin>
<groupId>ch.fortysix</groupId>
<artifactId>maven-postman-plugin</artifactId>
<configuration>
<mailhost>smtp.gmail.com</mailhost>
<mailport>465</mailport>
<mailssl>true</mailssl>
<mailAltConfig>true</mailAltConfig>
<mailuser>xxxx@gmail.com</mailuser>
<mailpassword>xxxxxxx</mailpassword>
<from>your_gmail_emailer_account@gmail.com</from>
<receivers>
<receiver>receipient@domain.com</receiver>
</receivers>
<subject>Important subject</subject>
<failonerror>true</failonerror>
<htmlMessage>
<![CDATA[
<p>Hi,</p>
<p>Check out the attached report.</p>
]]>
</htmlMessage>
<fileSets>
<fileSet>
<directory>${basedir}/${tests}</directory>
<includes>
<include>**/emailable-report.html</include>
</includes>
</fileSet>
</fileSets>
</configuration>
</plugin>
The above configuration will send the emailable-report.html as an attachment to all the recipients. If can be configured to send mail at any phase of maven. The following command is used to send mail after the test phase mvn test postman:send-mail.
Will send the mail after the test phase.
<plugin>
<groupId>ch.fortysix</groupId>
<artifactId>maven-postman-plugin</artifactId>
<configuration>
<mailhost>smtp.gmail.com</mailhost>
<mailport>465</mailport>
<mailssl>true</mailssl>
<mailAltConfig>true</mailAltConfig>
<mailuser>xxxx@gmail.com</mailuser>
<mailpassword>xxxxxxx</mailpassword>
<from>your_gmail_emailer_account@gmail.com</from>
<receivers>
<receiver>receipient@domain.com</receiver>
</receivers>
<subject>Important subject</subject>
<failonerror>true</failonerror>
<htmlMessage>
<![CDATA[
<p>Hi,</p>
<p>Check out the attached report.</p>
]]>
</htmlMessage>
<fileSets>
<fileSet>
<directory>${basedir}/${tests}</directory>
<includes>
<include>**/emailable-report.html</include>
</includes>
</fileSet>
</fileSets>
</configuration>
</plugin>
The above configuration will send the emailable-report.html as an attachment to all the recipients. If can be configured to send mail at any phase of maven. The following command is used to send mail after the test phase mvn test postman:send-mail.
Will send the mail after the test phase.
Monday, October 3, 2011
Data providers for selenium
There are different methods to provide data to selenium tests.
One of these is using the properties file. User can store the data in properties file and use its content for the selenium test
Here is a properties file with some content. Name it as datasuite.properties
Directory = C:/prodFiles/
One of these is using the properties file. User can store the data in properties file and use its content for the selenium test
Here is a properties file with some content. Name it as datasuite.properties
Directory = C:/prodFiles/
NumberOfFiles = 25
Extension = javaworld
and the corresponding java code to manipulate it
InputStream is = this.getClass().getResourceAsStream("/conf/datasuite.properties");
Properties prop = new Properties();
prop.load(is);
String directory = prop.getProperty("Directory");
String numberOfFiles = prop.getProperty("NumberOfFiles");
String fileExtension = prop.getProperty("Extension");
is.close();
System.out.println(directory+" "+numberOfFiles+" "+fileExtension);
Below is the folder structure
src/main/java(Source Folder)
>com(Package)
>DataDrivenTest.java
src/main/resource(Source Folder)
>conf(Package)
>datasuite.properties
Subscribe to:
Posts (Atom)