结合barcode4j生成条形码 图片以附件的方式发送出去

结合barcode4j生成条形码 并图片以附件的方式发送出去

见以下代码:

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
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;
import javax.mail.util.ByteArrayDataSource;

import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

public void sendEmailModel() {
		Session m_SmtpSession;
		String m_SmtpHost = null;
		String m_SmtpUsername = null;
		String m_SmtpPassword = null;
		Properties props = new Properties();
		// 设置mail服务器
		props.put("mail.smtp.host", m_SmtpHost);
		props.put("mail.smtp.auth", "true");
		// Get session
		m_SmtpSession = Session.getDefaultInstance(props);
		// watch the mail commands go by to the mail server
		m_SmtpSession.setDebug(false);
		try {
			// Create the barcode bean 条形码生成
			Code39Bean bean = new Code39Bean();
			final int dpi = 150;
			// Configure the barcode generator
			bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));
			// makes the narrow bar width exactly one pixel
			bean.setWideFactor(3);
			bean.doQuietZone(false);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/jpeg", dpi,
					BufferedImage.TYPE_BYTE_BINARY, true, 0);
			// Generate the barcode
			bean.generateBarcode(canvas, "条形源码");
			// Signal end of generation
			canvas.finish();
			// 邮件生成
			MimeMessage msg = new MimeMessage(m_SmtpSession);
			// 发送源
			msg.setFrom(new InternetAddress("[email protected]"));
			// 此处可以发送多个地址
			InternetAddress[] tos = new InternetAddress[1];
			tos[0] = new InternetAddress("[email protected]");
			msg.addRecipients(Message.RecipientType.TO, tos);
			msg.setSubject("主题", "utf-8");
			// 使用Multipart发送邮件
			Multipart multipart = new MimeMultipart();
			// 正文内容
			MimeBodyPart part = new MimeBodyPart();
			part.setContent("正文如下:<br/>条形码如下:<br/><img src='cid:barcode.jpg'/>",
					"text/html; charset=utf-8");
			multipart.addBodyPart(part);
			// 附件
			part = new MimeBodyPart();
			ByteArrayDataSource ds = new ByteArrayDataSource(out.toByteArray(),
					"application/octet-stream");
			out.close();// 关闭流
			// 加入附件
			part.setDataHandler(new DataHandler(ds));
			// setHeader 目的是 可以在源码中使用barcode.jpg图片 ,如下: src='cid:barcode.jpg'
			part.setHeader("Content-ID", "barcode.jpg");
			ds.setName("barcode.jpg");
			part.setFileName(MimeUtility.encodeText(ds.getName()));
			multipart.addBodyPart(part);
			msg.setContent(multipart);
			msg.saveChanges();
			Transport transport;
			// 协议 protocol
			transport = m_SmtpSession.getTransport("smtp");
			transport.connect(m_SmtpHost, m_SmtpUsername, m_SmtpPassword);
			transport.sendMessage(msg, msg.getAllRecipients());
			transport.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

barcode4j.jar 见附件!

猜你喜欢

转载自crazywen2011.iteye.com/blog/1735809