zoukankan      html  css  js  c++  java
  • java程序发送内嵌图片、带附件邮件

    package mail;

    import java.io.UnsupportedEncodingException;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    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;

    public class SendMail2 {

    /**
    * @param args
    * @throws MessagingException
    * @throws AddressException
    * @throws UnsupportedEncodingException
    */
    public static void main(String[] args) throws AddressException, MessagingException, UnsupportedEncodingException {
    Properties props = new Properties();//环境变量设置。
    props.setProperty("mail.transport.protocol", "smtp");//发送使用的协议
    props.setProperty("mail.host", "smtp.163.com");//发送服务器的主机地址
    props.setProperty("mail.smtp.auth", "true");//请求身份验证
    Session session = Session.getDefaultInstance(props,new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("zsz19901116@163.com","你猜?");
    }
    });
    MimeMessage message = new MimeMessage(session);//代表一封邮件
    message.setFrom(new InternetAddress("zsz19901116@163.com"));//设置发件人
    message.setRecipients(Message.RecipientType.TO, "zsz19901110@163.com");//设置收件人
    message.setSubject("这是一封文本中内嵌图片的邮件");//设置主题
    MimeBodyPart textPart=new MimeBodyPart();
    textPart.setContent("美女bbb<img src='cid:mm'/>bbb", "text/html;charset=UTF-8");
    //内嵌图片部分
    MimeBodyPart imagePart = new MimeBodyPart();
    DataHandler dh = new DataHandler(new FileDataSource("c:/1.jpg"));
    imagePart.setDataHandler(dh);
    imagePart.setContentID("mm");
    //文本和内嵌图片合体
    MimeMultipart mpart1 = new MimeMultipart();
    mpart1.addBodyPart(textPart);
    mpart1.addBodyPart(imagePart);
    mpart1.setSubType("related");
    MimeBodyPart textImagePart = new MimeBodyPart();
    textImagePart.setContent(mpart1);

    MimeBodyPart attachmentPart = new MimeBodyPart();
    dh = new DataHandler(new FileDataSource("c:/附件.zip"));
    String filename = dh.getName();//获取文件名
    System.out.println(filename);
    attachmentPart.setDataHandler(dh);
    attachmentPart.setFileName(MimeUtility.encodeText(filename));//手工设置文件名.中文文件名要注意编码


    MimeMultipart mpart2 = new MimeMultipart();
    mpart2.addBodyPart(textImagePart);//文本加内嵌图片
    mpart2.addBodyPart(attachmentPart);//附件
    mpart2.setSubType("mixed");//复杂关系
    message.setContent(mpart2);

    //发送邮件
    // Transport ts = session.getTransport();
    // ts.connect("zsz19901116@163.com","你猜猜?");//连接
    // ts.sendMessage(message, message.getAllRecipients());
    // ts.close();
    Transport.send(message);
    }

    }

  • 相关阅读:
    CSharpGL(36)通用的非托管数组排序方法
    CSharpGL(35)用ViewPort实现类似3DMax那样的把一个场景渲染到4个视口
    CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL
    CSharpGL(33)使用uniform块来优化对uniform变量的读写
    CSharpGL(32)矩阵与四元数与角度旋转轴的相互转换
    CSharpGL(31)[译]OpenGL渲染管道那些事
    CSharpGL(30)用条件渲染(Conditional Rendering)来提升OpenGL的渲染效率
    Go如何使用数据库、缓存
    Go内置常用包
    从零开始基于go-thrift创建一个RPC服务
  • 原文地址:https://www.cnblogs.com/zszitman/p/4436353.html
Copyright © 2011-2022 走看看