且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javaMail发邮件

更新时间:2021-11-12 17:51:33

使用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 
 4 
 5 
 6 
 7 
 8 
 9 

10

发送文本型邮件

11    

12      
13
14
15
18
19
20
21
24
25
26
27
30
31
32
33
36
37
38
39
42
43
44
45
48
49
50
53
54
55
60
61
                  
SMTP服务器
          	

16              
17            
        
                  
登录帐号
          	

22              
23            
        
                  
登录密码
          	

28              
29            
        
                  
发件人
          	

34              
35            
        
                  
收件人
          	

40              
41            
        
                  
主题
          	

46              
47            
        
                  

51              
52            
        
                  

56              


57                  
58              


59            
        
      

62    

63

 

64 

65

 

66 
67 

 

sendMail.jsp:

 1 <%@ page language=”java” import=”java.util.“ pageEncoding=”utf-8”%>
 2
 3 class=”com.email.SendTextMail”>
 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 }

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