JavaMail实现邮件发送的方法
作者:司青 时间:2023-08-18 06:37:38
本文实例讲述了JavaMail实现邮件发送的方法。分享给大家供大家参考。具体如下:
下载 activation.jar 和 mail.jar
配置CLASSPATH,将上面两个jar包加入到CLASSPATH中。
JavaBean : "SendEmail.java"
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:
<%@ 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程序设计有所帮助。
标签:Java,Mail,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android仿百度福袋红包界面
2023-11-01 04:53:01
![](https://img.aspxhome.com/file/2023/2/138232_0s.jpg)
布隆过滤器(Bloom Filter)的Java实现方法
2022-09-14 18:06:06
java线程并发控制同步工具CountDownLatch
2022-09-02 12:18:06
![](https://img.aspxhome.com/file/2023/1/75461_0s.jpg)
Maven中央仓库发布的实现方法
2023-12-09 07:15:06
![](https://img.aspxhome.com/file/2023/1/64521_0s.jpg)
java基于netty NIO的简单聊天室的实现
2023-11-18 15:29:43
![](https://img.aspxhome.com/file/2023/7/69697_0s.png)
Android 和 windows C/C++/QT通讯时字节存储
2022-07-16 06:09:02
Spring Boot基础学习之Mybatis操作中使用Redis做缓存详解
2023-01-19 16:35:55
![](https://img.aspxhome.com/file/2023/1/67961_0s.jpg)
23种设计模式(12)java模版方法模式
2021-10-24 11:00:31
![](https://img.aspxhome.com/file/2023/6/94306_0s.jpg)
C#基础知识系列八const和readonly关键字详细介绍
2023-06-25 16:39:37
![](https://img.aspxhome.com/file/2023/0/102190_0s.png)
基于Java实现的Dijkstra算法示例
2021-09-17 02:51:13
Recyclerview添加头布局和尾布局、item点击事件详解
2022-04-19 12:38:12
![](https://img.aspxhome.com/file/2023/8/139038_0s.gif)
lambda表达式解决java后台分组排序过程解析
2023-11-29 06:03:39
C#中WebBroeser控件用法实例教程
2021-09-14 18:11:05
Java NegativeArraySizeException异常解决方案
2023-10-31 07:16:51
SpringBoot整合Shiro两种方式(总结)
2021-09-08 21:28:24
![](https://img.aspxhome.com/file/2023/6/65146_0s.png)
Android实现欢迎滑动页面
2022-03-13 14:40:53
![](https://img.aspxhome.com/file/2023/4/137124_0s.jpg)
Java面试题解析之判断以及防止SQL注入
2023-05-26 18:08:59
重温C# clr 笔记总结
2023-06-22 02:24:30
c#根据文件大小显示文件复制进度条实例
2022-10-10 10:35:16
![](https://img.aspxhome.com/file/2023/5/116575_0s.png)
Java设计模式之GOF23全面讲解
2023-06-24 06:15:06
![](https://img.aspxhome.com/file/2023/8/78228_0s.jpg)