BASE64编码和解码---JavaMail发邮件

Base64加密事后的字符串html

要输入的东西也必须是Base64加密事后的字符串java

在sun.misc.BASE64Encoder包下,在JDK中有,这个类不该该咱们来用,这个包是java和javax包的底层依赖服务器

 在window/Preferences下进行配置session

 使用BASE64进行编码和解码:---不用导jar包ide

package com.xjs.base64;

import java.io.IOException;


import org.junit.Test;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class Demo1 {
    
    @Test
    public void fun1() throws IOException{
        //BASE64编码
        String s="13271315317";
        BASE64Encoder encoder=new BASE64Encoder(); 
        String str = encoder.encode(s.getBytes("utf-8"));
        System.out.println(str);
        
        //BASE64解码
        BASE64Decoder decoder=new BASE64Decoder(); 
        byte[] bytes = decoder.decodeBuffer(str);
        System.out.println(new String(bytes,"UTF-8"));
    }
}

 


 

JavaMail

在JavaEE的文档上测试

1.导包!编码

  *  mail.jar加密

  *  activation.jarspa

核心类:code

1.Session

  * 若是你获得了它,表示已经与服务器链接上了,与Connection的做用类似!

  获得Session,须要使用Session.getInstance(Properties,Authenticator);

  Properties props=new Properties();

  props.setProperty("mail.host", "smtp.163.com");

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

  

  Authenticator auth = new Authenticator() {

    protected PasswordAuthentication getPasswordAuthentication() {

      return new PasswordAuthentication("用户名",  "密码");

    }

  }

  Session session = Session.getInstance(props, auth);

2.MimeMessage

  * 它表示一个邮件对象,你能够调用它的setFrom(),设置发件人、设置收件人、设置主题、设置正文!

3.TransPort。

  * 它只有一个功能,发邮件。

package com.xjs.javamail;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.junit.Test;


public class Demo1 {
    @Test
    public void fun1() throws AddressException, MessagingException {
        /*
         * 1.获得Session
         */
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("13271315317", "邮箱的登陆受权码");
            }
        };

        Session session = Session.getInstance(props, auth);
        /*
         * 2.建立MimMessage
         */
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("13271315317@163.com"));// 设置发件人
        msg.setRecipients(RecipientType.BCC, "1820494894@qq.com");// 设置收件人
        msg.setRecipients(RecipientType.BCC, "1874704478@qq.com");
        /*
         * msg.setRecipients(RecipientType.CC, "itcast_cxf@126.com");//设置抄送
         * msg.setRecipients(RecipientType.BCC, "itcast_cxf@126.com");//设置暗送
         */
        msg.setSubject("这是来自谢军帅的测试邮件");
        msg.setContent("谢军帅最帅,谢军帅最帅,谢军帅最帅,谢军帅最帅!!!", "text/html;charset=utf-8");
        /*
         * 3.发
         */
        Transport.send(msg);
    }

    /**
     * 带有附件的邮件
     * 
     * @throws Exception
     */

    @Test
    public void fun2() throws Exception {
        /*
         * 1.获得Session
         */
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("13271315317", "邮箱的登陆受权码");
            }
        };

        Session session = Session.getInstance(props, auth);
        /*
         * 2.建立MimMessage
         */
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("13271315317@163.com"));// 设置发件人
        msg.setRecipients(RecipientType.BCC, "1874704478@qq.com");

        msg.setSubject("这是来自谢军帅的测试邮件--有附件");
        // ////////////////////////////////////////
        /*
         * 当发送包含附件的邮件时,邮件体就为多部件形式! 1.建立一个多部件的部件内容!MimeMultipart
         * MimeMultipart就是一个集合,用来装载多个主体部件! 2.咱们须要建立两个主体部件,一个是文本内容的,另外一个是附件的。
         * 主体部件叫MimeBodyPart 3.把MimeMultipart设置给MimeMessage的内容!
         */

        MimeMultipart list = new MimeMultipart();// 建立多部份内容

        // 建立MimeBodyPart
        MimeBodyPart part1 = new MimeBodyPart();
        // 设置主体部件的内容
        part1.setContent("这是一份包含附件的邮件,,,", "text/html;charset=utf-8");
        // 把主体部件添加到集合中
        list.addBodyPart(part1);

        
        //建立MimeBodyPart
        MimeBodyPart part2 = new MimeBodyPart();
        part2.attachFile(new File("G:/金软软.jpg"));//设置附件的内容
        part2.setFileName(MimeUtility.encodeText("我喜欢的金歌手.jpg"));//设置显示的文件名称,其中encodeText处理中文乱码问题
        list.addBodyPart(part2);
        
        msg.setContent(list);// 把它设置给邮件做为邮件的内容

        // ////////////////////////////////////////
        /*
         * 3.发
         */
        Transport.send(msg);
    }
}
相关文章
相关标签/搜索