JavaMail Gmail 收发邮件

因为gmail 需要用到ssl 安全方面的东西所以一般方法并不管用,需要特殊处理

 

 

一:发邮件

 

package mail; import java.security.Security; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class T3 { public static void main(String[] args) throws AddressException, MessagingException { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // Get a Properties object Properties props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); final String username = "jidfwiuf"; final String password = "yourPassword"; Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); // -- Create a new message -- Message msg = new MimeMessage(session); // -- Set the FROM and TO fields -- msg.setFrom(new InternetAddress(username + "@gmail.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse( "[email protected]", false)); msg.setSubject("Hello"); msg.setText("How are you"); msg.setSentDate(new Date()); Transport.send(msg); System.out.println("Message sent."); } }

 

 

 

 

public static void main(String argv[]) throws Exception { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // Get a Properties object Properties props = System.getProperties(); props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); props.setProperty("mail.pop3.socketFactory.fallback", "false"); props.setProperty("mail.pop3.port", "995"); props.setProperty("mail.pop3.socketFactory.port", "995"); //以下步骤跟一般的JavaMail操作相同 Session session = Session.getDefaultInstance(props,null); //请将红色部分对应替换成你的邮箱帐号和密码 URLName urln = new URLName("pop3","pop.gmail.com",995,null, "[邮箱帐号]", "[邮箱密码]"); Store store = session.getStore(urln); Folder inbox = null; try { store.connect(); inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); Message[] messages = inbox.getMessages(); inbox.fetch(messages, profile); System.out.println("收件箱的邮件数:" + messages.length); for (int i = 0; i < messages.length; i++) { //邮件发送者 String from = decodeText(messages[i].getFrom()[0].toString()); InternetAddress ia = new InternetAddress(from); System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')'); //邮件标题 System.out.println("TITLE:" + messages[i].getSubject()); //邮件大小 System.out.println("SIZE:" + messages[i].getSize()); //邮件发送时间 System.out.println("DATE:" + messages[i].getSentDate()); } } finally { try { inbox.close(false); } catch (Exception e) {} try { store.close(); } catch (Exception e) {} } } protected static String decodeText(String text) throws UnsupportedEncodingException { if (text == null) return null; if (text.startsWith("=?GB") || text.startsWith("=?gb")) text = MimeUtility.decodeText(text); else text = new String(text.getBytes("ISO8859_1")); return text; } }

http://download.csdn.net/source/2673139   源代码 ,在以上代码的基础上对发送过程进行了简单的封装,

 

你可能感兴趣的:(exception,String,properties,ssl,Gmail,javamail)