java发邮件的代码

通过java发送邮件的代码

转载请注明出处:java发邮件的代码

package com.zuidaima.util.mail;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailSender {

	public static void main(String[] args) throws MessagingException {

		String host = "smtp.gmail.com";
		int port = 587;
		String username = "zuidaima";
		String password = "zuidaima.com";

		Properties javaMailProperties = new Properties();
		javaMailProperties.put("mail.smtp.auth", "true");
		javaMailProperties.put("mail.smtp.starttls.enable", "true");
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost(host);
		mailSender.setPort(port);
		mailSender.setUsername(username);
		mailSender.setPassword(password);
		mailSender.setJavaMailProperties(javaMailProperties);
		String email = "[email protected]";
		String nickname = "最代码";

		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper help = new MimeMessageHelper(message, true, "UTF-8");
		help.setFrom("[email protected]");
		help.setTo(new String[] { "[email protected]", email });
		help.setSubject("测试");
		String content = "你好" + nickname;
		help.setText(content, true);
		mailSender.send(message);
	}

}	    			

 代码下载地址:http://www.zuidaima.com/share/1550463223024640.htm

你可能感兴趣的:(java,邮件,email)