欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java jdk email 博客分类: java utils  

程序员文章站 2024-03-14 08:47:46
...
public class SimpleMailSender {

	public SimpleMailSender() {
	}

	public static boolean sendTextMail(MailSenderInfo mailInfo) {
		JavaingAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();
		if (mailInfo.isValidate()) {
			authenticator = new JavaingAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
		try {
			Message mailMessage = new MimeMessage(sendMailSession);
			javax.mail.Address from = new InternetAddress(mailInfo.getFromAddress());
			mailMessage.setFrom(from);
			javax.mail.Address to = new InternetAddress(mailInfo.getToAddress());
			mailMessage.setRecipient(javax.mail.Message.RecipientType.TO, to);
			mailMessage.setSubject(mailInfo.getSubject());
			mailMessage.setSentDate(new Date());
			String mailContent = mailInfo.getContent();
			mailMessage.setText(mailContent);
			Transport.send(mailMessage);
		}
		catch (MessagingException ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
		JavaingAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();
		if (mailInfo.isValidate()) {
			authenticator = new JavaingAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
		try {
			Message mailMessage = new MimeMessage(sendMailSession);
			javax.mail.Address from = new InternetAddress(mailInfo.getFromAddress());
			mailMessage.setFrom(from);
			javax.mail.Address to = new InternetAddress(mailInfo.getToAddress());
			mailMessage.setRecipient(javax.mail.Message.RecipientType.TO, to);
			mailMessage.setSubject(mailInfo.getSubject());
			mailMessage.setSentDate(new Date());
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			mailMessage.setContent(mainPart);
			Transport.send(mailMessage);
		}
		catch (MessagingException ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}
}



public class MailSenderInfo {

	private String mailServerHost;
	private String mailServerPort;
	private String fromAddress;
	private String toAddress;
	private String userName;
	private String password;
	private boolean validate;
	private String subject;
	private String content;
	private String attachFileNames[];

	public MailSenderInfo() {
		mailServerPort = "25";
		validate = false;
	}

	public Properties getProperties() {
		Properties properties = new Properties();
		properties.put("mail.smtp.host", mailServerHost);
		properties.put("mail.smtp.port", mailServerPort);
		properties.put("mail.smtp.auth", validate ? "true" : "false");
		return properties;
	}

	public String getMailServerHost() {
		return mailServerHost;
	}

	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}

	public String getMailServerPort() {
		return mailServerPort;
	}

	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
	}

	public boolean isValidate() {
		return validate;
	}

	public void setValidate(boolean validate) {
		this.validate = validate;
	}

	public String[] getAttachFileNames() {
		return attachFileNames;
	}

	public void setAttachFileNames(String fileNames[]) {
		attachFileNames = fileNames;
	}

	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String textContent) {
		content = textContent;
	}
}