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, "邮件中发送文字与图片", "<h3>hello html5</h3><img src=\"data:image/png;base64,"+GetImageStr()+"\">" );
}
public static String GetImageStr()
{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "d:\\111.jpg";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
}
public static boolean GenerateImage(String imgStr)
{//对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
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)
{//调整异常数据
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;
}
}
}
广州白马档口出租
番禺大石空调维修中心