javamail

1package com.syj; 2 3import java.io.ByteArrayInputStream; 4import java.io.ByteArrayOutputStream; 5import java.io.FileInputStream; 6import java.io.IOException; 7import java.io.InputStream; 8import java.io.OutputStream; 9import java.util.Arrays; 10import java.util.Date; 11import java.util.Properties; 12 13import javax.activation.DataHandler; 14import javax.activation.DataSource; 15import javax.activation.FileDataSource; 16import javax.mail.Authenticator; 17import javax.mail.BodyPart; 18import javax.mail.Message; 19import javax.mail.Multipart; 20import javax.mail.PasswordAuthentication; 21import javax.mail.Session; 22import javax.mail.Transport; 23import javax.mail.internet.InternetAddress; 24import javax.mail.internet.MimeBodyPart; 25import javax.mail.internet.MimeMessage; 26import javax.mail.internet.MimeMultipart; 27 28 29/** 30 * <p> 31 * Title:用java发送邮件的例子 32 * </p> 33 * 34 * <p> 35 * Description:发送图片附件并在html中使用该图片 36 * </p> 37 * 38 * <p> 39 * Copyright: Copyright (c) 2007 40 * </p> 41 * 42 * @author 孙钰佳 43 * @blog http://blog.csdn.net/sunyujia/ 44 * @main sunyujia@yahoo.cn 45 * @date Jun 10, 2008 12:35:26 AM 46 */ 47public class SendMail { 48 private static String username = "xxxx"; 49 private static String password = "xxxx"; 50 private static String smtpServer = "smtp.163.com"; 51 private static String fromMailAddress = "xxxx@163.com"; 52 private static String toMailAddress = "sunyujia@yahoo.cn"; 53 54 public static void main(String[] args) throws Exception { 55 Properties props = new Properties(); 56 props.put("mail.smtp.auth", "true"); 57 props.put("mail.smtp.host", smtpServer); 58 // 获得邮件会话对象 59 Session session = Session.getDefaultInstance(props, 60 new SmtpAuthenticator(username, password)); 61 /** *************************************************** */ 62 // 创建MIME邮件对象 63 MimeMessage mimeMessage = new MimeMessage(session); 64 mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人 65 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress( 66 toMailAddress));// 收件人 67 mimeMessage.setSubject("主题"); 68 mimeMessage.setSentDate(new Date());// 发送日期 69 Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件 70 /** *************************************************** */ 71 BodyPart bodyPart = new MimeBodyPart();// 正文 72 bodyPart.setDataHandler(new DataHandler("测<img src=/"cid:IMG1/" />试", 73 "text/html;charset=GBK"));// 网页格式 74 /** *************************************************** */ 75 BodyPart attachBodyPart = new MimeBodyPart();// 普通附件 76 FileDataSource fds = new FileDataSource("c:/boot.ini"); 77 attachBodyPart.setDataHandler(new DataHandler(fds)); 78 attachBodyPart.setFileName("=?GBK?B?" 79 + new sun.misc.BASE64Encoder().encode(fds.getName().getBytes()) 80 + "?=");// 解决附件名中文乱码 81 mp.addBodyPart(attachBodyPart); 82 /** *************************************************** */ 83 MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件图标 84 byte[] bytes = readFile("C:/button.gif"); 85 ByteArrayDataSource fileds = new ByteArrayDataSource(bytes, 86 "application/octet-stream"); 87 imgBodyPart.setDataHandler(new DataHandler(fileds)); 88 imgBodyPart.setFileName("button.gif"); 89 imgBodyPart.setHeader("Content-ID", "<img1></img1>");// 在html中使用该图片方法src="cid:IMG1" 90 mp.addBodyPart(imgBodyPart); 91 /** *************************************************** */ 92 mp.addBodyPart(bodyPart); 93 mimeMessage.setContent(mp);// 设置邮件内容对象 94 Transport.send(mimeMessage);// 发送邮件 95 96 } 97 98 /** 99 * 读取文件 100 * 101 * @param file 102 * 文件路径 103 * @return 返回二进制数组 104 */ 105 public static byte[] readFile(String file) { 106 FileInputStream fis = null; 107 ByteArrayOutputStream bos = null; 108 try { 109 fis = new FileInputStream(file); 110 bos = new ByteArrayOutputStream(); 111 int bytesRead; 112 byte buffer[] = new byte[1024 * 1024]; 113 while ((bytesRead = fis.read(buffer)) != -1) { 114 bos.write(buffer, 0, bytesRead); 115 Arrays.fill(buffer, (byte) 0); 116 } 117 } catch (IOException e1) { 118 e1.printStackTrace(); 119 } finally { 120 try { 121 if (bos != null) 122 bos.close(); 123 } catch (IOException e) { 124 e.printStackTrace(); 125 } 126 } 127 return bos.toByteArray(); 128 } 129} 130 131/** 132 * Smtp认证 133 */ 134class SmtpAuthenticator extends Authenticator { 135 String username = null; 136 String password = null; 137 138 // SMTP身份验证 139 public SmtpAuthenticator(String username, String password) { 140 this.username = username; 141 this.password = password; 142 } 143 144 public PasswordAuthentication getPasswordAuthentication() { 145 return new PasswordAuthentication(this.username, this.password); 146 } 147 148} 149class ByteArrayDataSource implements DataSource { 150 151 private final String contentType; 152 private final byte[] buf; 153 private final int len; 154 155 public ByteArrayDataSource(byte[] buf, String contentType) { 156 this(buf, buf.length, contentType); 157 } 158 159 public ByteArrayDataSource(byte[] buf, int length, String contentType) { 160 this.buf = buf; 161 this.len = length; 162 this.contentType = contentType; 163 } 164 165 public String getContentType() { 166 if (contentType == null) 167 return "application/octet-stream"; 168 return contentType; 169 } 170 171 public InputStream getInputStream() { 172 return new ByteArrayInputStream(buf, 0, len); 173 } 174 175 public String getName() { 176 return null; 177 } 178 179 public OutputStream getOutputStream() { 180 throw new UnsupportedOperationException(); 181 } 182}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我

Java 将图片转二进制再将二进制转成图片

importjava.awt.image.BufferedImage;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.