zoukankan      html  css  js  c++  java
  • java 多媒体发送邮件

    import java.util.Properties;
    
    import javax.mail.Address;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    
    /**
     * 使用简单方式发送一封邮件(普通文本文件)
     * @author Administrator
     *
     */
    
    public class Demo1 {
    //    qq
    //       pop.qq.com 995     smtp.qq.com 465或587
    //    163:
    //       imap: imap.163.com 993 143
    //       pop3: pop.163.com 995 110
    //       smtp: smtp.163.com 465/994 25
    //    souhu:
    //       pop3.sohu.com    smtp.sohu.com
        
        public static void main(String[] args) throws MessagingException {
            Properties props = new Properties();
            props.setProperty("mail.smtp.auth", "false");
            props.setProperty("mail.transport.protocol", "smtp");
            
            Session session = Session.getDefaultInstance(props);
            //session.setDebug(true);
            
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("from@from.com"));
            msg.setText("Hello world!");
            msg.setSubject("test");
            
            String content = "<div>你不在学校吗?</div><br/><hr/><div>记得28号来学校</div>";
            
            Multipart mainPart = new MimeMultipart();
            // 创建一个包含HTML内容的MimeBodyPart
            BodyPart html = new MimeBodyPart();
            // 设置HTML内容
            html.setContent(content, "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            
            //msg.set
            msg.setContent(mainPart);

          String toUser= "**@baidu.com";
    InternetAddress mailList[] = InternetAddress.parse(toUser);
    String ccUser="**@baidu.com";
    InternetAddress ccmailList[] =InternetAddress.parse(ccUser);

    msg.setRecipients(Message.RecipientType.TO, mailList);
    msg.setRecipients(Message.RecipientType.CC, ccmailList);

    Transport trans = session.getTransport();
    trans.connect("appmail.baidu.com", 25, "s-baidu", null);
    trans.sendMessage(msg, msg.getAllRecipients());
    trans.close(); } }
  • 相关阅读:
    Android实战经验之图像处理及特效处理的集锦(总结版)
    Android类似于滚动的通知栏实现
    Python概览
    高效程序员的45个习惯读书笔记
    Web前台传对象字符串到后台并让后台反序列化对象字符串的方法(ASP.NET)
    发布订阅者模式之C#委托实现
    表数据复制(迁移)
    Code Smell
    Python学习过程遇到的Bug不断更新
    Resharper 7小技巧系列:导航、书签、和最近编辑
  • 原文地址:https://www.cnblogs.com/corecible/p/10151156.html
Copyright © 2011-2022 走看看