http://blog.csdn.net/ycg01/article/details/1394465
版权声明:本文为博主原创文章,未经博主允许不得转载。
一.邮件收发.
以gmail为例.(不知道为啥,用同一个帐户给自己收发,或者用两个帐户同时相互收发,会造成邮件收发混乱.估计与连接,和服务器端处理 有关.)正常的收发可以成功.
package cn.edu.nju.vicken;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(String user,String pass)
{
username = user;
password = pass;
}
public PasswordAuthentication performCheck(String user,String pass)
{
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
public class EmailProcessor {
protected String smtp_host = "smtp.gmail.com";
protected String pop3_host = "pop.gmail.com";
protected String pop3_port = "995";
protected String smtp_port = "465";
protected String user = "";
protected String password = "";
private Session mailsessionpop3 = null;
private Session mailsessionsmtp = null;
public EmailProcessor(String user,String password){
this.user = user;
this.password = password;
}
private void createSessionPop3(){
if(mailsessionpop3==null){
Properties props = System.getProperties();
props.put("mail.pop3.host", pop3_host);
props.put("mail.pop3.port", pop3_port);
props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.socketFactory.fallback", "false");
props.put("mail.pop3.socketFactory.port", pop3_port);
PopupAuthenticator popAuthenticator = new PopupAuthenticator(user,password);
mailsessionpop3 = Session.getDefaultInstance(props,popAuthenticator);
}
}
private void createSessionSmtp(){
if(mailsessionsmtp==null){
Properties props = System.getProperties();
props.put("mail.smtp.host", smtp_host);
props.put("mail.smtp.port", smtp_port);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.port", smtp_port);
PopupAuthenticator popAuthenticator = new PopupAuthenticator(user,password);
mailsessionsmtp = Session.getDefaultInstance(props,popAuthenticator);
}
}
public ArrayList getAllEmail() throws Exception{
createSessionPop3();
ArrayList ret = new ArrayList();
Store store = mailsessionpop3.getStore("pop3");
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
ret.add(messages[i].getSubject());
}
return ret;
}
public void sendEmail(String recv,String title,String text,File file) throws Exception{
createSessionSmtp();
MimeMessage mimeMsg =new MimeMessage(mailsessionsmtp);
mimeMsg.setFrom(new InternetAddress(user+"@gmail.com"));
mimeMsg.setRecipients(RecipientType.TO,InternetAddress.parse(recv+"@gmail.com"));
mimeMsg.setSubject(title);
mimeMsg.setSentDate(new Date());
mimeMsg.setText(text);
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(file);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
Multipart mp = new MimeMultipart();;
mp.addBodyPart(bp);
mimeMsg.setContent(mp);
Transport.send(mimeMsg);
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(String user,String pass)
{
username = user;
password = pass;
}
public PasswordAuthentication performCheck(String user,String pass)
{
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
public class EmailProcessor {
protected String smtp_host = "smtp.gmail.com";
protected String pop3_host = "pop.gmail.com";
protected String pop3_port = "995";
protected String smtp_port = "465";
protected String user = "";
protected String password = "";
private Session mailsessionpop3 = null;
private Session mailsessionsmtp = null;
public EmailProcessor(String user,String password){
this.user = user;
this.password = password;
}
private void createSessionPop3(){
if(mailsessionpop3==null){
Properties props = System.getProperties();
props.put("mail.pop3.host", pop3_host);
props.put("mail.pop3.port", pop3_port);
props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.socketFactory.fallback", "false");
props.put("mail.pop3.socketFactory.port", pop3_port);
PopupAuthenticator popAuthenticator = new PopupAuthenticator(user,password);
mailsessionpop3 = Session.getDefaultInstance(props,popAuthenticator);
}
}
private void createSessionSmtp(){
if(mailsessionsmtp==null){
Properties props = System.getProperties();
props.put("mail.smtp.host", smtp_host);
props.put("mail.smtp.port", smtp_port);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.port", smtp_port);
PopupAuthenticator popAuthenticator = new PopupAuthenticator(user,password);
mailsessionsmtp = Session.getDefaultInstance(props,popAuthenticator);
}
}
public ArrayList getAllEmail() throws Exception{
createSessionPop3();
ArrayList ret = new ArrayList();
Store store = mailsessionpop3.getStore("pop3");
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
ret.add(messages[i].getSubject());
}
return ret;
}
public void sendEmail(String recv,String title,String text,File file) throws Exception{
createSessionSmtp();
MimeMessage mimeMsg =new MimeMessage(mailsessionsmtp);
mimeMsg.setFrom(new InternetAddress(user+"@gmail.com"));
mimeMsg.setRecipients(RecipientType.TO,InternetAddress.parse(recv+"@gmail.com"));
mimeMsg.setSubject(title);
mimeMsg.setSentDate(new Date());
mimeMsg.setText(text);
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(file);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
Multipart mp = new MimeMultipart();;
mp.addBodyPart(bp);
mimeMsg.setContent(mp);
Transport.send(mimeMsg);
}
}