基于JavaMail的Java邮件发送:复杂邮件发送
来源:互联网 发布:淘宝成交指数 编辑:程序博客网 时间:2024/05/01 18:20
一封复杂的邮件内容可以看做是由很多节点(或者可以说是“片段”/“部分”/“零件”)组成,文本、图片、附件等都可以看成是邮件内容中的一个节点。这些节点之间又可以相互关联组合成一个节点。最终组合成一个大节点就是邮件的正文内容。
完整代码演示:
package com.xiets.javamaildemo;import java.util.Date;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Message.RecipientType;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;import javax.mail.internet.MimeUtility;/** * 创建并发送一封包含文本、图片、附件的复杂邮件 * * JavaMail 版本: 1.6.0 * JDK 版本: JDK 1.7 以上(必须) * * @author xietansheng */public class Main { public static String myEmailAccount = "xxxxxxxx@163.com"; public static String myEmailPassword = "xxxxxxxx"; public static String myEmailSMTPHost = "smtp.163.com"; public static String receiveMailAccount = "xxxxxxxxx@qq.com"; public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", myEmailSMTPHost); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(props); session.setDebug(true); MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount); Transport transport = session.getTransport(); transport.connect(myEmailAccount, myEmailPassword); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } /** * 创建一封复杂邮件(文本+图片+附件) */ public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(sendMail, "我的测试邮件_发件人昵称", "UTF-8")); message.addRecipient(RecipientType.TO, new InternetAddress(receiveMail, "我的测试邮件_收件人昵称", "UTF-8")); message.setSubject("TEST邮件主题(文本+图片+附件)", "UTF-8"); MimeBodyPart image = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("FairyTail.jpg")); image.setDataHandler(dh); image.setContentID("image_fairy_tail"); MimeBodyPart text = new MimeBodyPart(); text.setContent("这是一张图片<br/><img src='cid:image_fairy_tail'/>", "text/html;charset=UTF-8"); MimeMultipart mm_text_image = new MimeMultipart(); mm_text_image.addBodyPart(text); mm_text_image.addBodyPart(image); mm_text_image.setSubType("related"); MimeBodyPart text_image = new MimeBodyPart(); text_image.setContent(mm_text_image); MimeBodyPart attachment = new MimeBodyPart(); DataHandler dh2 = new DataHandler(new FileDataSource("妖精的尾巴目录.doc")); attachment.setDataHandler(dh2); attachment.setFileName(MimeUtility.encodeText(dh2.getName())); MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text_image); mm.addBodyPart(attachment); mm.setSubType("mixed"); message.setContent(mm); message.setSentDate(new Date()); message.saveChanges(); return message; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
发送后查看收件人的收件箱:
转载地址 http://blog.csdn.net/xietansheng/article/details/51722660