JavaMail的学习
一个简单的JavaMail程序
需要jar包 mail.jar
1 package com.utils.test; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.UnsupportedEncodingException; 6 import java.util.Properties; 7 8 import javax.activation.DataHandler; 9 import javax.activation.DataSource; 10 import javax.activation.FileDataSource; 11 import javax.mail.Authenticator; 12 import javax.mail.MessagingException; 13 import javax.mail.PasswordAuthentication; 14 import javax.mail.Session; 15 import javax.mail.Transport; 16 import javax.mail.internet.AddressException; 17 import javax.mail.internet.InternetAddress; 18 import javax.mail.internet.MimeBodyPart; 19 import javax.mail.internet.MimeMessage; 20 import javax.mail.internet.MimeMessage.RecipientType; 21 import javax.mail.internet.MimeMultipart; 22 import javax.mail.internet.MimeUtility; 23 24 import org.junit.Test; 25 /** 26 * JavaMail学习 27 * 需要的jar包 : mail.jar 28 * @author Administrator 29 * 30 */ 31 public class MailTest { 32 33 /** 34 * 简单的一个JavaMail 35 * @throws AddressException 36 * @throws MessagingException 37 */ 38 @Test 39 public void sendMail01() throws AddressException, MessagingException{ 40 // 1. 设置Properties属性 41 Properties props = new Properties(); 42 props.setProperty("mail.transport.protocol", "SMTP"); 43 props.setProperty("mail.host", "smtp.163.com");// 设置服务器主机名称 44 props.setProperty("mail.smtp.auth", "true");// 是否需要验证 45 // 2.创建验证器 46 Authenticator auth = new Authenticator(){ 47 public PasswordAuthentication getPasswordAuthentication() { 48 return new PasswordAuthentication("kittys_test", "******"); 49 } 50 }; 51 // 3. 获取Session对象 52 Session session = Session.getInstance(props, auth); 53 session.setDebug(true); 54 // 4. 创建MimeMessage对象 55 MimeMessage msg = new MimeMessage(session); 56 //设置发送人 57 msg.setFrom(new InternetAddress("kittys_test@163.com")); 58 // 收件人 59 msg.setRecipient(RecipientType.TO, new InternetAddress("gray_test@sohu.com")); 60 msg.setRecipient(RecipientType.CC, new InternetAddress("gray_test@sina.com"));//抄送 61 msg.setRecipient(RecipientType.BCC, new InternetAddress("gray_test@126.com"));//暗送 62 // 主题 63 msg.setSubject("JavaMail测试"); 64 // 正文 65 msg.setContent("测试JavaMail", "text/html;charset=utf-8"); 66 // 5. 发送 67 Transport.send(msg); 68 } 69 /** 70 * JavaMail带有附件 71 * @throws MessagingException 72 * @throws IOException 73 */ 74 @Test 75 public void sendMail02() throws MessagingException, IOException{ 76 // 1. 设置Properties属性 77 Properties props = new Properties(); 78 props.setProperty("mail.transport.protocol", "SMTP"); 79 props.setProperty("mail.host", "smtp.sina.com");//指定服务器主机名称 80 props.setProperty("mail.smtp.auth", "true");// 是否验证 81 // 2.创建验证器 82 Authenticator auth = new Authenticator(){ 83 public PasswordAuthentication getPasswordAuthentication(){ 84 return new PasswordAuthentication("gray_test", "******"); 85 } 86 }; 87 // 3. 获取Session对象 88 Session session = Session.getInstance(props, auth); 89 session.setDebug(true); 90 // 4. 创建MimeMessage对象 91 MimeMessage msg = new MimeMessage(session); 92 //发件人 93 msg.setFrom(new InternetAddress("gray_test@sina.com")); 94 //收件人 95 msg.setRecipient(RecipientType.TO, new InternetAddress("gray_test@126.com")); 96 //设置主题 97 msg.setSubject("这是一封JavaMail测试邮件"); 98 // 4.1 创建部件集对象 99 MimeMultipart parts = new MimeMultipart();// 可看作为一个List 100 // 4.2 创建一个部件-->正文 101 MimeBodyPart msgPart = new MimeBodyPart(); 102 msgPart.setContent("垃圾邮件,JavaMail测试", "text/html;charset=utf-8"); 103 parts.addBodyPart(msgPart);// 把部件添加到部件集中 104 // 4.3 创建一个部件-->附件 105 MimeBodyPart attachPart = new MimeBodyPart(); 106 attachPart.attachFile(new File("E:\\视频资料\\傅颖.jpg"));//添加文件 107 // 通过 MimeUtility.encodeText(String fileName) 对中文进行编码 108 attachPart.setFileName(MimeUtility.encodeText("傅颖.jpg"));// 设置文件名 109 parts.addBodyPart(attachPart); 110 // 4.4 把内容添加到 MimeMessage对象中 111 msg.setContent(parts); 112 // 5. 发送 113 Transport.send(msg); 114 } 115 /** 116 * 邮件带有 附件 和 内嵌图片 117 * @throws AddressException 118 * @throws MessagingException 119 * @throws UnsupportedEncodingException 120 */ 121 @Test 122 public void sendMail03() throws AddressException, MessagingException, UnsupportedEncodingException{ 123 // 1. 设置Properties属性 124 Properties props = new Properties(); 125 props.setProperty("mail.transport.protocol", "SMTP"); 126 props.setProperty("mail.host", "smtp.sina.com");//指定服务器主机名称 127 props.setProperty("mail.smtp.auth", "true");// 是否验证 128 // 2.创建验证器 129 Authenticator auth = new Authenticator(){ 130 public PasswordAuthentication getPasswordAuthentication(){ 131 return new PasswordAuthentication("gray_test", "******"); 132 } 133 }; 134 // 3. 获取Session对象 135 Session session = Session.getInstance(props, auth); 136 session.setDebug(true); 137 // 4. 创建MimeMessage对象 138 MimeMessage msg = new MimeMessage(session); 139 msg.setFrom(new InternetAddress("gray_test@sina.com")); 140 msg.setRecipient(RecipientType.TO, new InternetAddress("gray_test@126.com")); 141 msg.setSubject("这是一封JavaMail测试邮件"); 142 143 MimeMultipart msgMultipart = new MimeMultipart("mixed"); 144 msg.setContent(msgMultipart); 145 MimeBodyPart attach = new MimeBodyPart(); // 附件 146 MimeBodyPart content = new MimeBodyPart(); // 正文 147 // 建立关系 148 msgMultipart.addBodyPart(attach); 149 msgMultipart.addBodyPart(content); 150 151 DataSource ds = new FileDataSource("E:\\视频资料\\傅颖.jpg"); 152 DataHandler dh = new DataHandler(ds); 153 attach.setDataHandler(dh); 154 // 通过 MimeUtility.encodeText(String fileName) 对中文进行编码 155 attach.setFileName(MimeUtility.encodeText("傅颖.jpg"));// 设置文件名 156 157 // 用于组合文本和图片,"related"型的MimeMultipart对象 158 MimeMultipart bodyMultipart = new MimeMultipart("related");// 关联关系 159 content.setContent(bodyMultipart); 160 MimeBodyPart jpgPart = new MimeBodyPart(); 161 MimeBodyPart htmlPart = new MimeBodyPart(); 162 bodyMultipart.addBodyPart(jpgPart); 163 bodyMultipart.addBodyPart(htmlPart); 164 165 DataSource jpgds = new FileDataSource("E:\\视频资料\\baidu.png"); 166 DataHandler jpgdh = new DataHandler(jpgds); 167 jpgPart.setDataHandler(jpgdh); 168 // jgpPart.setHeader("Content-Location", "https://www.baidu.com/img/logo_76bcbf7d7c327b5f29dd98aa4d6e9a1e.png"); 169 //设置对应的资源文件的唯一标识符,即 MIME 协议对于邮件的结构组织格式中的 Content-ID 头字段; 170 jpgPart.setHeader("Content-ID","image");// 把图片放到 cid 中 171 // htmlPart.setContent("JavaMial学习,这可是我自己用程序生成和发送的邮件哦" 172 // + "<img src='https://www.baidu.com/img/logo_76bcbf7d7c327b5f29dd98aa4d6e9a1e.png'>", 173 // "text/html;charset=utf-8"); 174 htmlPart.setContent("JavaMial学习,这可是我自己用程序生成和发送的邮件哦" 175 + "<img src='cid:image'>", "text/html;charset=utf-8"); 176 177 msg.saveChanges(); // 注意不要漏掉 178 // 5. 发送 179 Transport.send(msg); 180 } 181 }