1.发邮件:
package cn.itwoody.mail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailExample {
? public static void main (String args[]) throws Exception {
?? ?
??? String host = "smtp.sina.com";?? //发件人使用发邮件的电子信箱服务器
??? String from = "
[email protected]";??? //发邮件的出发地(发件人的信箱)
??? String to = "
[email protected]";?? //发邮件的目的地(收件人信箱)
??? // Get system properties
??? Properties props = System.getProperties();
??? // Setup mail server
??? props.put("mail.smtp.host", host);
??? // Get session
??? props.put("mail.smtp.auth", "true"); //这样才能通过验证
??? MyAuthenticator myauth = new MyAuthenticator("
[email protected]", "password");
??? Session session = Session.getDefaultInstance(props, myauth);
??? //session.setDebug(true);
??? // Define message
??? MimeMessage message = new MimeMessage(session);
???
??? // Set the from address
??? message.setFrom(new InternetAddress(from,"你的昵称"));
??? // Set the to address
??? message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
??? // Set the subject
??? message.setSubject("测试程序!");
??? // Set the content(文本格式)
??? message.setText("这是用java写的发送电子邮件的测试程序!");
???
??? // Set the content(非文本格式)
??? // message.setContent("Hello", "text/plain");
??? message.saveChanges();
?
??? Transport.send(message);
???
? }
}?
package cn.itwoody.mail;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends javax.mail.Authenticator {
??? private String strUser;
??? private String strPwd;
??? public MyAuthenticator(String user, String password) {
????? this.strUser = user;
????? this.strPwd = password;
??? }
??? protected PasswordAuthentication getPasswordAuthentication() {
????? return new PasswordAuthentication(strUser, strPwd);
??? }
}
2.收邮件
package cn.itwoody.mail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class ReceiveMailExample {
???
??? public static void main(String[] args) throws MessagingException, IOException {
??? ??? // TODO Auto-generated method stub
??? ??? String host="pop.sina.com";
??? ???
??? ??? //Get system properties
??? ??? Properties props = System.getProperties();
??? ???
??? ??? Session session = Session.getDefaultInstance(props, null);
??? ???
??? ??? Store store=session.getStore("pop3");
??? ???
??? ??? store.connect(host, "
[email protected]","password");
??? ???
??? ??? Folder folder=store.getFolder("INBOX");
??? ???
??? ??? //open by read
??? ??? folder.open(Folder.READ_ONLY);
??? ???
??? ??? Message messages[]=folder.getMessages();
??? ???
??? ??? for(int i=0;i
??? ??? ??? System.out.println("sender:"+messages[i].getFrom()[0]+" - subject:"+messages[i].getSubject());
??? ??? ??? BufferedReader reader = new BufferedReader(new InputStreamReader(messages[i].getInputStream()));
??? ??? ??? String newLine="";
??? ??? ??? while((newLine=reader.readLine())!=null){
??? ??? ??? ??? System.out.println(newLine);
??? ??? ??? }
??? ??? ??? System.out.println("----------------------------------------------------------------------");
??? ??? }
??? ???
??? }
}
六、常见异常
com.sun.mail.smtp.SMTPSendFailedException:?553?authentication?is?required,smtp8,wKjADxuAyCAfmnZE8BwtIA==.32705S2
?at?com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
?at?com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
?at?com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
?at?javax.mail.Transport.send0(Transport.java:169)
?at?javax.mail.Transport.send(Transport.java:98)
这样的异常,要求你必须进行授权校验,它的目的就是阻止他人任意乱发邮件,也算是为了减少垃圾邮件的出现吧。这时候,我们就要使用
在用户名和密码都没有问题的情况下,如果代码抛出javax.mail.AuthenticationFailedException异常,先用OutLook测试一下看能否进行正常的收发邮件,
有时信箱如果是新注册的话,邮件的服务商默认刚注册的帐号是不能使用pop3的。例如163.com在我进行代码测试时,刚刚注册的帐号是没有权限使用该功能的,所以就会抛出上述的异常。
还有,要注意from的email地址和Authenticator类中验证的用户名是一致的,要不也会出错.