⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailerbean.java

📁 simple sending email by java. must have mail.jar first if do not.
💻 JAVA
字号:
package com.stardeveloper.bean.test;

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;

public final class MailerBean extends Object implements Serializable {
	
	/* Bean Properties */
	private String to = null;
	private String from = null;
	private String subject = null;
	private String message = null;
	public static Properties props = null;
	public static Session session = null;

	static {
		/*	Setting Properties for STMP host */
		props = System.getProperties();
		props.put("mail.smtp.host", "mail.yourisp.com");
		session = Session.getDefaultInstance(props, null);
	}
	/* Setter Methods */
	public void setTo(String to) {
		this.to = to;
	}

	public void setFrom(String from) {
		this.from = from;
	}

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

	public void setMessage(String message) {
		this.message = message;
	}
	/* Sends Email */
	public void sendMail() throws Exception {
		if(!this.everythingIsSet())
			throw new Exception("Could not send email.");
		try {
			MimeMessage message = new MimeMessage(session);
			message.setRecipient(Message.RecipientType.TO, 
				new InternetAddress(this.to));
			message.setFrom(new InternetAddress(this.from));
			message.setSubject(this.subject);
			message.setText(this.message);
			Transport.send(message);
		} catch (MessagingException e) {
			throw new Exception(e.getMessage());
		}
	}

	/* Checks whether all properties have been set or not */
	private boolean everythingIsSet() {
		if((this.to == null) || (this.from == null) || 
		   (this.subject == null) || (this.message == null))
			return false;

		if((this.to.indexOf("@") == -1) ||
			(this.to.indexOf(".") == -1))
			return false;

		if((this.from.indexOf("@") == -1) ||
			(this.from.indexOf(".") == -1))
			return false;

		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -