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

📄 smtpserverbean.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
/**
 * 
 * SMTPServerBean.java
 * 
 * Created on 2003-8-18 15:43:24
 * 
 */
package com.liuyang.jboss.sessionbean.smtp.ejb;

import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author
 * <a href="mailto:jdcyang@yahoo.com.cn">刘洋</a>
 */
public class SMTPServerBean implements SessionBean{

	/**
	 * @param host
	 */
	public void setHost(String host){
		SMTPHost = host;
	}
	/**
	 * @param user
	 * @param pass
	 */
	public void setUser(String user,String pass){
		Username = user;
		Password = pass; 
	}	
	private SessionContext ctx;
	public String SMTPHost = "";
	public String Username = "";
	public String Password = "";
	
	/**
	 * 
	 * @ejb.interface-method view-type = "remote"
	 */
	public boolean sentMail(String receiver,String subject, String content) {
		if(SMTPHost.equalsIgnoreCase("")||Username.equalsIgnoreCase("")||Password.equalsIgnoreCase(""))
		return false;	
		boolean success = true;
		Properties props = System.getProperties();	
		props.put("mail.smtp.host",SMTPHost);	
		props.put("mail.smtp.auth", "true");
		PAAuthenticator pa = new PAAuthenticator(Username,Password);
		Session sess = Session.getInstance(props,pa);
		sess.setDebug(false);
		MimeMessage msg = new MimeMessage(sess);
		try {
			msg.setFrom(new InternetAddress(this.Username+"@"+this.SMTPHost));
			if(receiver.equalsIgnoreCase(""))return false;
			msg.setRecipient(Message.RecipientType.TO,new InternetAddress(receiver));
			msg.setSubject(subject);
			msg.setText(content);
			Transport.send(msg);				
		} catch (AddressException e) {
			e.printStackTrace();
			success = false;
		} catch (MessagingException e) {
			e.printStackTrace();
			success = false;
		}
		return success;		
	}
	
	/**
	 * @ejb.create-method 
	 */
	public void ejbCreate () {
	}
	public void ejbActivate() throws EJBException, RemoteException {
	}
	public void ejbPassivate() throws EJBException, RemoteException {
	}

	public void ejbRemove() throws EJBException, RemoteException {
	}
	public void setSessionContext(SessionContext sc) throws EJBException, RemoteException {
		ctx = sc;
	}
}

⌨️ 快捷键说明

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