public static void main(String[] args) throws Exception { SendMail send = new SendMail(); send.sendMail(); }
public void sendMail() throws Exception { Properties prop = new Properties(); prop.setProperty("mail.smtp.host","smtp.163.com"); prop.setProperty("mail.smtp.auth","true"); MyAuthenticator auth = new MyAuthenticator(); Sessionsession=Session.getDefaultInstance(prop,auth);
MimeMessage message = new MimeMessage(session); Address from = new InternetAddress("hlq@163.com"); Address to = new InternetAddress("hlq@163.com"); message.setSubject("Common Send"); message.setText("just common text"); message.setFrom(from); message.setRecipient(Message.RecipientType.TO,to); message.saveChanges();
Transport trans =session.getTransport("smtp"); trans.connect("smtp.163.com","hlq_1211","123456"); trans.send(message); trans.close(); System.out.println("send ok!"); }
class MyAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("hlq","123456"); } } }
public static void main(String[] args) throws Exception { ReceiveMail receivemail = new ReceiveMail(); receivemail.receive(); }
public void receive() throws Exception { Properties prop = new Properties(); prop.setProperty("mail.pop3.host","127.0.0.1"); prop.setProperty("mail.pop3.auth","true");
MyAuthenticator auth = new MyAuthenticator(); Sessionsession=Session.getDefaultInstance(prop, auth);
Store store =session.getStore("pop3"); store.connect("127.0.0.1","pqds@accp.com","000000"); Folder defaultFolder = store.getDefaultFolder(); Folder folder = defaultFolder.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message[] message = folder.getMessages(); for(int i= 0;i< message.length;i++){ System.out.println( "---------------------------------------------------------------"); Message msg = message[i]; System.out.println("邮件标题:"+ msg.getSubject()); System.out.println("邮件征文:"+ msg.getContent()); if (msg.getContent() instanceof Multipart){ Multipart mp =(Multipart) msg.getContent(); for(int t = 0; t < mp.getCount(); t++){ BodyPart part = mp.getBodyPart(t); String fileName = part.getFileName(); if(fileName==null) { System.out.println(part.getContent()); }else { InputStream in=part.getInputStream(); byte[] date = new byte[in.available()]; in.read(date); FileOutputStream out = new FileOutputStream("c://"+fileName); out.write(date); System.out.println("文件 "+fileName +"保存在c://"); } }
}
} }
public class MyAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("pqds","000000"); } }
public class SendMailAddFile { public SendMailAddFile(){ }
public static void main(String[] args) throws Exception { SendMailAddFile send = new SendMailAddFile(); send.sendFile(); }
public void sendFile() throws Exception { Properties prop = new Properties(); prop.setProperty("mail.smtp.host","127.0.0.1"); // prop.setProperty("mail.smtp.auth","true"); MyAuthenticator authenticator = new MyAuthenticator(); Sessionsession=Session.getInstance(prop,authenticator);
MimeMessage message = new MimeMessage(session); Address from = new InternetAddress("pqds@accp.com"); Address to = new InternetAddress("pqds@accp.com"); message.setFrom(from); message.setRecipient(Message.RecipientType.TO,to); message.setSubject("addition File");
Multipart mpart = new MimeMultipart();
MimeBodyPart body= new MimeBodyPart(); body.setText("this is addition file");
mpart.addBodyPart(body);
body= new MimeBodyPart(); DataSource ds = new FileDataSource("c://1.jpg"); DataHandler dh = new DataHandler(ds); body.setDataHandler(dh); body.setFileName("readme.jpg"); mpart.addBodyPart(body);
message.setContent(mpart);
Transport trans =session.getTransport("smtp"); trans.connect("127.0.0.1","pqds","000000"); trans.send(message); trans.close(); System.out.println("send ok!"); }
public class MyAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("pqds","000000"); } }