Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
The following code allow you to download the email content using for example POP protocol.You can change the protocol your are using by changing the properties values your are using.If you want this code in your project , will just need to remove small things for example (GUILogger ). This code allow you to read the mail even if you have multi-parts (like attachments ,images , and text ) .
Note : You must have the Java mail library in your build path .
session = Session.getInstance(properties, null); int port = Integer.parseInt(properties.getProperty("port")); URLName urln = new URLName(properties.getProperty("protocal"), properties.getProperty("host"), port, null, username, password); gUILogger.logInfo("Started connecting to mail inbox ... "); store = new POP3SSLStore(session, urln);
} else { JOptionPane.showMessageDialog(null, " No properties loaded- Mail Connector Failed .", "Mail client Error", JOptionPane.ERROR_MESSAGE); gUILogger.logInfo("No properties loaded- Mail Connector Failed"); }
}
public Message[] readAllMessages() {
if (properties == null) { gUILogger.logInfo("No properties loaded- Mail Connector Failed"); return null; } try { store.connect(properties.getProperty("host"), username, password); } catch (MessagingException ex) { JOptionPane.showMessageDialog(null, " couldn't connect to mail server ,please make sure you entered to right(username,password). Or make sure of your mail client connect configuration under props/config.properties file.", "Mail client Error", JOptionPane.ERROR_MESSAGE); gUILogger.logInfo("Mail Connector Failed -" + ex);
your ID is : xxxxxx Connecting... Connected... java.lang.IllegalStateException: already connected at javax.mail.Service.connect(Service.java:198) at javax.mail.Service.connect(Service.java:156) at com.ReadInbox.readAllMessages(ReadInbox.java:91) at com.ReadInbox.refresh(ReadInbox.java:127) at com.Main.main(Main.java:17)
nithinj3
Code subject: Re: Read your gmail using Java code
Posted: Fri Mar 16, 2012 3:23 am
please add the gui code also i need to check the my mail through java in first time. i run my program but i got error in the program. please help me anybody and send me a solution. following my codes and error.
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
@Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username of authentication mail account", "password of authentication mail account");
} });
MimeMessage message = new MimeMessage(session); message.setSender(new InternetAddress(sender)); message.setSubject(subject); message.setContent(body, "text/plain"); if (recipients.indexOf(',') > 0) { message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); } else { message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); }
Transport.send(message);
}
public static void main(String args[]) throws Exception { SimpleMail mailutils = new SimpleMail(); mailutils.sendMail("test", "helo test from kites", "nithinj3@gmail.com", "nithinj3@gmail.com");
} }
my error is
Exception in thread "main" javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com; nested exception is: java.net.UnknownHostException: smtp.gmail.com at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1211) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311) at javax.mail.Service.connect(Service.java:255) at javax.mail.Service.connect(Service.java:134) at javax.mail.Service.connect(Service.java:86) at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144) at javax.mail.Transport.send0(Transport.java:150) at javax.mail.Transport.send(Transport.java:80) at com.kitesnet.database.SimpleMail.sendMail(SimpleMail.java:54) at com.kitesnet.database.SimpleMail.main(SimpleMail.java:60) Java Result: 1
i hope any one will help me.
msi_333
Code subject: Re: Read your gmail using Java code
Posted: Fri Mar 16, 2012 10:17 am
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
Actually i think the problem in the host name , i have used POP3 protocal ,as i remember SMTP is used for sending emails use these :