Compilation of already published Articles/Ideas/Problems-Solutions which I faced or came across over the period of time. Largely a place for me to document it as note-to-self. Nothing serious. :)
Wednesday, March 28, 2012
Lenovo Deal Catcher
Historical Mega Millions Numbers -- Lucky Number 36?
People are scrambling to their local gas stations, delis or supermarkets across the country this evening. Why? The Mega Millions lotto is up to $363 million, and the drawing is tonight.
If you want to get serious about which numbers to pick, why not take a look at the historical data?
Below we highlight which numbers have come up the most and least often in the 705 drawings that have occurred since the current Mega Millions format was put in place back in June 2005. Under the current format, players pick any 5 numbers between 1 and 56, and then they pick a final Mega Ball number between 1 and 46. To win the jackpot, you've got to get all five regular numbers and the Mega Ball number correct.
The difference between the numbers that get picked the most and least often might be higher than you think. In the first table, we show how often each number has shown up in the regular ball 1 through 5 drawing. As shown, the number 48 -- the most drawn number -- has come up as ball 1, 2, 3, 4 or 5 in 83 of the 705 drawings, or 11.77% of the time. The number 41 -- the least drawn number -- has only shown up in 48 drawings, or 6.81% of the time.
There are 8 numbers that have come up as ball 1, 2, 3, 4 or 5 in 10% or more of the drawings -- #48, 36, 53, 12, 27, 31, 51 and 52. The five least drawn numbers have been 41, 49, 47, 37 and 34.
So how about the elusive Mega Ball number? As shown below, #36 has been the Mega Ball number the most often at 3.55% of the time. So #36 has shown up the second most in the ball 1 through 5 drawing (10.5%) and the most in the Mega Ball drawing. Somebody check the machine!
Number 28 has been by far the least drawn Mega Ball number at just 0.85% (6 times out of 705 drawings).
So now that you have the historical numbers, which ones do you pick to hit the jackpot? Do you go with the numbers that show up the most often because they're hot, or do you go with the numbers that show up the least often because they're well overdue? Take your pick.
(There's plenty more analysis you could do with the data set, so here is a link to a site that has the historical numbers.)
Java - Sending Email
To send an email using your Java Application is simple enough but to start with you should haveJavaMail API and Java Activation Framework (JAF) installed on your machine.
You can download latest version of JavaMail (Version 1.2) from Java's standard website.
You can download latest version of JAF (Version 1.1.1) from Java's standard website.
Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH.
Send a Simple Email:
Here is an example to send a simple email from your machine. Here it is assumed that yourlocalhost is connected to the internet and capable enough to send an email.
// File Name SendEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail { public static void main(String [] args) { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // 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(); } } } |
Compile and run this program to send a simple email:
$ java SendEmail Sent message successfully.... |
If you want to send an email to multiple recipients then following methods would be used to specify multiple email IDs:
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException |
Here is the description of the parameters:
type: This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. Example Message.RecipientType.TO
addresses: This is the array of email ID. You would need to use InternetAddress() method while specifying email IDs
Send an HTML Email:
Here is an example to send an HTML email from your machine. Here it is assumed that yourlocalhost is connected to the internet and capable enough to send an email.
This example is very similar to previous one, except here we are using setContent() method to set content whose second argument is "text/html" to specify that the HTML content is included in the message.
Using this example, you can send as big as HTML content you like.
// File Name SendHTMLEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendHTMLEmail { public static void main(String [] args) { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // 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!"); // Send the actual HTML message, as big as you like message.setContent(" |
Compile and run this program to send an HTML email:
$ java SendHTMLEmail Sent message successfully.... |
Send Attachment in Email:
Here is an example to send an email with attachment from your machine. Here it is assumed that your localhost is connected to the internet and capable enough to send an email.
// File Name SendFileEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendFileEmail { public static void main(String [] args) { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // 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!"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart ); // Send message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } } |
Compile and run this program to send an HTML email:
$ java SendFileEmail Sent message successfully.... |
User Authentication Part:
If it is required to provide user ID and Password to the email server for authentication purpose then you can set these properties as follows:
props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); |
Rest of the email sending mechanism would remain as explained above.
How do you send email from a Java app using Gmail?
String host = "smtp.gmail.com";
String from = "username";
String pass = "password";
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 = {"to@gmail.com"}; // 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++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();