邮件发送
2013-11-30 00:00
260 查看
import java.io.File; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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; public class EmailSender { private static final String charset = "GBK"; private static final String defaultMimetype = "text/plain"; public static void main(String[] args) throws Exception { EmailSender.send(new String[]{"2345678@qq.com"}, "邮件测试xx", "<b>这是个测试</b>", null , "text/html"); } /** * 发送邮件 * @param receivers 收件人 * @param subject 标题 * @param mailContent 邮件内容 * @param attachements 附件 * @param mimetype 内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html */ public static void send(String[] receivers, String subject, String mailContent, File[] attachements, String mimetype) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.qq.com");//smtp服务器地址 sohu props.put("mail.smtp.auth", "true");//需要校验 Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("854334592","12345678");//登录用户名/密码 } }); session.setDebug(true); try { MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress("854334592@qq.com"));//发件人邮箱 InternetAddress[] toAddress = new InternetAddress[receivers.length]; for (int i=0; i<receivers.length; i++) { toAddress[i] = new InternetAddress(receivers[i]); } mimeMessage.setRecipients(Message.RecipientType.TO, toAddress);//收件人邮件 mimeMessage.setSubject(subject, charset); Multipart multipart = new MimeMultipart(); //正文 MimeBodyPart body = new MimeBodyPart(); // body.setText(message, charset);不支持html body.setContent(mailContent, (mimetype!=null && !"".equals(mimetype) ? mimetype : defaultMimetype)+ ";charset="+ charset); multipart.addBodyPart(body);//发件内容 //附件 if(attachements!=null){ for (File attachement : attachements) { MimeBodyPart attache = new MimeBodyPart(); //ByteArrayDataSource bads = new ByteArrayDataSource(byte[],"application/x-any"); attache.setDataHandler(new DataHandler(new FileDataSource(attachement))); String fileName = getLastName(attachement.getName()); attache.setFileName(MimeUtility.encodeText(fileName, charset, null)); multipart.addBodyPart(attache); } } mimeMessage.setContent(multipart); // SimpleDateFormat formcat = new SimpleDateFormat("yyyy-MM-dd"); mimeMessage.setSentDate(new Date());//formcat.parse("2010-5-23") Transport.send(mimeMessage); } catch (Exception e) { e.printStackTrace(); } } private static String getLastName(String fileName) { int pos = fileName.lastIndexOf("\\"); if (pos > -1) { fileName = fileName.substring(pos + 1); } pos = fileName.lastIndexOf("/"); if (pos > -1) { fileName = fileName.substring(pos + 1); } return fileName; } }
相关文章推荐
- 《pro Spring》学习笔记之使用Spring+James邮件服务器发送内嵌图片的HTML邮件
- 利用Oracle的utl_smtp 发送邮件(公网邮件和服务器)
- 对邮件发送系统的深刻解析(一般程序员都不一定搞的懂)
- iOS 捕获Crash 发送Bug邮件
- 在ASP.NET中使用SmtpClient发送邮件
- Jmail发送和接收邮件
- 实现javaMail邮件发送和个人心得
- Android中使用JavaMail发送邮件
- WebEmail免费邮件发送组件!!
- 让nagios使用外部邮箱发送警告邮件
- python发送邮件
- perl 发送邮件脚本
- rails4.2.6配置发送邮件
- 发送邮件的类
- Testlink发送邮件的两种配置方法
- 利用阿帕奇邮件服务器发送邮件
- Python自动发送邮件的脚本源码
- 一步一步从原理跟我学邮件收取及发送 1.网络命令的发送
- 如何在.NET发送邮件(摘)
- Iphone的发送短信-邮件-打电话代码示例(转)