javamail與exchange 發(fā)送圖片與文字(圖片內(nèi)嵌)
package com.ellington.test;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Mailer {
private String host;
private String auth;
private String username;
private String domainUser;
private String password;
public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
Session s = Session.getInstance(props);
// s.setDebug(true);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress(username);
message.setFrom(from);
InternetAddress[] Toaddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
Toaddress[i] = new InternetAddress(to[i]);
message.setRecipients(Message.RecipientType.TO, Toaddress);
if (cc != null) {
InternetAddress[] Ccaddress = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++)
Ccaddress[i] = new InternetAddress(cc[i]);
message.setRecipients(Message.RecipientType.CC, Ccaddress);
}
if (bcc != null) {
InternetAddress[] Bccaddress = new InternetAddress[bcc.length];
for (int i = 0; i < bcc.length; i++)
Bccaddress[i] = new InternetAddress(bcc[i]);
message.setRecipients(Message.RecipientType.BCC, Bccaddress);
}
message.setSubject(subject);
message.setSentDate(new Date());
BodyPart mdp = new MimeBodyPart();
mdp.setContent(content, "text/html;charset=utf-8");
Multipart mm = new MimeMultipart();
mm.addBodyPart(mdp);
message.setContent(mm);
message.saveChanges();
Transport transport = s.getTransport("smtp");
transport.connect(host, (null == domainUser) ? username : domainUser, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}
public Mailer(String host, String auth, String domainUser, String username, String password) {
super();
this.host = host;
this.auth = auth;
this.domainUser = domainUser;
this.username = username;
this.password = password;
}
/**
* @param args
* @throws MessagingException
*/
public static void main(String[] args) throws MessagingException {
// TODO Auto-generated method stub
System.out.println(GetImageStr());
new Mailer("10.110.4.4", "false", "abc\\sysadmin", "sysadmin@abc.com", "sysadmin").send(new String[] { "G@abc.com","F@abc.com" }, null, null, "郵件中發(fā)送文字與圖片", "<h3>hello html5</h3><img src=\"data:image/png;base64,"+GetImageStr()+"\">" );
}
public static String GetImageStr()
{//將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理
String imgFile = "d:\\111.jpg";//待處理的圖片
InputStream in = null;
byte[] data = null;
//讀取圖片字節(jié)數(shù)組
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//對字節(jié)數(shù)組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64編碼過的字節(jié)數(shù)組字符串
}
public static boolean GenerateImage(String imgStr)
{//對字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片
if (imgStr == null) //圖像數(shù)據(jù)為空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解碼
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//調(diào)整異常數(shù)據(jù)
b[i]+=256;
}
}
//生成jpeg圖片
String imgFilePath = "d:\\222.jpg";//新生成的圖片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
}
廣州白馬檔口出租
番禺大石空調(diào)維修中心