用Java发送邮件

要用Java发送邮件,除过JDK本身的jar包之外,还需要两个额外的jar包:JavaMail和JAF。当然,如果你使用的JavaEE的JDK,那就不用单独去网上下载了,因为JavaEE的JDK中已经自带了这两个jar包。如果你使用的是JavaSE的JDK,那么你需要去Oracle的网站上下载这两个jar包。JavaMail 1.4.5 jar包下载地址是:http://www.oracle.com/technetwork/java/index-138643.html,JAF 1.1.1 jar包下载地址是:http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html。将这两个Zip包下载下来之后解压,将里边的几个jar包(mail.jar、dsn.jar、imap.jar、mailapi.jar、pop3.jar、smtp.jar和activation.jar)全部加到工程里边。下边就可以开始写程序了。加上测试程序,一共有4个类。
第一个类:MailSenderInfo.java,记录发送邮件所需的各种信息,如发送邮件服务器的地址、端口号、发件人邮箱、收件人邮箱等等。代码如下:
 1 package com.test.mail;
 2 import java.util.Properties;
 3 
 4 public class MailSenderInfo {
 5  // 发送邮件的服务器的IP(或主机地址)
 6  private String mailServerHost;
 7  // 发送邮件的服务器的端口
 8  private String mailServerPort = "25";
 9  // 发件人邮箱地址
10  private String fromAddress;
11  // 收件人邮箱地址
12  private String toAddress;
13  // 登陆邮件发送服务器的用户名
14  private String userName;
15  // 登陆邮件发送服务器的密码
16  private String password;
17  // 是否需要身份验证
18  private boolean validate = false;
19  // 邮件主题
20  private String subject;
21  // 邮件的文本内容
22  private String content;
23  // 邮件附件的文件名
24  private String[] attachFileNames;
25  
26  public Properties getProperties() {
27   Properties p = new Properties();
28   p.put("mail.smtp.host", this.mailServerHost);
29   p.put("mail.smtp.port", this.mailServerPort);
30   p.put("mail.smtp.auth", validate ? "true" : "false");
31   return p;
32  }
33  public String getMailServerHost() {
34   return mailServerHost;
35  }
36  public void setMailServerHost(String mailServerHost) {
37   this.mailServerHost = mailServerHost;
38  }
39  public String getMailServerPort() {
40   return mailServerPort;
41  }
42  public void setMailServerPort(String mailServerPort) {
43   this.mailServerPort = mailServerPort;
44  }
45  public boolean isValidate() {
46   return validate;
47  }
48  public void setValidate(boolean validate) {
49   this.validate = validate;
50  }
51  public String[] getAttachFileNames() {
52   return attachFileNames;
53  }
54  public void setAttachFileNames(String[] fileNames) {
55   this.attachFileNames = fileNames;
56  }
57  public String getFromAddress() {
58   return fromAddress;
59  }
60  public void setFromAddress(String fromAddress) {
61   this.fromAddress = fromAddress;
62  }
63  public String getPassword() {
64   return password;
65  }
66  public void setPassword(String password) {
67   this.password = password;
68  }
69  public String getToAddress() {
70   return toAddress;
71  }
72  public void setToAddress(String toAddress) {
73   this.toAddress = toAddress;
74  }
75  public String getUserName() {
76   return userName;
77  }
78  public void setUserName(String userName) {
79   this.userName = userName;
80  }
81  public String getSubject() {
82   return subject;
83  }
84  public void setSubject(String subject) {
85   this.subject = subject;
86  }
87  public String getContent() {
88   return content;
89  }
90  public void setContent(String textContent) {
91   this.content = textContent;
92  }
93 }
View Code

第二个类:MyAuthenticator.java,邮箱用户名和密码认证器。代码如下:

 1 package com.test.mail;
 2 import javax.mail.Authenticator;
 3 import javax.mail.PasswordAuthentication;
 4 
 5 public class MyAuthenticator extends Authenticator {
 6  String userName = null;
 7  String password = null;
 8  public MyAuthenticator() {
 9  }
10  public MyAuthenticator(String username, String password) {
11   this.userName = username;
12   this.password = password;
13  }
14  protected PasswordAuthentication getPasswordAuthentication() {
15   return new PasswordAuthentication(userName, password);
16  }
17 }
View Code

第三个类:SimpleMailSender.java,用来发送邮件类。代码如下:

 1 package com.test.mail;
 2 import java.util.Date;
 3 import java.util.Properties;
 4 import javax.mail.Address;
 5 import javax.mail.BodyPart;
 6 import javax.mail.Message;
 7 import javax.mail.MessagingException;
 8 import javax.mail.Multipart;
 9 import javax.mail.Session;
10 import javax.mail.Transport;
11 import javax.mail.internet.InternetAddress;
12 import javax.mail.internet.MimeBodyPart;
13 import javax.mail.internet.MimeMessage;
14 import javax.mail.internet.MimeMultipart;
15 
16 public class SimpleMailSender {
17  
18  public boolean sendTextMail(MailSenderInfo mailInfo) {
19   // 判断是否需要身份认证
20   MyAuthenticator authenticator = null;
21   Properties pro = mailInfo.getProperties();
22   if (mailInfo.isValidate()) {
23    // 如果需要身份认证,则创建一个密码验证器
24    authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
25   }
26   // 根据邮件会话属性和密码验证器构造一个发送邮件的session
27   Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
28   try {
29    // 根据session创建一个邮件消息
30    Message mailMessage = new MimeMessage(sendMailSession);
31    // 创建邮件发送者地址
32    Address from = new InternetAddress(mailInfo.getFromAddress());
33    // 设置邮件消息的发送者
34    mailMessage.setFrom(from);
35    // 创建邮件的接收者地址,并设置到邮件消息中
36    Address to = new InternetAddress(mailInfo.getToAddress());
37    mailMessage.setRecipient(Message.RecipientType.TO, to);
38    // 设置邮件消息的主题
39    mailMessage.setSubject(mailInfo.getSubject());
40    // 设置邮件消息发送的时间
41    mailMessage.setSentDate(new Date());
42    // 设置邮件消息的主要内容
43    String mailContent = mailInfo.getContent();
44    mailMessage.setText(mailContent);
45    // 发送邮件
46    Transport.send(mailMessage);
47    return true;
48   } catch (MessagingException ex) {
49    ex.printStackTrace();
50   }
51   return false;
52  }
53  
54  public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
55   // 判断是否需要身份认证
56   MyAuthenticator authenticator = null;
57   Properties pro = mailInfo.getProperties();
58   // 如果需要身份认证,则创建一个密码验证器
59   if (mailInfo.isValidate()) {
60    authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
61   }
62   // 根据邮件会话属性和密码验证器构造一个发送邮件的session
63   Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
64   try {
65    // 根据session创建一个邮件消息
66    Message mailMessage = new MimeMessage(sendMailSession);
67    // 创建邮件发送者地址
68    Address from = new InternetAddress(mailInfo.getFromAddress());
69    // 设置邮件消息的发送者
70    mailMessage.setFrom(from);
71    // 创建邮件的接收者地址,并设置到邮件消息中
72    Address to = new InternetAddress(mailInfo.getToAddress());
73    // Message.RecipientType.TO属性表示接收者的类型为TO
74    mailMessage.setRecipient(Message.RecipientType.TO, to);
75    // 设置邮件消息的主题
76    mailMessage.setSubject(mailInfo.getSubject());
77    // 设置邮件消息发送的时间
78    mailMessage.setSentDate(new Date());
79    // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
80    Multipart mainPart = new MimeMultipart();
81    // 创建一个包含HTML内容的MimeBodyPart
82    BodyPart html = new MimeBodyPart();
83    // 设置HTML内容
84    html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
85    mainPart.addBodyPart(html);
86    // 将MiniMultipart对象设置为邮件内容
87    mailMessage.setContent(mainPart);
88    // 发送邮件
89    Transport.send(mailMessage);
90    return true;
91   } catch (MessagingException ex) {
92    ex.printStackTrace();
93   }
94   return false;
95  }
96 }
View Code

第三个类:SimpleMailSender.java,用来发送邮件类。代码如下:

 1 package com.test.mail;
 2 public class SendMailDemo {
 3  public static void main(String[] args) {
 4   // 设置邮件服务器信息
 5   MailSenderInfo mailInfo = new MailSenderInfo();
 6   mailInfo.setMailServerHost("smtp.163.com");
 7   mailInfo.setMailServerPort("25");
 8   mailInfo.setValidate(true);
 9   
10   // 邮箱用户名
11   mailInfo.setUserName("zhangsan@163.com");
12   // 邮箱密码
13   mailInfo.setPassword("zhangsanpass");
14   // 发件人邮箱
15   mailInfo.setFromAddress("zhangsan@163.com");
16   // 收件人邮箱
17   mailInfo.setToAddress("lisi@sina.com");
18   // 邮件标题
19   mailInfo.setSubject("测试Java程序发送邮件");
20   // 邮件内容
21   StringBuffer buffer = new StringBuffer();
22   buffer.append("JavaMail 1.4.5 jar包下载地址:http://www.oracle.com/technetwork/java/index-138643.html\n");
23   buffer.append("JAF 1.1.1 jar包下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html");
24   mailInfo.setContent(buffer.toString());
25   
26   // 发送邮件
27   SimpleMailSender sms = new SimpleMailSender();
28   // 发送文体格式
29   sms.sendTextMail(mailInfo);
30   // 发送html格式
31   SimpleMailSender.sendHtmlMail(mailInfo);
32   System.out.println("邮件发送完毕");
33  }
34 }
View Code

至此,Java邮件发送程序已经写完,当然了,这只是实现了最基本的功能。经过测试,可以将邮件发送出去。

posted @ 2014-03-28 11:00  Emily1130  阅读(223)  评论(0编辑  收藏  举报