欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

JAVA成鬼之JAVA MAIL,

来源: javaer 分享于  点击 38018 次 点评:256

JAVA成鬼之JAVA MAIL,


JAVA Mail

  • show me your code
    • 简易邮件
    • 带附件邮件
    • 图文邮件(邮件正文包含图片)

show me your code

java mail +腾讯邮箱

简易邮件

public static void main(String[] args) throws GeneralSecurityException {
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        // 使用ssl登录必需字段
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        try {
            msg.setSubject("主题");
            Transport transport = session.getTransport();
            
            // password 为授权码
            transport.connect("smtp.qq.com", "xxxx@qq.com", "password");
            
			// 邮件xxxx发给yyyy
            transport.sendMessage(msg, new Address[] { new InternetAddress("yyyyy@qq.com") });
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

带附件邮件

MimeMultipart 包裹附件MimeBodyPart

 public static void main(String[] args) throws GeneralSecurityException {
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        try {
            msg.setSubject("主题");
            msg.setText("test");
            msg.setFrom(new InternetAddress("xxxx@qq.com"));
            
            // 主内容
            MimeMultipart mm = new MimeMultipart();

            // 添加简易附件
            MimeBodyPart singleAttachment = new MimeBodyPart();
            // 读取本地文件
            DataHandler dh2 = new DataHandler(new FileDataSource("C:\\Users\\zorpz\\Desktop\\201901.png"));
            // 将附件数据添加到"节点"
            singleAttachment.setDataHandler(dh2);
            // 设置附件的文件名(需要编码)
            singleAttachment.setFileName(MimeUtility.encodeText(dh2.getName()));
			// 如果有多个附件,可以创建多个多次添加
            mm.addBodyPart(singleAttachment);     
            msg.setContent(mm);

            Transport transport = session.getTransport();
            transport.connect("smtp.qq.com", "xxxx@qq.com", "password");

            transport.sendMessage(msg, new Address[] { new InternetAddress("xxxx@qq.com") });
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}

图文邮件(邮件正文包含图片)

把图片做附件上传并设置cid与正文关联

public static void main(String[] args) throws GeneralSecurityException {
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        try {
            msg.setSubject("主题");
            
            msg.setText("test");
            msg.setFrom(new InternetAddress("774797836@qq.com"));

            // 主内容
            MimeMultipart mm = new MimeMultipart();

           // 添加图文附件
            String path = "C:\\Users\\zorpz\\Desktop\\go.png";
            File file = new File(path);
            String strFileName = file.getName();
            MimeBodyPart mimebodypart = new MimeBodyPart();
            mimebodypart.setDataHandler(new DataHandler(new FileDataSource(
                    path)));
            mimebodypart.setFileName(MimeUtility.encodeText(strFileName));
            mimebodypart.setHeader("Content-ID", "test");

            mm.addBodyPart(mimebodypart);

            String content = "这是一张图片<br/><a href='http://www.baidu.com'><img src='cid:test'/></a>" ;
            MimeBodyPart mimebodypart1 = new MimeBodyPart();
            mimebodypart1.setDataHandler(new DataHandler(content,
                    "text/html;charset=GBK"));
            mm.addBodyPart(mimebodypart1);

            msg.setContent(mm);

            Transport transport = session.getTransport();
            transport.connect("smtp.qq.com", "xxxx@qq.com", "xxxxx");

            transport.sendMessage(msg, new Address[] { new InternetAddress("xxxx@qq.com") });
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}

相关文章

    暂无相关文章
相关栏目:

用户点评