JAVAWEB练习——邮箱服务器,邮箱发送测试,定时发送邮件祝福

一、邮箱服务器简介

1.邮箱服务器的基本概念

  • 邮件的客户端:可以只安装在电脑上的也可以是网页形式的
  • 邮件服务器:起到邮件的接受与推送的作用
  • 邮件发送的协议:POP3,IMAP4、SMTP

2.邮箱的发送过程

简单来说,我们发邮件,或者接受别人发来的邮件IMAP4协议、SMTP协议帮我们实现。
我们需要取邮件,读邮件,拿右键都是POP3协议帮我们实现。
在这里插入图片描述

3.邮箱服务器的安装

第一步: 双击邮箱服务器软件

第二步:对邮箱服务器进行配置

双击界面找到工具——>服务器设置——>随后设置服务器的内容

在这里插入图片描述
第三步:新建账户

回到界面——>账户——>新建账户

在这里插入图片描述

4.邮箱客户端安装

应用商店下载Formail
注册成功后,找到设置——>系统——>账户设置,新建相应的在服务器中输入的账户。
在这里插入图片描述

二、使用代码发送邮件

第一步:导入jar包

在这里插入图片描述

第二步:修改MainUtils

public class MailUtils {
    
    
	//email:邮件发给谁 subject:表示主题  emailMsg:邮件的内容
	public static void sendMail(String email,String subject, String emailMsg)
			throws AddressException, MessagingException, GeneralSecurityException {
    
    
	
		//创建一个程序与邮件服务器会话对象Session
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");//发邮件协议——SMTP
		props.setProperty("mail.host", "smtp.qq.com");		//发送邮件的服务器地址
		props.setProperty("mail.smtp.auth", "true");// 指定验证为true
		
        props.put("mail.smtp.ssl.socketFactory",sf);
		//创建验证器
		Authenticator auth = new Authenticator() {
    
    
			public PasswordAuthentication getPasswordAuthentication() {
    
    
				return new PasswordAuthentication("2322517484", "032426zyl,.");//发邮件的账号的验证
			}
		};
		//此时并非HttpSession
		Session session = Session.getInstance(props, auth);
		
		// 创建一个Message,相当于邮件的内容
		Message message = new MimeMessage(session);

		message.setFrom(new InternetAddress("[email protected]")); // 设置发送者

		message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 谁是接收者

		message.setSubject(subject);

		message.setContent(emailMsg, "text/html;charset=utf-8");

		// 3.创建 Transport用户将邮件发送

		Transport.send(message);
	}
}

第三步:做测试:

public class SendMailTest {
    
    
	public static void main(String[] args) throws GeneralSecurityException {
    
    
		try {
    
    
			MailUtils.sendMail("[email protected]","测试","此为此时邮件");
		} catch (AddressException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

三、定时发送邮件demo

第一步:准备数据库
在这里插入图片描述
第二步:导包
在这里插入图片描述
第四步:做测试

 public class BirthdayListener implements ServletContextListener{
    
    
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
    
    
		//当web应用启动开启任务调动——功能在用户的生日当天发送邮件
		Timer timer =new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
    
    
			
			@Override
			public void run() {
    
    
				// 为当前的生日的用户进行发邮件
				//1.获得当前过生日的人
				//获得今天过生日人的信息
				SimpleDateFormat format =new SimpleDateFormat("MM-dd");
				String currentDate= format.format(new Date());
				//根据当前时间从数据查询今天过生日的人
				QueryRunner queryRunner =new QueryRunner(DataSourceUtils.getDataSource());
				String sql="SELECT * FROM users WHERE birthday like ?";
				List <User>users=null;
				try {
    
    
					users= queryRunner.query(sql,new BeanListHandler<User>(User.class),"%"+currentDate);
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//发邮件
				if (users !=null&&users.size()>0) {
    
    
					for (User user : users) {
    
    
						String emailMsg="亲爱的"+user.getUsername()+"生日快了";
						try {
    
    
							MailUtils.sendMail(user.getEmail(), "生日祝福", emailMsg);
							System.out.println("邮件发送完毕");
						} catch (MessagingException | GeneralSecurityException e) {
    
    
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				
				}
			}
		},new Date(),1000*60*60*24);//实际开发中起始时间一定是一个固定的时间
						//实际开发中你的间隔时间为一天,每一天取出当天过生日的人,发送邮件
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
    
    
		
		
	}
}

第五步:配置web.xml文件

<listener>
  		<listener-class>com.listener.birthday.BirthdayListener</listener-class>
  </listener>

第六步
如果我们需要进行实现,我们可以在MailUtils,自行设置一些属性

public class MailUtils {
    
    
	//email:邮件发给谁 subject:表示主题  emailMsg:邮件的内容
	public static void sendMail(String email,String subject, String emailMsg)
			throws AddressException, MessagingException, GeneralSecurityException {
    
    
	
		//创建一个程序与邮件服务器会话对象Session
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");//发邮件协议——SMTP
		props.setProperty("mail.host", "smtp.qq.com");		//发送邮件的服务器地址
		props.setProperty("mail.smtp.auth", "true");// 指定验证为true
		
       
		//创建验证器
		Authenticator auth = new Authenticator() {
    
    
			public PasswordAuthentication getPasswordAuthentication() {
    
    
				return new PasswordAuthentication("2322517484", "032426zyl,.");//发邮件的账号的验证
			}
		};
		//此时并非HttpSession
		Session session = Session.getInstance(props, auth);
		
		// 创建一个Message,相当于邮件的内容
		Message message = new MimeMessage(session);

		message.setFrom(new InternetAddress("[email protected]")); // 设置发送者

		message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 谁是接收者

		message.setSubject(subject);

		message.setContent(emailMsg, "text/html;charset=utf-8");

		// 3.创建 Transport用户将邮件发送

		Transport.send(message);
	}
}

猜你喜欢

转载自blog.csdn.net/Mr_GYF/article/details/109284605
今日推荐