Created
January 2, 2013 12:08
-
-
Save ifsantos/4434131 to your computer and use it in GitHub Desktop.
Mail Client, to test javamail-1.4.5 API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mail.client; | |
import java.util.Enumeration; | |
import java.util.Properties; | |
import javax.mail.Address; | |
import javax.mail.Folder; | |
import javax.mail.Message; | |
import javax.mail.Session; | |
import javax.mail.Store; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JPasswordField; | |
public class TesteMail { | |
public static void main(String[] args) { | |
Properties props = System.getProperties(); | |
String email = JOptionPane.showInputDialog("Email:"); | |
String psswd = getPassword(); | |
props.setProperty("mail.store.protocol", "imaps"); | |
try { | |
Session session = Session.getDefaultInstance(props, null); | |
Store store = session.getStore("imaps"); | |
store.connect("imap.gmail.com", email, psswd); | |
Folder folder = store.getDefaultFolder(); | |
Message[] messages = folder.getMessages(); | |
System.out.println("Messages: " + messages.length); | |
for (Message msg : messages) { | |
System.out.println("===================================="); | |
Address[] from = msg.getFrom(); | |
for (Address address : from) { | |
System.out.println("from: " + address.toString()); | |
} | |
System.out.println("subject: " + msg.getSubject()); | |
@SuppressWarnings("rawtypes") | |
Enumeration allHeaders = msg.getAllHeaders(); | |
while (allHeaders.hasMoreElements()) { | |
System.out.println("headers: " + allHeaders.nextElement()); | |
} | |
} | |
} catch (javax.mail.NoSuchProviderException e) { | |
e.printStackTrace(); | |
} catch (javax.mail.MessagingException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static String getPassword() { | |
JPanel panel = new JPanel(); | |
JLabel label = new JLabel("Senha:"); | |
JPasswordField pass = new JPasswordField(20); | |
panel.add(label); | |
panel.add(pass); | |
String[] options = new String[] { "OK", "Cancel" }; | |
int option = JOptionPane.showOptionDialog(null, panel, "Senha", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); | |
if (option == 0) // pressing OK button | |
{ | |
return new String(pass.getPassword()); | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment