spring-JavaMailSenderImpl

使用spring的JavaMailSenderImpl
JavaMailSenderImpl jmsi = new JavaMailSenderImpl();
// 这个host相关的邮箱都会介绍
		jmsi.setHost("smtp.sina.com");
		jmsi.setPort(25);
// 或者[email protected],注册的邮箱的用户名和密码
		jmsi.setUsername("test");
		jmsi.setPassword("test");
		Properties p = new Properties();
// 邮件认证
		p.setProperty("mail.smtp.auth", "true");
		jmsi.setJavaMailProperties(p);
		
		MimeMessage mimeMessage = jmsi.createMimeMessage();
// 编码UTF-8
		 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
// 邮箱名必须是已注册的,后面可以添加一个展示名,不添加默认为邮箱名
         helper.setFrom("[email protected]","用户找回密码");
         helper.setTo("[email protected]");
         helper.setSubject("用户找回密码");
         helper.setText("邮箱找回密码哦!",true);
         jmsi.send(mimeMessage);


javaMail发邮件
	 Properties p = new Properties();
	 p.setProperty("mail.smtp.auth", "true");
         Transport transport;
         Session session = Session.getInstance(p);
         transport = session.getTransport("smtp");
         transport.connect("smtp.sina.com", 25, "test_report", "test_report");
         MimeMessage mm = new MimeMessage(session);
         mm.setSentDate(new Date());
         mm.saveChanges();
         mm.setText("邮箱找回密码哦!!!!!!!!!!!!!!!", "UTF-8");
         mm.setSubject("用户找回密码", "UTF-8");
         mm.setFrom(new InternetAddress("[email protected]", "【注册】"));
         mm.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("[email protected]"));
         Address[] addresses =  mm.getRecipients(MimeMessage.RecipientType.TO);
         transport.sendMessage(mm, addresses);

你可能感兴趣的:(javamail)