博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建Java工程实现发送邮件(163邮箱)
阅读量:2386 次
发布时间:2019-05-10

本文共 4721 字,大约阅读时间需要 15 分钟。

创建Java工程

结构图

导入jar包:activation.jar/mail.jar

创建VerifyEmail类,继承Authenticator,返回账号密码验证

package com.cp.entity;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

/**

 * @author chengpeng

 * @date 2017年9月12日 下午4:26:01

 * description 继承Authenticator,返回账号密码验证

 */

public class VerifyEmail extends Authenticator{

//成员变量

String userName = "";//账号

String password = "";//密码

//构造方法

public VerifyEmail(){

super();

}

public VerifyEmail(String userName , String password){

super();

this.userName = userName;

this.password = password;

}

protected PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(userName, password);

}

}

创建EmailEntity,用来存放所需的数据

package com.cp.entity;

import java.io.Serializable;

/**

 * @author chengpeng

 * @date 2017年9月12日 下午4:38:27

 * description 用来存放所需的数据

 */

public class EmailEntity implements Serializable{

private static final long serialVersionUID = 1L;

String host;//邮箱主机

Integer port;//主机端口

String userName;//发送者的账号

String password;//发送者的密码

String fromAddress;//发送者的邮箱地址

String toAddress;//接收者的邮箱地址

String subject;//设置邮件主题

String context;//设置邮件内容

String contextType;//设置邮件类型

public String getHost() {

return host;

}

public Integer getPort() {

return port;

}

public String getUserName() {

return userName;

}

public String getPassword() {

return password;

}

public String getFromAddress() {

return fromAddress;

}

public String getToAddress() {

return toAddress;

}

public String getSubject() {

return subject;

}

public String getContext() {

return context;

}

public String getContextType() {

return contextType;

}

public void setHost(String host) {

this.host = host;

}

public void setPort(Integer port) {

this.port = port;

}

public void setUserName(String userName) {

this.userName = userName;

}

public void setPassword(String password) {

this.password = password;

}

public void setFromAddress(String fromAddress) {

this.fromAddress = fromAddress;

}

public void setToAddress(String toAddress) {

this.toAddress = toAddress;

}

public void setSubject(String subject) {

this.subject = subject;

}

public void setContext(String context) {

this.context = context;

}

public void setContextType(String contextType) {

this.contextType = contextType;

}

}

创建EmailSend,测试、发送邮件

package com.cp.util;

import java.util.Date;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import com.cp.entity.EmailEntity;

import com.cp.entity.VerifyEmail;

/**

 * @author chengpeng

 * @date 2017年9月12日 下午5:02:16

 * description 测试,发送邮件

 */

public class EmailSend {

//判断发送是否成功

public static boolean EmailSendTest(EmailEntity emailEntity){

try {

//配置文件

Properties properties = new Properties();

properties.put("mail.smtp.auth", "true");

properties.put("mail.smtp.host", emailEntity.getHost());

properties.put("mail.smtp.port", 25);

properties.put("mail.smtp.starrttls.enable", "true");

//使用starrtls安全连接

//创建会话

VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(), emailEntity.getPassword());

Session mailSession = Session.getInstance(properties, verifyEmail);

mailSession.setDebug(true);

//创建信息对象

Message message = new MimeMessage(mailSession);

InternetAddress from = new InternetAddress(emailEntity.getFromAddress());

InternetAddress to = new InternetAddress(emailEntity.getToAddress());

message.setFrom(from);//设置邮件信息的来源

message.setRecipient(MimeMessage.RecipientType.TO, to);//设置邮件的接收者

message.setSubject(emailEntity.getSubject());

message.setSentDate(new Date());//设置邮件发送日期

message.setContent(emailEntity.getContext() , emailEntity.getContextType());//设置邮件内容

message.saveChanges();

//发送邮件

Transport transport = mailSession.getTransport("smtp");

transport.connect(emailEntity.getHost(), emailEntity.getUserName(), emailEntity.getPassword());

System.out.println("trans:" + transport);

transport.sendMessage(message, message.getAllRecipients());

System.out.println("sent successful...");

return true;

} catch (MessagingException e) {

e.printStackTrace();

System.out.println("send fial...");

return false;

}

}

//主函数测试

public static void main(String []args){

EmailEntity emailEntity = new EmailEntity();

emailEntity.setHost("smtp.163.com");

emailEntity.setPort(25);

emailEntity.setUserName("......@163.com");//有些参数自己添加

emailEntity.setPassword("......");

emailEntity.setFromAddress("......@163.com");

emailEntity.setToAddress("......");

emailEntity.setSubject("第一封邮件测试");

emailEntity.setContext("This is a emial send test...");

emailEntity.setContextType("text/html;charset=utf-8");

boolean isSend = EmailSendTest(emailEntity);

System.out.println("email send successful == " + isSend);

}

}

运行程序,测试成功

你可能感兴趣的文章
Grep与web漏洞挖掘
查看>>
正则表达式使用详解
查看>>
引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法(新手推荐)
查看>>
编写不受魔术引号影响的php应用
查看>>
PHP开发安全设置
查看>>
Php Endangers - Remote Code Execution
查看>>
变量的变量,PHP和你
查看>>
PROC系列之四---/proc/loadavg
查看>>
某大型网站的内核TCP/ip优化脚本
查看>>
Defeating SSL using SSLStrip (Marlinspike Blackhat)
查看>>
大型网站数据库架构
查看>>
rdp 安全策略
查看>>
Threat Intelligence Quotient Test
查看>>
Cisco路由器上防止DDOS的一些建议
查看>>
系统安全防护之UNIX下入侵检测方法
查看>>
域控渗透技巧
查看>>
Minion security project and 分布式nmap
查看>>
防火墙相关
查看>>
网络性能测试工具Iperf上手指南
查看>>
opensecuritytraining video
查看>>