javaMail发邮件

使用javaMail发送电子邮件需要两个jar包:activation.jar和mail.jar。具体代码如下:

index.jsp:

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>发送邮箱</title>
 6 <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 7 </head>
 8 <body>
 9 <form name="form1" method="post" action="sendMail.jsp">
10  <p align="center">发送文本型邮件</p>
11    <div align="center">
12      <table width="70%" border="1">
13        <tr> 
14          <td><div align="center">SMTP服务器</div></td>
15          <td><div align="center"> 
16              <input name="SMTPHost" type="text" id="SMTPHost" size="30" maxlength="30">
17            </div></td>
18        </tr>
19        <tr> 
20          <td><div align="center">登录帐号</div></td>
21          <td><div align="center"> 
22              <input name="user" type="text" id="user" size="30" maxlength="30">
23            </div></td>
24        </tr>
25        <tr> 
26          <td><div align="center">登录密码</div></td>
27          <td><div align="center"> 
28              <input name="password" type="password" id="password" size="30" maxlength="100">
29            </div></td>
30        </tr>
31        <tr> 
32          <td><div align="center">发件人</div></td>
33          <td><div align="center"> 
34              <input name="from" type="text" id="from" size="30" maxlength="100">
35            </div></td>
36        </tr>
37        <tr> 
38          <td><div align="center">收件人</div></td>
39          <td><div align="center"> 
40              <input name="to" type="text" id="to" size="30" maxlength="100">
41            </div></td>
42        </tr>
43        <tr> 
44          <td><div align="center">主题</div></td>
45          <td><div align="center"> 
46              <input name="subject" type="text" id="subject" size="30" maxlength="30">
47            </div></td>
48        </tr>
49        <tr> 
50          <td colspan="2"><div align="center"> 
51              <textarea name="body" cols="40" rows="10" id="body"></textarea>
52            </div></td>
53        </tr>
54        <tr> 
55          <td colspan="2"><div align="center"> 
56              <input type="submit" name="Submit" value="发送">
57              &nbsp;&nbsp;&nbsp;&nbsp; 
58              <input name="Reset" type="submit" id="Reset" value="清空">
59            </div></td>
60        </tr>
61      </table>
62    </div>
63    <p align="center">&nbsp;</p>
64 </form>
65 <p align="center">&nbsp;</p>
66 </body>
67 </html>

 

sendMail.jsp:

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 
 3 <jsp:useBean id="mySend" class="com.email.SendTextMail"></jsp:useBean>
 4 <jsp:setProperty property="*" name="mySend"/>
 5 
 6 <%
 7     //注意这里使用javaBean自省机制为属性赋值
 8     boolean status = mySend.send();
 9     if(status){
10         out.println("恭喜你,发送邮件成功!");
11     }else{
12         out.println("对不起,发送邮件失败!");
13     }
14 %>

SendTextMail.java:

  1 package com.email;
  2 
  3 import java.util.Date;
  4 import java.util.Properties;
  5 
  6 import javax.mail.Authenticator;
  7 import javax.mail.Message;
  8 import javax.mail.PasswordAuthentication;
  9 import javax.mail.Session;
 10 import javax.mail.Transport;
 11 import javax.mail.internet.InternetAddress;
 12 import javax.mail.internet.MimeMessage;
 13 
 14 public class SendTextMail {
 15     String SMTPHost = "";//SMTP服务器
 16     String user = "";//登录SMTP服务器的帐号
 17     String password = "";//密码
 18     String from = "";
 19     String to = "";
 20     String subject = "";
 21     String content = "";
 22     
 23     public SendTextMail(){}
 24 
 25     public String getSMTPHost() {
 26         return SMTPHost;
 27     }
 28 
 29     public void setSMTPHost(String host) {
 30         SMTPHost = host;
 31     }
 32 
 33     public String getUser() {
 34         return user;
 35     }
 36 
 37     public void setUser(String user) {
 38         this.user = user;
 39     }
 40 
 41     public String getPassword() {
 42         return password;
 43     }
 44 
 45     public void setPassword(String password) {
 46         this.password = password;
 47     }
 48 
 49     public String getFrom() {
 50         return from;
 51     }
 52 
 53     public void setFrom(String from) {
 54         this.from = from;
 55     }
 56 
 57     public String getTo() {
 58         return to;
 59     }
 60 
 61     public void setTo(String to) {
 62         this.to = to;
 63     }
 64 
 65     public String getSubject() {
 66         return subject;
 67     }
 68 
 69     public void setSubject(String subject) {
 70         this.subject = subject;
 71     }
 72 
 73     public String getContent() {
 74         return content;
 75     }
 76 
 77     public void setContent(String content) {
 78         this.content = content;
 79     };
 80     /*
 81      * liuling476777389@sina.cn
 82         pop.sina.cn
 83      */
 84     public boolean send(){
 85         //创建一个属性对象
 86         Properties props = System.getProperties();
 87         //指定SMTP服务器
 88         props.put("mail.smtp.host", SMTPHost);
 89         props.put("mail.smtp.auth", "true"); //true一定要加引号
 90         try {
 91             //创建一个授权验证对象
 92             SmtpAuth auth = new SmtpAuth();
 93             auth.setAccount(user, password);
 94             //创建一个session对象
 95             Session mailSession = Session.getDefaultInstance(props,auth);
 96             mailSession.setDebug(true);
 97             //创建一个message对象
 98             Message message = new MimeMessage(mailSession);
 99             message.setFrom(new InternetAddress(from));//指定发件人
100             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//指定收件人
101             message.setSubject(subject);//指定主题
102             message.setText(content);//指定内容
103             message.setSentDate(new Date());//指定发送日期
104             //指定邮件等级1.紧急  3.普通  5.缓慢
105             message.setHeader("X-Priority", "1");
106             message.saveChanges();
107             //创建一个Transport对象
108             Transport transport = mailSession.getTransport("smtp");
109             //连接SMTP服务器
110             transport.connect(SMTPHost, user, password);
111             //发送邮件
112             transport.sendMessage(message, message.getAllRecipients());
113             transport.close();
114             return true;
115         } catch (Exception e) {
116             e.printStackTrace();
117             return false;
118         }
119     }
120         
121 }
122 
123 class SmtpAuth extends Authenticator{
124     String user,password;
125     void setAccount(String user,String password){
126         this.user = user;
127         this.password = password;
128     }
129     //取得PasswordAuthentication对象
130     protected PasswordAuthentication getPasswordAuthentication(){
131         return new PasswordAuthentication(user,password);
132     }
133 }

 

经测试用过,能过成功发送邮件。 

 

posted @ 2013-03-25 21:10  残剑_  阅读(520)  评论(0编辑  收藏  举报