Email(邮件发送)

public void sendEmail(String userPass, String userEmail) {
		String host = "smtp.exmail.qq.com";//发送平台
		String from = "[email protected]";//发件人Email
		String password = "unsayability";//发件人密码

		// 1
		Properties properties = new Properties();
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.auth", "true");

		// 2
		Session session = Session.getInstance(properties);
		session.setDebug(true);

		// 3
		MimeMessage mimeMessage = new MimeMessage(session);
		InternetAddress fromInternetAddress = null;
		InternetAddress toInternetAddress = null;
		try {
			fromInternetAddress = new InternetAddress(from);
			toInternetAddress = new InternetAddress(userEmail);

		} catch (AddressException e) {
			e.printStackTrace();
		}

		try {
			mimeMessage.setFrom(fromInternetAddress);
			mimeMessage.setRecipient(Message.RecipientType.TO,
					toInternetAddress);
			mimeMessage.setSubject("恭喜你已成功找回密码");
			mimeMessage.setText("小伙你的密码是" + userPass
					+ " 切记!切记!");
			mimeMessage.setSentDate(new Date());

			// 4
			mimeMessage.saveChanges();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		Transport transport = null;
		try {
			// 5
			transport = session.getTransport("smtp");
			transport.connect(host, from, password);		
			transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
			// 6
			transport.close();
		} catch (NoSuchProviderException e) {
			
			e.printStackTrace();
		} catch (MessagingException e) {
			
			e.printStackTrace();
		}
		
	}

你可能感兴趣的:(java)