zoukankan      html  css  js  c++  java
  • java mail 邮箱发送

    引用自http://www.cnblogs.com/xdp-gacl/p/4220190.html

    package com.longcai.simple;

    import java.io.FileOutputStream;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.NoSuchProviderException;
    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.MimePart;
    import javax.swing.plaf.basic.BasicDirectoryModel;

    public class SendImaMail {
        public static void main(String[] args) throws Exception {
            Properties properties=new Properties();
            properties.setProperty("mail.host", "smtp.sohu.com");
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth","true");
            
            Session session=Session.getInstance(properties);
            session.setDebug(true);
            
            Transport ts=session.getTransport();
            ts.connect("smtp.sohu.com", "sohu588168", "dzj8013168");
            
             Message message = createMixedMail(session);
             ts.sendMessage(message, message.getAllRecipients());
             ts.close();
        }
        //图片和附件一起发送
        private static Message createMixedMail(Session session) throws Exception {
            MimeMessage message=new MimeMessage(session);
            message.setFrom(new InternetAddress("sohu588168@sohu.com"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
            message.setSubject("带图片的邮件");
            
            MimeBodyPart text=new MimeBodyPart();
            text.setContent("xxx<img src='cid:aaa.jpg'/>","text/html;charset=utf-8");
            
            MimeBodyPart img=new MimeBodyPart();
            img.setDataHandler(new DataHandler(new FileDataSource("C:\Users\Administrator\Desktop\444.jpg")));
            img.setContentID("aaa.jpg");
            
            MimeBodyPart attach=new MimeBodyPart();
            DataHandler dh=new DataHandler(new FileDataSource("C:\Users\Administrator\Desktop\333.jpg"));
            attach.setDataHandler(dh);
            attach.setFileName(dh.getName());
            
            MimeMultipart mp1=new MimeMultipart();
            mp1.addBodyPart(text);
            mp1.addBodyPart(img);
            mp1.setSubType("related");
            
            MimeMultipart mp2=new MimeMultipart();
            mp2.addBodyPart(attach);
            mp2.setSubType("mixed");
            
            MimeBodyPart content =new MimeBodyPart();
            content.setContent(mp1);
            mp2.addBodyPart(content);

            
            message.setContent(mp2);
            message.saveChanges();;
            
            return message;
        }
        //发送简单文本
        private static Message createSimpleMail(Session session) throws Exception {
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress("sohu588168@sohu.com"));
            //to 代表发送  还有cc抄送等等
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
            message.setSubject("简单的标题");
            message.setContent("你好,第一封邮件", "text/html;charset=utf-8");
            return message;
        }
        //创建带图片的文件  
        private static Message createImageMail(Session session) throws Exception {
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress("sohu588168@sohu.com"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
            message.setSubject("带图片的邮件");
            MimeBodyPart  text=new MimeBodyPart();
            //这里的img必须有,才可以在邮件中显示出来图片
            text.setContent("这是一封带图片的<img src='cid:xxx.jpg'>邮件","text/html;charset=utf-8");
            
            MimeBodyPart image =new MimeBodyPart();
            //可以是src\22.jpg  也可以是当前桌面路径
            DataHandler dh=new DataHandler(new FileDataSource("C:\Users\Administrator\Desktop\333.jpg"));
            image.setDataHandler(dh);
            image.setContentID("xxx.jpg");
            
            MimeMultipart mm=new MimeMultipart();
            mm.addBodyPart(text);
            mm.addBodyPart(image);
            mm.setSubType("related");
            
            message.setContent(mm);
            message.saveChanges();
            message.writeTo(new FileOutputStream("C:\Users\Administrator\Desktop\222.eml"));
            
            return message;
        }
        private static Message createAttachMail(Session session) throws Exception {
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress("sohu588168@sohu.com"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("275002199@qq.com"));
            message.setSubject("JavaMail邮件发送测试");
            
            MimeBodyPart  text=new MimeBodyPart();
            text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8");
            
            MimeBodyPart attach  =new MimeBodyPart();
            DataHandler dh=new DataHandler(new FileDataSource("C:\Users\Administrator\Desktop\555.zip"));
            attach.setDataHandler(dh);
            //图片是ContentID   附件是setFileName
            attach.setFileName(dh.getName());
            
            MimeMultipart mm=new MimeMultipart();
            mm.addBodyPart(text);
            mm.addBodyPart(attach);
            //如果是附件需要改这个
            mm.setSubType("mixed");
            
            message.setContent(mm);
            message.saveChanges();
            message.writeTo(new FileOutputStream("C:\Users\Administrator\Desktop\333.eml"));
            
            return message;
        }
    }


  • 相关阅读:
    Linq to LLBL Gen Pro LLBL Gen的Linq程序设计
    应用反射技术为Infragistics Solution设计例子程序 代码简洁而且学习的效率高
    ASP.NET Web开发框架之零 项目介绍
    .NET 应用程序界面开发经验总结 设计良好的程序的表现之一就是细节做的还可以
    ORM Querier 基于TransactSQL解析的代码生成利器 帮助开发人员高效快速生成需要的ORM代码
    Visual Studio 2010 开发与调试IronPython脚本 为你的ERP/MIS 应用程序添加脚本功能
    ORM + .NET Remoting 完整例子程序 虽然现在都流行WCF,也没有必要抛弃已经掌握的.NET Remoting
    总结一下ERP .NET程序员必须掌握的.NET技术,掌握了这些技术工作起来才得心应手
    LLBL Gen 元数据编程 LLBL Gen Metadata Programming
    iPhone开发笔记[14/50]:没有开发者证书,先用模拟器也要开发
  • 原文地址:https://www.cnblogs.com/happy0120/p/7199009.html
Copyright © 2011-2022 走看看