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

📄 sendmail.java

📁 关于 Jaoso新闻文章发布系统 --- --- --- --- --- --- --- --- --- --- --- --- --- -- 版本信息:Jaoso新闻文章发布系统 0.9.1b
💻 JAVA
字号:
package jaoso.framework.common;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


/**
 * A simple util class that sends out emails
 * @author David Xu
 * @author Charles Huang
 * @version 1.1
 */
public class SendMail {

    //~ Methods ================================================================

    /**
     * DOCUMENT ME!
     *
     * @param args DOCUMENT ME!
     */
    public static void main(String[] args) {

        try {

            SendMail.send("edgeloner@163.com", "pp968admin@sina.com",
                "Notification",
                "<html><img src='http://www.chinaxp.org/forum/images/logo.gif'><b>test</b></html>");
            System.out.println("send mail success!");
        } catch (final Exception exception) {

            exception.printStackTrace();
        }
    }

    /**
     * "send" method to send the message.
     * @param to   To e-mail address.
     * @param from From e-mail address,optional.If not provided, will use "redsoft@chinaxp.org".
     * @param subject Subject of the mail
     * @param body Mail body
     * @exception Exception if to e-mail address is empty
     */
    public static void send(final String to, final String from,
        final String subject, final String body)
        throws javax.mail.MessagingException, 
            javax.mail.internet.AddressException {

        //read the hardcoded values from propertiy file
        //Validation.validateNotNull( to );
        //Validation.validateNotNull( from );
        //Validation.validateNotNull( subject );
        // Validation.validateNotNull( body );
        //final String smtpServer="www.chinaxp.org";
        final String smtpServer = "smtp.sina.com.cn";
        final String user = "pp968admin";
        final String password = "6803533";

        final Properties props = new Properties();
        props.put("mail.smtp.host", smtpServer);

        //add smtp Authenticator
        props.put("mail.smtp.auth", "true");

        final Session session = Session.getDefaultInstance(props, null);
        session.setDebug(false);

        final Transport tr = session.getTransport("smtp");
        tr.connect(smtpServer, user, password);

        MimeMessage msg = new MimeMessage(session);
        final MimeBodyPart mbp1 = new MimeBodyPart();
        final MimeMultipart mp = new MimeMultipart();
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject(subject, "GB2312");
        mbp1.setText(body, "GB2312");
        mp.addBodyPart(mbp1);
        msg.setContent(mp);
        msg.setSentDate(new java.util.Date());
        msg.saveChanges(); // don't forget this

        //Transport.send(msg);change to use intance not use static method send()
        tr.sendMessage(msg, msg.getAllRecipients());
        tr.close();
    }

    //~ Inner Classes ==========================================================

    /**
     * DOCUMENT ME!
     *
     * @author $author$
     * @version $Revision$
     */
    class XAuthenticator extends Authenticator {

        //~ Instance fields ====================================================

        /**  DOCUMENT ME! */
        private String password;

        /**  DOCUMENT ME! */
        private String user;

        //~ Constructors =======================================================

        /**
         * Creates a new XAuthenticator object.
         *
         * @param user DOCUMENT ME!
         * @param password DOCUMENT ME!
         */
        public XAuthenticator(final String user, final String password) {

            // Validation.validateNotNull( user );
            //  Validation.validateNotNull( password );
        }

        //~ Methods ============================================================

        /**
         * DOCUMENT ME!
         *
         * @return DOCUMENT ME!
         */
        public PasswordAuthentication getPasswordAuthentication() {

            return new PasswordAuthentication(user, password);
        }
    }
}

⌨️ 快捷键说明

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