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

📄 mailservice.sql

📁 用来JAVA存储过程在ORACLE9I或者10G里面发邮件的例子
💻 SQL
字号:
create or replace and compile java source named MailService as
import java.util.*;
import java.io.*;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * @author junsansi
 * @qq 5454589
 * @website www.5ienet.com
 */
public class MailService {
    String from;
    String host;
    String subject;
    String to;
    Vector cc;
    Vector bcc;
    String priority;
    String replyTo;
    String userName;
    String password;
    Properties props;
    boolean smtpAuth;
    String mailBodyBegin, mailBodyEnd;
    String title = "";
    String filename;
    public MailService(String host) {
        this.host = host;
        this.init();
    }

    public MailService() {
        this.host = "localhost";
        this.init();
    }

    public void setTitle(String title) {
        this.title = title;
    }


    void init() {
        props = System.getProperties();
        props.put("mail.smtp.host", host);
        this.mailBodyBegin = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n" +
                             "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n" +
                             "<head>\r\n" +
                             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\r\n" +
                             "<title>junsansi.cn</title>\r\n" +
                             "</head>\r\n" +
                             "<body>\r\n";
        this.mailBodyEnd = "\r\n</body>\r\n" +
                           "</html>";

    }

    public void setTo(String to) {
        this.to = to;
    }

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

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

    public void setSmtpAuth(boolean smtpAuth) {
        if (smtpAuth) {
            props.put("mail.smtp.auth", "true");
        } else {
            props.put("mail.smtp.auth", "false");
        }
    }

    public void setReplyTo(String replyTo) {
        this.replyTo = replyTo;
    }

    public void addCC(String ccStr) {
        if (cc == null) {
            cc = new Vector(3);
        }
        cc.addElement(ccStr);
    }

    public void addBCC(String bccStr) {
        if (bcc == null) {
            bcc = new Vector(3);
        }
        bcc.addElement(bccStr);
    }

    public void setSubject(String subject) {
        this.subject = subject.replace('\n', ' ').replace('\r', ' ');
    }

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

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public void setCc(Vector cc) {
        this.cc = cc;
    }

    public void setBcc(Vector bcc) {
        this.bcc = bcc;
    }

    public void setMailBodyEnd(String mailBodyEnd) {
        this.mailBodyEnd = mailBodyEnd;
    }

    public void setMailBodyBegin(String mailBodyBegin) {
        this.mailBodyBegin = mailBodyBegin;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    // bypass invalid email addresses
    public boolean send(String content, int ishtml) {
        String mailer = "jss.mailRobot";
        // Get a Session object
        Session session = Session.getDefaultInstance(props, null);

        // construct the message
        Message msg = new MimeMessage(session);
        boolean ok = false;
        try {
            msg.setFrom(new InternetAddress(from));
            Address[] mailto = null;
            try {
                mailto = InternetAddress.parse(to, false);
            } catch (Exception e) {
                System.out.println(to + "地址错误,无法发送");
                return false;
            }
            if (mailto == null) {
                return false;
            }
            msg.setRecipients(Message.RecipientType.TO, mailto);

            if (cc != null) {
                InternetAddress[] ia = new InternetAddress[cc.size()];
                for (int i = 0; i < cc.size(); i++) {
                    String s = (String) cc.elementAt(i);
                    ia[i] = new InternetAddress(s);
                }
                try {
                    msg.setRecipients(Message.RecipientType.CC, ia);
                } catch (Exception me) {
                    //me.printStackTrace();
                }
            }

            if (bcc != null) {
                InternetAddress[] ia = new InternetAddress[bcc.size()];
                for (int i = 0; i < bcc.size(); i++) {
                    String s = (String) bcc.get(i);
                    ia[i] = new InternetAddress(s);
                }

                try {
                    msg.setRecipients(Message.RecipientType.BCC, ia);
                } catch (Exception me) {
                    //me.printStackTrace();
                }
            }

            if (replyTo != null) {
                InternetAddress[] addrs = new InternetAddress[1];
                try {
                    addrs[0] = new InternetAddress(replyTo, false);
                    msg.setReplyTo(addrs);
                } catch (Exception me) {
                    //me.printStackTrace();
                }
            }

            msg.setSubject(subject);
            if (ishtml == 1) {
                msg.setDataHandler(new DataHandler(new ByteArrayDataSource(
                        new String((this.getMailBodyBegin() + content +
                                    this.getMailBodyEnd()).getBytes("GBK"),
                                   "ISO8859_1"),
                        "text/html")));
            } else {
                msg.setText(content);
            }
            if (filename != null) {
                MimeBodyPart mbpBody = new MimeBodyPart();
                if (ishtml == 1) {
                    mbpBody.setDataHandler(new DataHandler(new
                            ByteArrayDataSource(
                                    new String((this.getMailBodyBegin() +
                                                content + this.getMailBodyEnd()).
                                               getBytes("GBK"), "ISO8859_1"),
                                    "text/html")));
                } else {
                     mbpBody.setText(content);
                }

                MimeBodyPart mbpFile = new MimeBodyPart();

                // attach the file to the message
                FileDataSource fds = new FileDataSource(filename);
                mbpFile.setDataHandler(new DataHandler(fds));
                mbpFile.setFileName(fds.getName());

                // create the Multipart and its parts to it
                Multipart mp = new MimeMultipart();
                mp.addBodyPart(mbpBody);
                mp.addBodyPart(mbpFile);

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

            if (priority != null) {
                msg.setHeader("X-Priority", priority);
            } else {
                msg.setHeader("X-Mailer", mailer);
            }
            msg.setSentDate(new Date());

            Session mailSession = Session.getInstance(props, null);
            Transport transport = mailSession.getTransport("smtp");
            try {
                transport.connect(host, 25, userName,
                                  password);
                transport.sendMessage(msg,
                                      msg.getRecipients(Message.RecipientType.TO));
                if (cc != null) {
                    transport.sendMessage(msg,
                                          msg.getRecipients(Message.
                            RecipientType.CC));
                }
                if (bcc != null) {
                    transport.sendMessage(msg,
                                          msg.getRecipients(Message.
                            RecipientType.BCC));
                }           
                ok = true;
            } catch (Exception ee) {
                ok = false;
                ee.printStackTrace();
            } finally {
                transport.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
        }
        /*
                 if (ok) {
            System.out.println(title + " mail to:" + to);
                 } else {
            System.out.println(title + " error to:" + to);
                 }
         */
        return ok;
    }

    public String getFrom() {
        return from;
    }

    public Vector getCc() {
        return cc;
    }

    public Vector getBcc() {
        return bcc;
    }

    public String getPassword() {
        return password;
    }

    public String getPriority() {
        return priority;
    }

    public String getReplyTo() {
        return replyTo;
    }

    public boolean isSmtpAuth() {
        return smtpAuth;
    }

    public String getSubject() {
        return subject;
    }

    public String getTitle() {
        return title;
    }

    public String getTo() {
        return to;
    }

    public String getUserName() {
        return userName;
    }

    public String getMailBodyEnd() {
        return mailBodyEnd;
    }

    public String getMailBodyBegin() {
        return mailBodyBegin;
    }

    public String getFilename() {
        return filename;
    }
}

⌨️ 快捷键说明

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