使用Java Mail发送邮件

    xiaoxiao2025-11-11  6

    /**

     * 邮件发送类,使用Builder来发送

     * 需要使用的jar包javax.mail.jar

     * https://java.net/downloads/javamail/

     * 

     * @author syh

     * 

     */

    public class EmailBuilder {

        String to = "";

        String from = "";

        String host = "";

        String username = "";

        String password = "";

        String filename = "";

        String title = "";

        String content = "";

        Vector<String> file = new Vector<String>();

        public EmailBuilder() {

        }

        /**

         * 设置smtp地址

         * 

         * @param host

         */

        public EmailBuilder setHost(String host) {

            this.host = host;

            return this;

        }

        /**

         * 设置密码

         * 

         * @param pwd

         */

        public EmailBuilder setPassWord(String pwd) {

            this.password = pwd;

            return this;

        }

        /**

         * 设置账户

         * 

         * @param usn

         */

        public EmailBuilder setUserName(String usn) {

            this.username = usn;

            return this;

        }

        /**

         * 设置收件人

         * 

         * @param to

         */

        public EmailBuilder setTo(String to) {

            this.to = to;

            return this;

        }

        /**

         * 设置发件人

         * 

         * @param from

         */

        public EmailBuilder setFrom(String from) {

            this.from = from;

            return this;

        }

        /**

         * 设置标题

         * 

         * @param title

         */

        public EmailBuilder setTitle(String title) {

            this.title = title;

            return this;

        }

        /**

         * 设置内容

         * 

         * @param content

         */

        public EmailBuilder setContent(String content) {

            this.content = content;

            return this;

        }

        /**

         * 添加附件

         * 

         * @param fname

         */

        public EmailBuilder attachfile(String fname) {

            file.addElement(fname);

            return this;

        }

        private CallBack mCallback;

        /**

         * 设置回调

         * 

         * @param fname

         * @return

         */

        public EmailBuilder sendCallback(CallBack callback) {

            this.mCallback = callback;

            return this;

        }

        /**

         * 发送邮件

         * 

         * @return

         */

        public boolean sendMail() {

            Properties props = System.getProperties();

            props.put("mail.smtp.host", host);

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

            Session session = Session.getDefaultInstance(props,

                    new Authenticator() {

                        public PasswordAuthentication getPasswordAuthentication() {

                            return new PasswordAuthentication(username, password);

                        }

                    });

            boolean result = false;

            try {

                MimeMessage msg = new MimeMessage(session);

                msg.setFrom(new InternetAddress(from));

                InternetAddress[] address = { new InternetAddress(to) };

                msg.setRecipients(Message.RecipientType.TO, address);

                msg.setSubject(title);

                Multipart mp = new MimeMultipart();

                MimeBodyPart mbpContent = new MimeBodyPart();

                mbpContent.setText(content);

                mp.addBodyPart(mbpContent);

                Enumeration<String> efile = file.elements();

                while (efile.hasMoreElements()) {

                    MimeBodyPart mbpFile = new MimeBodyPart();

                    filename = efile.nextElement().toString();

                    FileDataSource fds = new FileDataSource(filename);

                    mbpFile.setDataHandler(new DataHandler(fds));

                    mbpFile.setFileName(fds.getName());

                    mp.addBodyPart(mbpFile);

                }

                file.removeAllElements();

                msg.setContent(mp);

                msg.setSentDate(new Date());

                Transport.send(msg);

                result = true;

            } catch (MessagingException mex) {

                mex.printStackTrace();

                Exception ex = null;

                if ((ex = mex.getNextException()) != null) {

                    ex.printStackTrace();

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

            if (mCallback != null) {

                mCallback.onCompleted(result);

            }

            return result;

        }

        /**

         * 测试

         * @param args

         */

        public static void main(String[] args) {

            EmailBuilder sendmail = new EmailBuilder();

            sendmail.setHost("smtp.qq.com")

            .setUserName("327867994")

            .setPassWord("******")

            .setFrom("327867994@qq.com")

            .setTo("shenyanghong@staff.ourpalm.com")

            .setTitle("标题")

            .setContent("内容 测试")

            .attachfile("/home/syh/Pictures/4a0665b2d2fed1e216f7da8875453c84.jpg");

            boolean result = sendmail.sendMail();

            System.out.println("result:" + result);

        }

        /**

         * 发送回调

         * 

         * @author syh

         * 

         */

        public static interface CallBack {

            public void onCompleted(boolean success);

        }

    }

    (2013-12-20 写于百度空间)
    转载请注明原文地址: https://ju.6miu.com/read-1304073.html
    最新回复(0)