1,读入图片的方式:
发现网上讲的很多读取图片的方式都不对,按下面提供的这个方法来读取,保证成功。
1
private byte[] getImageBytes(String file)
{
2
byte[] myData = null;
3
InputStream input = getClass().getClassLoader().getResourceAsStream(
4
file);
5
try
{
6
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
7
int ch = 0;
8
while ((ch = input.read()) != -1)
{
9
byteArray.write(ch);
10
}
11
// System.out.println(byteArray.size());
12
myData = byteArray.toByteArray();
13
// System.out.println(myData.length);
14
} catch (Exception e)
{
15
e.printStackTrace();
16
}
17
return myData;
18
} 2,发送邮件的“机关”
1
MimeMessage msg = new MimeMessage(mailSession);
2
msg.setFrom(new InternetAddress(this.getSenderAddress()));
3
msg.setSubject(this.getTitle());
4
msg.setSentDate(new Date());
5
Address[] adds = InternetAddress.parse(getToAddress());
6
msg.addRecipients(javax.mail.Message.RecipientType.TO, adds);
7
// 新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
8
MimeMultipart mm = new MimeMultipart("related");
9
// 新建一个存放信件内容的BodyPart对象
10
BodyPart mdp = new MimeBodyPart();
11
// 给BodyPart对象设置内容和格式/编码方式
12
mdp.setContent(this.getContent(), "text/html;charset=utf-8");
13
// 这句很重要,千万不要忘了
14
mm.addBodyPart(mdp);
15
16
// ---------图片处理开始!!!!!!!!!!!!!!!!
17
mdp = new MimeBodyPart();
18
byte bbb[] = new byte[1024 * 10];
19
this.getClass().getClassLoader().getResourceAsStream("notice.jpg")
20
.read(bbb);
21
DataHandler dh = new DataHandler(new ByteArrayDataSource(this
22
.getImageBytes("notice.jpg"), "application/octet-stream"));
23
mdp.setDataHandler(dh);
24
// 加上这句将作为附件发送,否则将作为信件的文本内容
25
mdp.setFileName("1.jpg");
26
mdp.setHeader("content-id", "<IMG1>");
27
// 将含有附件的BodyPart加入到MimeMultipart对象中
28
mm.addBodyPart(mdp);
29
// ---------图片处理结束!!!!!!!!!!!!!!!!
30
31
// 把mm作为消息对象的内容
32
msg.setContent(mm); 仔细看代码中的注释吧,相信大有帮助。
3,一个实际应用的完整代码
要求根据一个格式文件和模版,发一封漂亮的邮件,所以需要用到HTML格式来发送邮件。不多说了,看代码吧!


1
import java.io.ByteArrayOutputStream;
2
import java.io.File;
3
import java.io.FileReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.util.Date;
7
import java.util.HashMap;
8
import java.util.Map;
9
import java.util.Properties;
10
11
import javax.activation.DataHandler;
12
import javax.mail.Address;
13
import javax.mail.BodyPart;
14
import javax.mail.internet.InternetAddress;
15
import javax.mail.internet.MimeBodyPart;
16
import javax.mail.internet.MimeMessage;
17
import javax.mail.internet.MimeMultipart;
18
import javax.mail.util.ByteArrayDataSource;
19
20
/** *//**
21
*
22
* @author robin
23
* @version $Revision: 1.4 $
24
*/
25
public class SendMailUtil
{
26
27
/** *//**
28
* Field mailServerAddress.
29
*/
30
private String mailServerAddress;
31
32
/** *//**
33
* Field user.
34
*/
35
private String user;
36
37
/** *//**
38
* Field password.
39
*/
40
private String password;
41
42
/** *//**
43
* Field toAddress.
44
*/
45
private String toAddress;
46
47
/** *//**
48
* Field ccAddress.
49
*/
50
private String ccAddress;
51
52
/** *//**
53
* Field title.
54
*/
55
private String title;
56
57
/** *//**
58
* Field content.
59
*/
60
private String content;
61
62
/** *//**
63
* Field isHtml.
64
*/
65
private boolean isHtml = true;
66
67
/** *//**
68
* Field attachmentFiles.
69
*/
70
private Map attachmentFiles = null;
71
72
/** *//**
73
* Field senereAddress.
74
*/
75
private String senderAddress;
76
77
private byte[] getImageBytes(String file)
{
78
byte[] myData = null;
79
InputStream input = getClass().getClassLoader().getResourceAsStream(
80
file);
81
try
{
82
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
83
int ch = 0;
84
while ((ch = input.read()) != -1)
{
85
byteArray.write(ch);
86
}
87
// System.out.println(byteArray.size());
88
myData = byteArray.toByteArray();
89
// System.out.println(myData.length);
90
} catch (Exception e)
{
91
e.printStackTrace();
92
}
93
return myData;
94
}
95
96
public void sendMail() throws Exception
{
97
Properties pos = new Properties();
98
pos.put("mail.smtp.host", "10.5.1.1");
99
javax.mail.Session mailSession = javax.mail.Session.getInstance(pos,
100
null);
101
MimeMessage msg = new MimeMessage(mailSession);
102
msg.setFrom(new InternetAddress(this.getSenderAddress()));
103
msg.setSubject(this.getTitle());
104
msg.setSentDate(new Date());
105
Address[] adds = InternetAddress.parse(getToAddress());
106
msg.addRecipients(javax.mail.Message.RecipientType.TO, adds);
107
// 新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
108
MimeMultipart mm = new MimeMultipart("related");
109
// 新建一个存放信件内容的BodyPart对象
110
BodyPart mdp = new MimeBodyPart();
111
// 给BodyPart对象设置内容和格式/编码方式
112
mdp.setContent(this.getContent(), "text/html;charset=utf-8");
113
// 这句很重要,千万不要忘了
114
mm.addBodyPart(mdp);
115
116
// ---------图片处理开始!!!!!!!!!!!!!!!!
117
mdp = new MimeBodyPart();
118
byte bbb[] = new byte[1024 * 10];
119
this.getClass().getClassLoader().getResourceAsStream("notice.jpg")
120
.read(bbb);
121
DataHandler dh = new DataHandler(new ByteArrayDataSource(this
122
.getImageBytes("notice.jpg"), "application/octet-stream"));
123
mdp.setDataHandler(dh);
124
// 加上这句将作为附件发送,否则将作为信件的文本内容
125
mdp.setFileName("1.jpg");
126
mdp.setHeader("content-id", "<IMG1>");
127
// 将含有附件的BodyPart加入到MimeMultipart对象中
128
mm.addBodyPart(mdp);
129
// ---------图片处理结束!!!!!!!!!!!!!!!!
130
131
// 把mm作为消息对象的内容
132
msg.setContent(mm);
133
msg.saveChanges();
134
javax.mail.Transport transport = mailSession.getTransport("smtp");
135
transport.connect();
136
transport.sendMessage(msg, msg.getAllRecipients());
137
transport.close();
138
139
}
140
141
/** *//**
142
* Method getCcAddress.
143
*
144
* @return String
145
*/
146
public String getCcAddress()
{
147
return ccAddress;
148
}
149
150
/** *//**
151
* Method getContent.
152
*
153
* @return String
154
*/
155
public String getContent()
{
156
if (content == null)
{
157
return "";
158
} else
{
159
return content;
160
}
161
}
162
163
/** *//**
164
* Method getMailServerAddress.
165
*
166
* @return String
167
*/
168
public String getMailServerAddress()
{
169
return "10.5.1.1";
170
}
171
172
/** *//**
173
* Method getSenderId.
174
*
175
* @return String
176
*/
177
public String getUser()
{
178
if (user == null || user.equals(""))
{
179
user = "";
180
}
181
return user;
182
}
183
184
/** *//**
185
* Method getPassword.
186
*
187
* @return String
188
*/
189
public String getPassword()
{
190
if (password == null || password.equals(""))
{
191
password = "";
192
}
193
return password;
194
}
195
196
/** *//**
197
* Method getTitle.
198
*
199
* @return String
200
*/
201
public String getTitle()
{
202
if (title == null)
{
203
return "";
204
} else
{
205
return title;
206
}
207
}
208
209
/** *//**
210
* Method getToAddress.
211
*
212
* @return String
213
*/
214
public String getToAddress()
{
215
return toAddress;
216
}
217
218
/** *//**
219
* Method isHtml.
220
*
221
* @return boolean
222
*/
223
public boolean isHtml()
{
224
return isHtml;
225
}
226
227
/** *//**
228
* Method setCcAddress.
229
*
230
* @param ccAddress
231
* String
232
*/
233
public void setCcAddress(String ccAddress)
{
234
this.ccAddress = ccAddress;
235
}
236
237
/** *//**
238
* Method setContent.
239
*
240
* @param content
241
* String
242
*/
243
public void setContent(String content)
{
244
this.content = content;
245
}
246
247
/** *//**
248
* Method setMailServerAddress.
249
*
250
* @param mailServerAddress
251
* String
252
*/
253![]()