• JAVA 发送邮件代码---发送HTML内容


     依赖包:mail.jar

    JAR链接地址: http://pan.baidu.com/s/1o8LNl0Y 密码: ja52
    package com.dava; 
    
    import java.util.Properties; 
    
    import javax.activation.DataHandler; 
    import javax.mail.BodyPart; 
    import javax.mail.Multipart; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.Message.RecipientType; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeBodyPart; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeMultipart; 
    
    public class sendEmal { 
    
    public static void main(String[] args) { 
    String content="尊敬的客户,您好:<br>    您的退换货申请已由博乐宝客服受理,现需您将机器故障照片及检测结果等附件直接回复至此邮箱。我们收到您的邮件后会尽快为您处理。<br>如有任何问题,请致电。感谢您的配合与支持!"; 
    try { 
    
    System.out.println("开始发送邮件!"); 
    Properties props = new Properties(); 
    
    props.put("mail.smtp.host", "smtp.163.com"); 
    props.put("mail.smtp.auth", true); 
    
    Session session = Session.getDefaultInstance(props); 
    
    MimeMessage message = new MimeMessage(session); 
    message.setFrom(new InternetAddress("yangxinghe_it@163.com")); 
    message.setSubject("测试邮件标题");// 邮件标题 
    
    Multipart mp = new MimeMultipart("related"); 
    BodyPart bodyPart = new MimeBodyPart(); 
    bodyPart.setDataHandler(new DataHandler(content,"text/html;charset=UTF-8")); 
    
    mp.addBodyPart(bodyPart); 
    message.setContent(mp);// 设置邮件内容对象 
    
    message.setSentDate(new java.util.Date(System.currentTimeMillis())); 
    message.saveChanges(); 
    message.addRecipient(RecipientType.TO, new InternetAddress( 
    "1049232220@qq.com")); 
    
    Transport transport = session.getTransport("smtp"); 
    transport.connect("smtp.163.com", "yangxinghe_it@163.com", 
    ".dava4978$."); 
    transport.sendMessage(message, message.getAllRecipients()); 
    transport.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    System.out.println("发送邮件结束!"); 
    } 
    
    }
    
    
    
     
  • 相关阅读:
    测试SQL
    UpdatePanel中弹出新窗口
    无法打开物理文件 操作系统错误 5:拒绝访问 SQL Sever
    Repeater嵌套Repeater
    SQL2000清除SQL日志
    sql批量修改字段内容的语句-SQL技巧
    SQL时间格式化 转载备用~
    远程连接数据库
    MySql 文件导入导出
    pyspark启动与简单使用----本地模式(local)----shell
  • 原文地址:https://www.cnblogs.com/dava/p/6416517.html
Copyright © 2020-2023  润新知