• 用java发邮件之一 (直接源于真实项目) 【原】


    真实项目应用的java发送邮件,应该还待进一步完善。

    依赖 mail-1.4.jar

    jar包下载地址: http://mvnrepository.com/artifact/javax.mail/mail/1.4.7

    import javax.mail.BodyPart;
    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.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;

    Mail.java

    package com.testdemo.tool.zsmail;
    
    import java.io.Serializable;
    
    /**
     * Mail属性实体
     * 
     * @author shadow
     * 
     */
    @SuppressWarnings("serial")
    public class Mail implements Serializable {
    
        public static final String ENCODEING = "UTF-8";
    
        private String sender;// ProperUtil.get("mail_account"); // 发件人的邮箱
    
        private String name; // 发件人昵称
    
        private String subject; // 主题
    
        private String message; // 信息(支持HTML)
    
        private String[] receivers; // 收件人的邮箱
    
        public String getSender() {
            return sender;
        }
    
        public void setSender(String sender) {
            this.sender = sender;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSubject() {
            return subject;
        }
    
        /**
         * 设置主题
         * 
         * @param subject
         */
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public String getMessage() {
            return message;
        }
    
        /**
         * 设置发送消息
         * 
         * @param message
         */
        public void setMessage(String message) {
            this.message = message;
        }
    
        public String[] getReceivers() {
            return receivers;
        }
    
        /**
         * 设置接收者
         * 
         * @param receivers
         */
        public void setReceivers(String[] receivers) {
            this.receivers = receivers;
        }
    
    }

    MailTool.java

    package com.testdemo.tool.zsmail;
    
    import java.util.Date;
    import java.util.Properties;
    
    import javax.mail.BodyPart;
    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.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    
    import java.util.HashMap;
    
    
    public class MailTool {
        
        private static Properties prop = new Properties();
        private static Session session = null;
    
        static {
            prop.put("mail.smtp.auth", "true");
            prop.put("mail.smtp.host", "smtp.qq.com");//邮件服务器
            prop.put("mail.smtp.port", "25");
            prop.put("mail.transport.protocol", "smtp");
            prop.put("mail_account", "");//qq帐号
            prop.put("mail_password", "");//qq密码
    
            if (null == session)
                session = Session.getInstance(prop);
        }
    
        public static synchronized HashMap<String, Object> sendMail(Mail mail) {
            String error = "";
            boolean b = false;
            Message msg = null;
            BodyPart messageBodyPart = null;
            Multipart multipart = null;
            Transport tran = null;
            try {
                msg = new MimeMessage(session); // 建立信息
                messageBodyPart = new MimeBodyPart();
                multipart = new MimeMultipart();
                msg.setFrom(new InternetAddress(prop.getProperty("mail_account"))); // 发件人
                InternetAddress[] receivers = InternetAddress.parse(getMailList(mail.getReceivers()));
                msg.setRecipients(Message.RecipientType.TO, receivers); // 收件人
                msg.setSentDate(new Date()); // 发送日期
                msg.setSubject(mail.getSubject()); // 主题
                msg.setText(mail.getMessage()); // 内容
                // 显示以html格式的文本内容
                messageBodyPart.setContent(mail.getMessage(), "text/html;charset=UTF-8");
                multipart.addBodyPart(messageBodyPart);
                msg.setContent(multipart);
                tran = session.getTransport("smtp");
                tran.connect(prop.getProperty("mail.smtp.host"), prop.getProperty("mail_account"), prop.getProperty("mail_password"));
                tran.sendMessage(msg, msg.getAllRecipients()); // 发送
                b = true;
            } catch (Exception e) {
                e.printStackTrace();
                b = false;
                error = e.getMessage();
            } finally {
                System.out.println("邮件发送" + (b == true ? "成功" : "失败"));
                try {
                    if (null != tran)
                        tran.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("status", b);
            map.put("error", error);
            return map;
        }
    
        public static String getMailList(String[] mailArray) {
            StringBuffer toList = new StringBuffer();
            int length = mailArray.length;
            if (mailArray != null && length < 2) {
                toList.append(mailArray[0]);
            } else {
                for (int i = 0; i < length; i++) {
                    toList.append(mailArray[i]);
                    if (i != (length - 1)) {
                        toList.append(",");
                    }
                }
            }
            return toList.toString();
        }
    
        public static void help(){
            System.out.println(
            "public static void main(String[] args) {
    "+
            "	Mail mail = new Mail();//基本对象
    "+
            "	mail.setReceivers(new String[] { "1713930654@qq.com", "xiao@163.com" });//接收者数组
    "+
            "	mail.setSubject("subject");//主题
    "+
            "	mail.setMessage("message");//内容
    "+
            "	sendMail(mail);//发送");
    "+
            "}");
        }
        
        public static void main(String[] args) {
    //        Mail mail = new Mail();//基本对象
    //        mail.setReceivers(new String[] { "1713930654@qq.com", "389522975@qq.com" });//接收者数组
    //        mail.setSubject("subject");//主题
    //        mail.setMessage("message");//内容
    //        sendMail(mail);//发送
            help();
        }
    }

    其它

    java mail(发送邮件--163邮箱)

    javamail 发送邮件失败(被网易禁止)解决办法

    开启 SSL 加密

    抄送

  • 相关阅读:
    关于windows线程的各种状态
    《深入理解计算机系统》(第二版)第二章练习题3
    《深入理解计算机系统》(第二版)第二章中的一练习题2
    Linux进程/线程模型
    用户进程中执行的操作系统
    关于操作系统模型
    《深入理解计算机系统》(第二版)第二章中的一题目
    并发问题互斥(Dekker算法和Peterson算法)
    概率问题:星期二出生的孩子
    Yii添加扩展加载Ckeditor 4.0以上版本
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/6375053.html
Copyright © 2020-2023  润新知