JavaMail

	public static void sendMessage(String smtpHost, String from,String fromName, String[] to,
			String subject, String messageText) throws MessagingException,
			java.io.UnsupportedEncodingException {

		// 1、创建session
		System.out.println("Configuring mail session for: " + smtpHost);
		Properties props = new Properties();
//		props.setProperty("mail.smtp.auth", "true");// 指定是否需要SMTP验证
		props.setProperty("mail.smtp.host", smtpHost);// 指定SMTP服务器
		props.put("mail.transport.protocol", "smtp");
		Session mailSession = Session.getDefaultInstance(props);
		mailSession.setDebug(true);// 是否在控制台显示debug信息

		// 2、构造邮件收发人、内容、文本格式、编码
		InternetAddress fromAddress = new InternetAddress(from, fromName);
		InternetAddress[] toAddresses = new InternetAddress[to.length];
		for (int i = 0; i < to.length; i++) {
			toAddresses[i] = new InternetAddress(to[i]);
		}
		MimeMessage testMessage = new MimeMessage(mailSession);
		testMessage.setFrom(fromAddress);
		testMessage.addRecipients(javax.mail.Message.RecipientType.TO,
				toAddresses);
		testMessage.setSentDate(new java.util.Date());
		testMessage.setSubject(MimeUtility.encodeText(subject, "gb2312", "B"));

		testMessage.setContent(messageText, "text/html;charset=gb2312");

		// 3、发送
		Transport transport = mailSession.getTransport("smtp");
		transport.connect();
		transport.sendMessage(testMessage, testMessage.getAllRecipients());
		transport.close();

	}
 

你可能感兴趣的:(html)