邮箱服务器(三) 邮件基本格式与编码、复合邮件结构、整体架构代码

来源:互联网 发布:手机文件粉碎软件 编辑:程序博客网 时间:2024/05/17 09:28

1、 基本格式和编码
格式: RFC822邮件格式
mime编码: base64和quoted-printable(括上的可打印项目)
base64原理: 将一组连续的字节数据按6个bit位进行分组,然后对每组数据用一个ASCII字符表示。6个bit位最多能表示64(2*6)个数值,因此可以用64个ASCII字符来对应这64个字符。64个字符为: A~Za~z0~9+/
quoted-printable: 也是一种将二进制数据转换成可打印的ASCII字符的编码方式。它对ASCII字符不进行转换,只对非ASCII字符转换,每个非ASCII字符的字节数据,都被转换成一个”=”号后跟这个字节的十六进制数据

2、 复合邮件结构和整体架构代码
参考

3、 整体架构代码

import java.io.FileOutputStream;import java.io.OutputStream;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.activation.FileDataSource;import javax.mail.Session;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;public class Test3 {    public static void main(String[] args) throws Exception {        Properties props = new Properties();        props.setProperty("mail.transport.protocol", "smtp");        props.setProperty("mail.debug", "true");        props.setProperty("mail.smtp.host", "sina.com");        Session session = Session.getInstance(new Properties());        MimeMessage msg = new MimeMessage(session);        msg.setFrom(new InternetAddress("111@qq.com"));        msg.setSubject("hahhahahahha");        MimeMultipart msgMultipart = new MimeMultipart("mixed");        msg.setContent(msgMultipart);        MimeBodyPart attch1 = new MimeBodyPart();        MimeBodyPart attch2 = new MimeBodyPart();        MimeBodyPart content = new MimeBodyPart();        msgMultipart.addBodyPart(attch1);        msgMultipart.addBodyPart(attch2);        msgMultipart.addBodyPart(content);        DataSource ds1 = new FileDataSource("E://temp//111.txt");        DataHandler dh1 = new DataHandler(ds1);        attch1.setDataHandler(dh1);        // 附件名称,否则无法展示 如果有中文: setFileName(MimeUtility.encodeText("中文"))        attch1.setFileName("111.txt");        DataSource ds2 = new FileDataSource("E://temp//222.txt");        DataHandler dh2 = new DataHandler(ds2);        attch2.setDataHandler(dh2);        attch2.setFileName("222.txt");        MimeMultipart bodyMultipart = new MimeMultipart("related");        content.setContent(bodyMultipart);        MimeBodyPart htmlPart = new MimeBodyPart();        MimeBodyPart gifPart = new MimeBodyPart();        bodyMultipart.addBodyPart(htmlPart);        bodyMultipart.addBodyPart(gifPart);        DataSource gifds = new FileDataSource("E://temp//333.jpg");        DataHandler gifdh = new DataHandler(gifds );        gifPart.setDataHandler(gifdh );        // 必须与 img中的src的名称一致        gifPart.setHeader("Content-Location", "333.jpg");        // 必须用gbk        htmlPart.setContent("<imp style='width: 100px; height=100px;' src='333.jpg' />", "text/html;charset=GBK");        msg.saveChanges();        OutputStream os = new FileOutputStream("E://temp//444.eml");        msg.writeTo(os);        os.close();        InputStream in = new FileInputStream(new File("E://temp/444.eml"));        MimeMessage msg1 = new MimeMessage(session, in);        Transport.send(msg1, new Address[]{new InternetAddress("111@ww.com")});    }}