JavaMail实现邮件发送的方法

时间:2022-06-10 06:41:30

本文实例讲述了JavaMail实现邮件发送的方法。分享给大家供大家参考。具体如下:

下载 activation.jar 和 mail.jar
配置CLASSPATH,将上面两个jar包加入到CLASSPATH中。

JavaBean : "SendEmail.java"

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cls;
import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
 String host;
 String username;
 String password;
 String fromAddr;
 String toAddr;
 String subject;
 String content;
 public SendEmail()
 {
 // 变量初始化
 host = "smtp.qq.com";
 username = "";
 password = "";
 fromAddr = "@";
 }
 // 发送邮件
 public boolean sendMail()
 {
 Properties prop;
 Session session;
 MimeMessage msg;
 try
 {
  prop = new Properties(); // 存储连接参数
  prop.put("mail.smtp.host",host);
  prop.put("mail.smtp.auth","true");
  session = Session.getDefaultInstance(prop,null);
  // 获得一个邮件的Session
  msg = new MimeMessage(session); // 邮件信息
  // 检查邮件地址是否合法
  if(fromAddr == null || fromAddr.equals(""))
  {
  throw new Exception("发件地址错误");
  }
  if(toAddr == null || toAddr.equals(""))
  {
  throw new Exception("目标地址错误");
  }
  //设置源地址
  msg.setFrom(new InternetAddress(fromAddr));
  //设置目的地址
  msg.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddr));
  //设置主题
  msg.setSubject(subject);
  Multipart mp = new MimeMultipart(); //邮件内容
  MimeBodyPart mbpContent = new MimeBodyPart();
  mbpContent.setContent(content,"text/html"); // 邮件格式
  mp.addBodyPart(mbpContent);
  msg.setContent(mp);
  msg.setSentDate(new Date());
  // 发送邮件
  Transport transport = session.getTransport("smtp");
  transport.connect((String)prop.get("mail.smtp.host"),username,password);
  transport.sendMessage(msg,msg.getRecipients(MimeMessage.RecipientType.TO));
  transport.close();
  return true;
 }
 catch(Exception e)
 {
  System.out.println(e);
  return false;
 }
 }
 // getter and setter
 public String getPassword()
 {
 return password;
 }
 public void setPassword(String password)
 {
 this.password = password;
 }
 public String getHost()
 {
 return host;
 }
 public void setHost(String host)
 {
 this.host = host;
 }
 public String getUsername()
 {
 return username;
 }
 public void setUsername(String username)
 {
 this.username = username;
 }
 public String getFromAddr()
 {
 return this.fromAddr;
 }
 public void setFromAddr(String addr)
 {
 fromAddr = addr;
 }
 public String getToAddr()
 {
 return this.toAddr;
 }
 public void setToAddr(String addr)
 {
 toAddr = addr;
 }
 public String getSubject()
 {
 return subject;
 }
 public void setSubject(String sub)
 {
 subject = sub;
 }
 public String getContent()
 {
 return content;
 }
 public void setContent(String content)
 {
 this.content = content;
 }
}

sendmail.jsp:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <title>Mail</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 </head>
 <body>
 <center>
  <jsp:useBean id="email" class="cls.SendEmail" scope="page" />
  <jsp:setProperty name="email" property="toAddr" value="host" />
  <jsp:setProperty name="email" property="fromAddr" value="host" />
  <jsp:setProperty name="email" property="username" value="username" />
  <jsp:setProperty name="email" property="password" value="password" />
  <jsp:setProperty name="email" property="subject" value="mail test" />
  <jsp:setProperty name="email" property="content" value="hello world"/>
  <%=email.sendMail() %>
 </center>
 </body>
</html>

希望本文所述对大家的java程序设计有所帮助。