Email(邮件发送)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Email(邮件发送)

Email(邮件发送)

 2014/6/10 18:41:23  GreyPenguin  程序员俱乐部  我要评论(0)
  • 摘要:publicvoidsendEmail(StringuserPass,StringuserEmail){Stringhost="smtp.exmail.qq.com";//发送平台Stringfrom="240541928@qq.com";//发件人EmailStringpassword="unsayability";//发件人密码//1Propertiesproperties=newProperties();properties.put("mail.smtp.host",host)
  • 标签:邮件
class="java" name="code">
public void sendEmail(String userPass, String userEmail) {
		String host = "smtp.exmail.qq.com";//发送平台
		String from = "240541928@qq.com";//发件人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();
		}
		
	}
发表评论
用户名: 匿名