class="java">package org.lazyzhong.test; import java.util.Properties; public class MailSenderInfo { //发送邮件的服务器的ip和端口 private String mailServerHost; private String mailServerPort; //用户名和密码 private String username; private String password; //主题和内容 private String subject; private String content; //发送和接收者 private String fromAddress; private String toAddress; //是否需要验证 private boolean validate; //附件名 private String[] attachFileNames; public Properties getProperties(){ Properties p=new Properties(); p.put("mail.smtp.host",this.mailServerHost); p.put("mail.smtp.port",this.mailServerPort); p.put("mail.smtp.auth",validate?"true":"false"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] attachFileNames) { this.attachFileNames = attachFileNames; } public MailSenderInfo() { super(); } }
?
package org.lazyzhong.test; import javax.mail.*; public class MyAuthenticator extends Authenticator{ String userName=null; String password=null; public MyAuthenticator(){ } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName, password); } }
?
package org.lazyzhong.test; import java.util.Properties; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Message.RecipientType; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class SimpleMailSender { //简单文本 public boolean sendText(MailSenderInfo mailInfo) throws Exception{ MyAuthenticator authen=null; Properties p=mailInfo.getProperties(); if(mailInfo.isValidate()){ authen=new MyAuthenticator(mailInfo.getUsername(),mailInfo.getPassword()); } //根据权限判断和配置信息生成会话 Session session=Session.getDefaultInstance(p,authen); //根据会话生成消息 Message message=new MimeMessage(session); Address fromAddress=new InternetAddress(mailInfo.getFromAddress()); Address toAddress=new InternetAddress(mailInfo.getToAddress()); message.setFrom(fromAddress); message.setRecipient(RecipientType.TO,toAddress); message.setSubject(mailInfo.getSubject()); message.setSentDate(new Date()); message.setText(mailInfo.getContent()); Transport.send(message); return true; } //html文本 public boolean sendHtmlText(MailSenderInfo mailInfo) throws Exception{ MyAuthenticator authen=null; Properties p=mailInfo.getProperties(); if(mailInfo.isValidate()){ authen=new MyAuthenticator(mailInfo.getUsername(),mailInfo.getPassword()); } //根据权限判断和配置信息生成会话 Session session=Session.getDefaultInstance(p,authen); //根据会话生成消息 Message message=new MimeMessage(session); //发送人的名字可以设置可以不设置...不设置的情况下显示的是邮箱名,即邮件地址截取掉@那部分... Address fromAddress=new InternetAddress(mailInfo.getFromAddress(),"红尘眷恋"); Address toAddress=new InternetAddress(mailInfo.getToAddress()); message.setFrom(fromAddress); message.setRecipient(RecipientType.TO,toAddress); message.setSubject(mailInfo.getSubject()); message.setSentDate(new Date()); // 一个Multipart能包含多个BodyPart Multipart mainPart=new MimeMultipart(); BodyPart body=new MimeBodyPart(); body.setContent(mailInfo.getContent(),"text/html; charset=utf-8"); mainPart.addBodyPart(body); message.setContent(mainPart); Transport.send(message); return true; } /* 当发送多个消息时,建议采用这种方式... * 因为它将保持消息间活动服务器的连接 * message.saveChanges(); // implicit with send() Transport transport = session.getTransport("smtp"); transport.connect(host, username, password); transport.sendMessage(message, message.getAllRecipients()); transport.close();*/ } import javax.mail.Folder; import javax.mail.Message; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Store; import javax.mail.search.AndTerm; import javax.mail.search.FromStringTerm; import javax.mail.search.SearchTerm; import javax.mail.search.SubjectTerm; public class SimpleMailGet { public void getMessage(MailSenderInfo mailInfo) throws Exception{ // Properties props = new Properties(); // props.setProperty("mail.store.protocol","pop3"); // props.setProperty("mail.pop3.host","pop3.163.com"); // Session session = Session.getInstance(props); // // 利用Session对象获得Store对象,并连接pop3服务器 // Store store = session.getStore(); // store.connect("pop3.163.com",110,"xxxxxx@163.com","yyyyyyy"); Properties p=new Properties(); Session session=Session.getDefaultInstance(p,null); Store store=session.getStore("pop3"); store.connect(mailInfo.getMailServerHost(),Integer.parseInt(mailInfo.getMailServerPort()),mailInfo.getUsername(),mailInfo.getPassword()); Folder folder=store.getFolder("INBOX"); /* num1 folder.open(Folder.READ_ONLY); //获取所有。。。 Message message[]=folder.getMessages(); //根据邮件id获取 Message mess=folder.getMessage(35); for(int i=0;i<message.length;i++){ System.out.println(i+" "+message[i].getFrom()[0]+" "+message[i].getSubject()+" "+" "+message[i].getMessageNumber()); } System.out.println(mess.getFrom()[0]);*/ /* * 通过修改标记来改变邮件删除、已阅等状态...pop3是只读的》。。 * folder.open(Folder.READ_WRITE); Message mess=folder.getMessage(35); mess.setFlag(Flags.Flag.DELETED,true); mess.saveChanges(); folder.expunge();*/ folder.open(Folder.READ_ONLY); SearchTerm st = new FromStringTerm("lazy_zhong@163.com"); Message[] message = folder.search(st); for(int i=0;i<message.length;i++){ System.out.println(i+" "+message[i].getFrom()[0]+" "+message[i].getSubject()+" "+" "+message[i].getMessageNumber()); } folder.close(false); store.close(); } } package org.lazyzhong.test;<br> public class MyTest {<br> public static void main(String[] args) throws Exception{<br> //不同的邮箱有不同的主机名和端口号...<br> ////这个类主要是设置邮件<br> // MailSenderInfo mailInfo = new MailSenderInfo(); <br> //mailInfo.setMailServerHost("smtp.163.com"); <br> // mailInfo.setMailServerPort("25"); <br> //mailInfo.setValidate(true); <br> // mailInfo.setUsername("xxxxxxxx@163.com"); <br> // mailInfo.setPassword("yyyyyyyyyyyy");//您的邮箱密码 <br> //mailInfo.setFromAddress("aaaaaaaa@163.com"); <br> //mailInfo.setToAddress("bbbbbbbbb@163.com"); <br> // mailInfo.setSubject("设置邮箱标题"); <br> //mailInfo.setContent("<div style='color:red;font-size:25px;'>春天在哪里啊春天在哪里...</div>"); <br> ////这个类主要来发送邮件<br> // SimpleMailSender sms = new SimpleMailSender();<br> ////sms.sendText(mailInfo);//发送文体格式 <br> //sms.sendHtmlText(mailInfo);//发送邮件格式<br> //获取邮件信息 MailSenderInfo mailInfo = new MailSenderInfo(); <br> mailInfo.setMailServerHost("pop3.163.com");<br> mailInfo.setMailServerPort("110"); <br> mailInfo.setUsername("xxxxxxxxxxxx@163.com");<br> mailInfo.setPassword("yyyyyy");<br> SimpleMailGet smg=new SimpleMailGet();<br> smg.getMessage(mailInfo);<br> }<br> }
?