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();
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
How do you send email from a Java app using Gmail?
How to open a URL in the default browser
If you ever wondered if there is an easy way to open a URL from your Java program in the user-default browser, with Java SE 6 there is. Unless your platform doesn't support this. The good news is that you can test if this is the case or not.
In Java SE 6 a new class has been added to the java.awt package: Desktop. This class has a public class method isDesktopSupported() which returns true if the class is supported on the current platform, and false otherwise.
If the class is supported, you can retrieve an instance using the class method getDesktop().
You can use the browse() method to display a URI in the default browser. If this browser is not able to handle the specified URI, the application registered for handling the URIs of the specified type is invoked. For example, the URI news:comp.lang.perl.misc might invoke a Usenet client.
However, before you call the browse() method make sure to check if this action is supported on the current platform by calling the isSupported()method with java.awt.Desktop.Action.BROWSE as the value of the first and only parameter.
Note that even though this method returns true, it's still possible that the platform doesn't support the URI - for example if there is no application registered for the given scheme. In this case the browse() method will throw an IOException.
Example Java program using the browse method
Below follows a small example program that can be invoked from the command line. It attempts to open each argument as a URI in the user-default browser, or if the browser can't handle the URI, an attempt is made to pass the URI to the application registered for the URI's scheme.
import java.net.URI; import java.awt.Desktop; public class OpenURI { public static void main(String [] args) { if( !java.awt.Desktop.isDesktopSupported() ) { System.err.println( "Desktop is not supported (fatal)" ); System.exit( 1 ); } if ( args.length == 0 ) { System.out.println( "Usage: OpenURI [URI [URI ... ]]" ); System.exit( 0 ); } java.awt.Desktop desktop = java.awt.Desktop.getDesktop(); if( !desktop.isSupported( java.awt.Desktop.Action.BROWSE ) ) { System.err.println( "Desktop doesn't support the browse action (fatal)" ); System.exit( 1 ); } for ( String arg : args ) { try { java.net.URI uri = new java.net.URI( arg ); desktop.browse( uri ); } catch ( Exception e ) { System.err.println( e.getMessage() ); } } } }But there are more actions
Besides the browse() method there are 5 more methods, providing support for the 4 following Desktop actions:
- EDIT
- The edit() method launches the associated editor application and opens a file for editing.
- There are two mail() methods. The first one doesn't take any parameters and launches the mail composing window of the user-default mail client. The second method takes a single parameter: a "mailto" URI. It also launces the mail composing window of the user-default mail client, but additionally fills in the message fields specified by the "mailto" URI (for example, to, subject, etc.).
- OPEN
- The open() method launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.
- Prints a file with the native desktop printing facility, using the associated application's print command.
Java 1.6 Desktop class related
- Desktop - Class documentation, Java Platform SE 6
- Desktop.Action - Enum documentation, Java Platform SE6
- Using the Desktop API in Java SE 6
Using the Desktop API in Java SE 6
With the default graphical user interface (GUI) look and feel, printing, and performance, the Java platform has come a long way in its effort to minimize the difference between the performance and integration of native applications and Java applications. Version 6 of the Java Platform, Standard Edition (Java SE), continues to narrow the gap with new system tray functionality, better print support for Desktop Overview This new functionality is provided by the
The Desktop API uses your host operating system's file associations to launch applications associated with specific file types. For example, if OpenDocument text ( Running the DesktopDemo Application DesktopDemo is a simple Java application that uses the new Desktop API. The application has one main window that exposes the entire API, allowing you to do three things:
Figure 1 shows the user interface (UI).
You can run this application by downloading the application source and
Determining Desktop API Support Before launching the browser, email client, or any application, DesktopDemo must determine whether your platform supports the API. First, however, DesktopDemo disables all the graphical text fields and buttons. It enables these graphical components after determining that the platform supports them. After instantiating the UI, the application's constructor quickly disables the few components of this application as shown in the following code:
Use the
If your application doesn't check for API support using Once retrieved, the
Before invoking any of these actions, an application should determine whether the
Using a new
Once the application determines the supported actions, it enables the appropriate graphical components. If all components are enabled, the UI should look like Figure 3.
Opening the Browser Calling the following instance method will open your host's default browser:
Because DesktopDemo's UI components are enabled only if the associated
DesktopDemo adds a
The If all goes well, however, the listener retrieves the text from the associated text field in Figure 4, creates a URI, and invokes the
Sending Email Applications can launch the host's default email client, if that action is supported, by calling this
DesktopDemo has an
The
You can use more than just a single email recipient when creating this URI. The
Figure 7 shows the result.
You can, of course, invoke Opening, Editing, and Printing a File Java applications can open, edit, and print files from their associated application using a
Each of DesktopDemo's radio buttons has its own
When you press the Launch Default Application button, it invokes its own listener, which calls the following method:
This method determines which Interestingly, different applications may be registered for these different actions even on the same file type. For example, the Firefox browser may be launched for the Summary Desktop integration is an important theme. One way that Java SE 6 technology supports this theme is through the Note: Any API additions or other enhancements to the Java SE platform specification are subject to review and approval by theJSR 270 Expert Group. For More Information The following information may help you learn more about Java SE 6's integration features as well as related topics: |
Monday, March 26, 2012
| Topic | Description |
|---|---|
| Windows Internet Explorer 8 provides enhanced Developer Tools to help you research and resolve HTML, Cascading Style Sheets (CSS), and JavaScript related problems. This article is focused on the HTML and CSS tools of the Developer Tools. For information about debugging HTML and CSS code using F12 developer tools in Windows Internet Explorer 9 see Using F12 Developer Tools to Debug HTML and CSS. | |
| The Developer Tools feature of Internet Explorer 8 offers a built-in, lightweight Microsoft JScript debugger that enables developers to set breakpoints and to step through client-side JScript code without leaving the browser. This document outlines the high-level features of the JScript debugger. | |
| F12 tools help you to quickly research and resolve HTML, CSS, and JavaScript related problems. This article provides a list of shortcut keys for F12 tools that will help you accomplish these tasks even faster. The keys are listed below by feature set. A comprehensive list of all shortcut keys is also provided for easy lookup and reference. | |
| The Developer Tools tutorials will show you how to use the Developer Tools to inspect and resolve webpage issues. To establish a context for these scenarios, a starting webpage is provided. Each tutorial is focused on resolving a specific issue in order to make this webpage better, and is designed to provide a simple and safe environment for experimenting with the many features of the Developer Tools. For more on F12 tools in Internet Explorer 9 see How to use F12 Developer Tools to Debug your Webpages. | |
| This article is designed to give you a quick reference to the commands and menus available in the Developer Tools interface commands. The goal is to help you quickly identify main areas of the interface and to quickly find your way around, ultimately improving your productivity. Each element of the interface is identified and a short description of what it does is provided. For more on F12 tools in Internet Explorer 9 see How to use F12 Developer Tools to Debug your Webpages. | |
| Every installation of Internet Explorer 8 comes with the Developer Tools. This tool enables Web site developers to quickly debug JScript, investigate a behavior specific to Windows Internet Explorer, or iterate rapidly to prototype a new design or try solutions to a problem on-the-fly. This article introduces you to key features of the Developer Tools in Internet Explorer 8. For F12 tools in Internet Explorer 9 see How to use F12 Developer Tools to Debug your Webpages. | |
| The Internet Explorer 8 Developer Tools provide a built-in script profiler that enables you to profile the JavaScript code running in Internet Explorer. This document outlines the high-level features of the script Profiler. For information about the F12 tools Profiler tab in Internet Explorer 9 see Using the Profiler Tool to Analyze the Performance of your Code. | |
| The Network tab in the Internet Explorer 9 Developer Tools can help you diagnose network-related issues by showing all the traffic that is related to a page and exposing details about individual connections. You can see the relative timing that each item on a webpage takes to load and render, so you can quickly see and solve problems. For more on F12 tools in Internet Explorer 9 see How to use F12 Developer Tools to Debug your Webpages. |
Developer Tools Keyboard Shortcuts Reference
[This documentation is preliminary and is subject to change.]
The F12 developer tools help you to quickly research and resolve HTML, Cascading Style Sheets (CSS), and JavaScript related problems. This article provides a list of shortcut keys for F12 tools that will help you accomplish these tasks even faster. The keys are listed below by feature set. A comprehensive list of all shortcut keys is also provided for easy lookup and reference.
- Developer Tools Menu Bar Shortcut Keys
- Jump-To Tab Shortcut Keys
- HTML Tab Toolbar Shortcut Keys
- CSS Tab Toolbar Shortcut Keys
- Console Tab Shortcut Keys For Debugger Mode
- Script Tab Shortcut Keys For Debugger Mode
- Profiler Tab Toolbar Shortcut Keys
- Network Tab Toolbar Shortcut Keys
- Search Shortcut Keys
- Related topics
Developer Tools Menu Bar Shortcut Keys
The following shortcut keys can be accessed at any time while the F12 tools are open. Differences between Windows Internet Explorer 8 and Windows Internet Explorer 9 are noted. For more on using F12 tools in Internet Explorer 9 see How to use F12 Developer Tools to Debug your Webpages
| Action | Shortcut Keys |
|---|---|
| Open/Close Developer Tools | F12 |
| Resize to 800x600 | Ctrl+Shift+1 |
| Resize to 1024x768 | Ctrl+Shift+2 |
| Resize to 1280x768 | Ctrl+Shift+3 |
| Resize to 1280x1024 | Ctrl+Shift+4 |
| Switch to Quirks mode | Alt+Q |
| Switch to Windows Internet Explorer 7 Standards mode | Alt+7 |
| Switch to Internet Explorer 8 Standards mode | Alt+8 |
| Switch to Internet Explorer 9 Standards mode (Internet Explorer 9 only) | Alt+9 |
| Switch to (page default) mode | Alt+D |
| Clear Browser Cache | Ctrl+R |
| Clear Browser Cache for This Domain | Ctrl+D |
| Select Element by Click | Ctrl+B |
| View Class and ID Information | Ctrl+I |
| View DOM (Element+Styles) | Ctrl+T |
| View DOM (Page) (Internet Explorer 8 only) | Ctrl+G |
| Show/Hide Color Picker (Toggle) | Ctrl+K |
| Show/Hide Ruler (Toggle) | Ctrl+L |
| Minimize/Maximize (Toggle) (Internet Explorer 8 only) | Ctrl+M |
| Outline Any Element | Ctrl+O |
| Clear Outlines (Internet Explorer 8 only) | Ctrl+Shift+O |
| Pin/Unpin (Toggle) | Ctrl+P |
| Help (Internet Explorer 9 only) | F1 |
Jump-To Tab Shortcut Keys
The following shortcut keys can be accessed at any time while the F12 tools are open. For more on using tabs in F12 tools in Introduction to F12 Developer Tools
| Action | Internet Explorer 8 Shortcut Keys | Internet Explorer 9 Shortcut Keys |
|---|---|---|
| Select HTML Tab | Ctrl+1 | Ctrl+1 |
| Select CSS Tab | Ctrl+2 | Ctrl+2 |
| Select Console Tab (Internet Explorer 9 only) | n/a | Ctrl+3 |
| Select Script Tab | Ctrl+3 | Ctrl+4 |
| Select Profiler Tab | Ctrl+4 | Ctrl+5 |
| Select Network Tab (Internet Explorer 9 only) | n/a | Ctrl+6 |
HTML Tab Toolbar Shortcut Keys
The following shortcut keys can be accessed under the HTML tab. For more on debugging HTML in F12 tools, see Using F12 Developer Tools to Debug HTML and CSS
| Action | Shortcut Keys |
|---|---|
| Make value editable | F2 |
| Refresh | F5 |
| Select Element by Click | Ctrl+B |
| Edit Mode | Alt+E |
| Clear Browser Cache | Ctrl+R |
| Save | Ctrl+S |
| View element source with style | Ctrl+T |
| Add Attribute | Ctrl+Plus Sign |
| Expand all beneath this three node. This only works with the number's keypad symbol keys. | Alt+* |
| Collapse all beneath this three node. This only works with the number's keypad symbol keys. | Alt+- |
CSS Tab Toolbar Shortcut Keys
The following shortcut keys can be accessed under the CSS tab. Differences between Internet Explorer 8 and Internet Explorer 9 are noted. For more on debugging CSS in F12 tools, see Using F12 Developer Tools to Debug HTML and CSS.
| Action | Shortcut Keys |
|---|---|
| Make value editable | F2 |
| Select Element by Click | Ctrl+B |
| File chooser (Internet Explorer 8 only) | Ctrl+F |
| Clear Browser Cache | Ctrl+R |
| Save | Ctrl+S |
| Add CSS property while focus is on a property | Ctrl+Plus Sign |
| Expand all beneath this three node. This only works with the number's keypad symbol keys. | Alt+* |
| Collapse all beneath this three node. This only works with the number's keypad symbol keys. | Alt+- |
| Increment selected value (Internet Explorer 9 only) | Up arrow |
| Decrement selected value (Internet Explorer 9 only) | Down arrow |
Console Tab Shortcut Keys For Debugger Mode
The following shortcut keys work under the Console tab. This tab is only available in Internet Explorer 9. For more on using the F12 tools Console tab, see How to use F12 Developer Tools to Debug your Webpages.
| Action | Shortcut Keys |
|---|---|
| Clear browser cache | Ctrl+R |
| Clear browser cache for this domain | Ctrl+D |
| Run script (Single line mode) | Return |
| Run script (Multi-line mode) | Ctrl+Return |
| Toggle between single and multi-line mode | Ctrl+Alt+M |
Script Tab Shortcut Keys For Debugger Mode
The following shortcut keys work under the Script tab. To work, script debugging does not have to start for these shortcut keys. However, some debugging features are disabled when debugging is not started, and pressing these shortcut keys will yield no results. For more on using the F12 tools Script tab, see Using the F12 Developer Tools to Debug JavaScript Errors.
| Action | Shortcut Keys |
|---|---|
| Start debugging or continue | F5 |
| Stop debugging | Shift+F5 |
| Step over | F10 |
| Step into | F11 |
| Step out | Shift+F11 |
| Break all | Ctrl+Shift+B |
| Break on Error - toggle on/off | Ctrl+Shift+E |
| Toggle Breakpoints - set/unset | F9 |
| Set breakpoint Condition | Alt+F9 |
| Enable/Disable breakpoints | Ctrl+F9 |
| Delete all breakpoints | Ctrl+Shift+F9 |
| Go to Breakpoint window | Ctrl+Alt+B |
| Go to Locals window | Ctrl+Alt+L (or V) |
| Go to Watch window | Ctrl+Alt+W |
| Go to Call Stack window | Ctrl+Alt+C |
| Go to Console window | Ctrl+Alt+I |
| Console - Run script (in single-line mode) | Return |
| Console - Run script (in multi-line mode) | Ctrl+Return |
| Console - toggle between single-line and multi-line mode | Ctrl+Alt+M |
| Go to Script chooser dropdown | Ctrl+Alt+N |
| Next function on Stack | Ctrl+Period |
| Previous function on Stack | Ctrl+Comma |
| Go to specific line number (Internet Explorer 9 only) | Ctrl+G |
Profiler Tab Toolbar Shortcut Keys
The following shortcut keys work under the Profiler tab. For more on using the F12 tools Profiler tab, see Using the Profiler Tool to Analyze the Performance of your Code.
| Action | Shortcut Keys |
|---|---|
| Start Profiling | F5 |
| Stop Profiling | Shift+F5 |
Network Tab Toolbar Shortcut Keys
The following shortcut keys work under the Network tab. For more on using the F12 tools Network tab, see Using Internet Explorer Developer Tools Network Capture.
| Action | Shortcut Keys |
|---|---|
| Start Capturing | F5 |
| Stop Capturing | Shift+F5 |
Search Shortcut Keys
The following shortcut keys can be accessed to do Search. For more on using the F12 tools Search, see Introduction to F12 Developer Tools.
| Action | Shortcut Keys |
|---|---|
| Move focus to Search Box | Ctrl+E |
| Next match | F3 |
| Previous match | Shift+F3 |
Related topics
- How to use F12 Developer Tools to Debug your Webpages
- Using the F12 Developer Tools to Debug JavaScript Errors
- Using F12 Developer Tools to Debug HTML and CSS
- Using the Profiler Tool to Analyze the Performance of your Code
- Introduction to F12 Developer Tools
- Using Internet Explorer Developer Tools Network Capture
Build date: 2/7/2012




