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

📄 mailservicebean.java

📁 Oracle的J2EE Sample
💻 JAVA
字号:
/*
 * @author  : Elangovan
 * @version : 1.0
 *
 * Development Environment : Oracle9i JDeveloper
 * Name of the File        : MailServiceBean.java
 *
 * Creation / Modification History
 *    Elangovan           26-Apr-2002        Created
 *
 */
package oracle.otnsamples.ibfbs.admin.ejb;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

// Packages for Java Mail API
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeBodyPart;

import java.util.Date;

/**
 * This class is the implementation of the Mail Service. Connects to the mail server
 * and sends mail. Clients use the remote interface(Exchange.java) to invoke methods
 * on this bean.
 *
 * @see MailService.java
 * @see MailServiceHome.java
 */
public class MailServiceBean implements SessionBean {

  /**
   * This method connects to the Mail server and sends mail.
   * The mail server address is taken from the System Property,
   * which is set when the application is initialized.
   *
   * @param toAddress Mail address of the recipient
   * @param fromAddress Mail address of the sender
   * @param subject Subject of the message
   * @param message mail body
   * @return status of the mail, 0 - sent , -1 - send failed
   * @since 1.0
   */
  public Integer sendMail(String toAddress, String fromAddress, String subject,
                          String message) {

    int status = -1;

    if(toAddress == null || fromAddress == null || message== null) 
      return new Integer(status);

    // Get the default Session using System properties 
    Session session = Session.getDefaultInstance(System.getProperties(), null);

    // Disable the debug mode
    session.setDebug(false);

    // Create a New message
    MimeMessage msg = new MimeMessage(session);

    // Check if it is a valid email-id
    if(fromAddress.indexOf("@") != -1 && toAddress.indexOf("@") != -1 &&
       fromAddress.indexOf(".") != -1 && toAddress.indexOf(".") != -1 ) {
    
      try {

        // Set the From address
        msg.setFrom(new InternetAddress(fromAddress));

        // Setting the "To recipients" addresses
        msg.setRecipients(Message.RecipientType.TO,
                          InternetAddress.parse(toAddress, false));

        // Set mail subject
        msg.setSubject(subject);

        // Create and fill the first message part
        MimeBodyPart mbp = new MimeBodyPart();
        mbp.setContent(message, "text/html");

        // Create the Multipart and its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp);

        // Add the Multipart to the message
        msg.setContent(mp);

        // Set the Date: header
        msg.setSentDate(new Date());

        // Send the message
        Transport.send(msg);

        status = 0;
      
      } catch (MessagingException msgEx) {  // Messaging errors
        System.err.println("MailServiceBean.sendMail: Error sending mail : " +
                             msgEx.toString());
      
      } catch (Exception ex) {              // Trap other errors
        System.err.println("MailServiceBean.sendMail: Generic Error sending mail : " +
                            ex.toString());
      }

    }
    
    return new Integer(status);
  }

  // Standard call back methods

  public void ejbCreate() {}

  public void ejbActivate() {}

  public void ejbPassivate() {}

  public void ejbRemove() {}

  public void setSessionContext(SessionContext ctx) {}
  
}

⌨️ 快捷键说明

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