邮件开发项目的总结
2011-06-07 16:44
246 查看
这个月主要是做邮件开发项目,熟悉了javamail的一些应用,了解了邮件Mime格式。还有制作邮件内容的CKEditor编辑器(搞了很头疼。。)。算是做下总结了,以避免忘记。
1,javaMail的使用,用一个发送邮件的demo吧。
public class MailSendAction {
private static final Logger logger = Logger.getLogger(MailSendAction.class);
public static void main(String[] args) {
try {
String hostName = "smtp.qq.com";
String EMAIL_NAME = "258072761";
String EMAIL_PASSWORD = "*********";
MailSendAction mailAction = new MailSendAction();
Properties props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
// 发送开始
Transport transport = session.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"),
EMAIL_NAME, EMAIL_PASSWORD);
// transport.sendMessage(message,
// message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public MimeBodyPart createAttachment(String fileName) throws Exception {
MimeBodyPart attachementPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
attachementPart.setDataHandler(new DataHandler(fds));
String[] strs = fds.getName().split("_",2);
if(strs.length>1)
attachementPart.setFileName(strs[1]);
else
attachementPart.setFileName(fds.getName());
return attachementPart;
}
public MimeBodyPart createContent(String body, String fileName)
throws Exception {
MimeBodyPart contentBody = new MimeBodyPart();
MimeMultipart contentMulti = new MimeMultipart("related");
// 正文文本部分
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=gbk");
contentMulti.addBodyPart(textBody);
// 正文图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("logo_jpg");
contentMulti.addBodyPart(jpgBody);
contentBody.setContent(contentMulti);
return contentBody;
}
/**
*
* @param body
* @param fileName
* @return
* @throws Exception
*/
public MimeBodyPart createContent(String body, List<String> imgFileNames)
throws Exception {
MimeBodyPart relatedBodyPart = new MimeBodyPart();
MimeMultipart relatedMulti = new MimeMultipart("related");
// 正文文本部分
MimeBodyPart alternativeBodyPart = new MimeBodyPart();
MimeMultipart alternativeMulti = new MimeMultipart("alternative");
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=gbk");
alternativeMulti.addBodyPart(textBody);
alternativeBodyPart.setContent(alternativeMulti);
relatedMulti.addBodyPart(alternativeBodyPart);
// 内置资源部分
int i = 0;
for (String imgFileName : imgFileNames) {
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(imgFileName);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("img" + i);
relatedMulti.addBodyPart(jpgBody);
i++;
}
relatedBodyPart.setContent(relatedMulti);
return relatedBodyPart;
}
/**
*
* @param session
* @return
* @throws Exception
*/
public MimeMessage createMessage(String subject, String body,
Session session, List<String> imgFileNames,
List<String> attacheFilePaths) throws Exception {
MimeMessage msg = new MimeMessage(session);
msg.setSubject(subject, "gb2312");
// 创建邮件的各个MimeBodyPart部份
// 创建附件
List<MimeBodyPart> attachmentList = new ArrayList<MimeBodyPart>();
for (String attachFilePath : attacheFilePaths) {
attachmentList.add(createAttachment(attachFilePath));
}
MimeBodyPart content = createContent(body, imgFileNames);
// 将邮件中各个部份组合到一个"mixed"型的MimeMultipart对象
MimeMultipart allpart = new MimeMultipart("mixed");
for (MimeBodyPart mimeBodyPart : attachmentList) {
allpart.addBodyPart(mimeBodyPart);
}
allpart.addBodyPart(content);
// 将上面混合型的 MimeMultipart对象作为邮件内容并保存
msg.setContent(allpart);
return msg;
}
public static String sendMail(String emlPath, String to){
try {
String hostName = "smtp.qq.com";
String EMAIL_NAME = "258072761";
String EMAIL_PASSWORD = "hhysbyj123456";
String from = "258072761@qq.com";
Properties props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
InputStream fis = new FileInputStream(emlPath);
MimeMessage message = new MimeMessage(session, fis);
message.setRecipients(Message.RecipientType.TO, (Address[])
InternetAddress.parse(to));
message.setFrom(new InternetAddress(from));
// 发送开始
Transport transport = session.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"),
EMAIL_NAME, EMAIL_PASSWORD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
return "发送邮件成功";
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
} Mime邮件的格式请见:http://dev.csdn.net/htmls/18/18448.html
2,关于ckeditor编辑器的使用
<tr>
<th width="15%">邮件内容</th>
<td id= "mailContent" width="85%" colspan="3">
<textarea id="content" name="content" style="display:none"></textarea>
<script type="text/javascript">
CKEDITOR.replace('content',{filebrowserUploadUrl : '/MailManager/ckeditor/uploader?Type=File',
filebrowserImageUploadUrl : '/MailManager/ckeditor/uploader?Type=Image',
filebrowserFlashUploadUrl : '/MailManager/ckeditor/uploader?Type=Flash'
});
</script>
<span id="sendTimeTip"></span>
</td>
</tr> jsp页面导入:<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %><script type="text/javascript" src="../javascript/ckeditor/ckeditor.js" charset="UTF-8"></script>在制作邮件内容时,动态删除了mailContent节点,然后重新生成一个ckeditor编辑器,怎么都生成不出来,后来知道是没有销毁对象的原因,正确的销毁对象:
if(CKEDITOR.instances['content'])//content为代替textarea的id名称
delete CKEDITOR.instances['content'];
1,javaMail的使用,用一个发送邮件的demo吧。
public class MailSendAction {
private static final Logger logger = Logger.getLogger(MailSendAction.class);
public static void main(String[] args) {
try {
String hostName = "smtp.qq.com";
String EMAIL_NAME = "258072761";
String EMAIL_PASSWORD = "*********";
MailSendAction mailAction = new MailSendAction();
Properties props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
// 发送开始
Transport transport = session.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"),
EMAIL_NAME, EMAIL_PASSWORD);
// transport.sendMessage(message,
// message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public MimeBodyPart createAttachment(String fileName) throws Exception {
MimeBodyPart attachementPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
attachementPart.setDataHandler(new DataHandler(fds));
String[] strs = fds.getName().split("_",2);
if(strs.length>1)
attachementPart.setFileName(strs[1]);
else
attachementPart.setFileName(fds.getName());
return attachementPart;
}
public MimeBodyPart createContent(String body, String fileName)
throws Exception {
MimeBodyPart contentBody = new MimeBodyPart();
MimeMultipart contentMulti = new MimeMultipart("related");
// 正文文本部分
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=gbk");
contentMulti.addBodyPart(textBody);
// 正文图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("logo_jpg");
contentMulti.addBodyPart(jpgBody);
contentBody.setContent(contentMulti);
return contentBody;
}
/**
*
* @param body
* @param fileName
* @return
* @throws Exception
*/
public MimeBodyPart createContent(String body, List<String> imgFileNames)
throws Exception {
MimeBodyPart relatedBodyPart = new MimeBodyPart();
MimeMultipart relatedMulti = new MimeMultipart("related");
// 正文文本部分
MimeBodyPart alternativeBodyPart = new MimeBodyPart();
MimeMultipart alternativeMulti = new MimeMultipart("alternative");
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=gbk");
alternativeMulti.addBodyPart(textBody);
alternativeBodyPart.setContent(alternativeMulti);
relatedMulti.addBodyPart(alternativeBodyPart);
// 内置资源部分
int i = 0;
for (String imgFileName : imgFileNames) {
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(imgFileName);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("img" + i);
relatedMulti.addBodyPart(jpgBody);
i++;
}
relatedBodyPart.setContent(relatedMulti);
return relatedBodyPart;
}
/**
*
* @param session
* @return
* @throws Exception
*/
public MimeMessage createMessage(String subject, String body,
Session session, List<String> imgFileNames,
List<String> attacheFilePaths) throws Exception {
MimeMessage msg = new MimeMessage(session);
msg.setSubject(subject, "gb2312");
// 创建邮件的各个MimeBodyPart部份
// 创建附件
List<MimeBodyPart> attachmentList = new ArrayList<MimeBodyPart>();
for (String attachFilePath : attacheFilePaths) {
attachmentList.add(createAttachment(attachFilePath));
}
MimeBodyPart content = createContent(body, imgFileNames);
// 将邮件中各个部份组合到一个"mixed"型的MimeMultipart对象
MimeMultipart allpart = new MimeMultipart("mixed");
for (MimeBodyPart mimeBodyPart : attachmentList) {
allpart.addBodyPart(mimeBodyPart);
}
allpart.addBodyPart(content);
// 将上面混合型的 MimeMultipart对象作为邮件内容并保存
msg.setContent(allpart);
return msg;
}
public static String sendMail(String emlPath, String to){
try {
String hostName = "smtp.qq.com";
String EMAIL_NAME = "258072761";
String EMAIL_PASSWORD = "hhysbyj123456";
String from = "258072761@qq.com";
Properties props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
InputStream fis = new FileInputStream(emlPath);
MimeMessage message = new MimeMessage(session, fis);
message.setRecipients(Message.RecipientType.TO, (Address[])
InternetAddress.parse(to));
message.setFrom(new InternetAddress(from));
// 发送开始
Transport transport = session.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"),
EMAIL_NAME, EMAIL_PASSWORD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
return "发送邮件成功";
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
} Mime邮件的格式请见:http://dev.csdn.net/htmls/18/18448.html
2,关于ckeditor编辑器的使用
<tr>
<th width="15%">邮件内容</th>
<td id= "mailContent" width="85%" colspan="3">
<textarea id="content" name="content" style="display:none"></textarea>
<script type="text/javascript">
CKEDITOR.replace('content',{filebrowserUploadUrl : '/MailManager/ckeditor/uploader?Type=File',
filebrowserImageUploadUrl : '/MailManager/ckeditor/uploader?Type=Image',
filebrowserFlashUploadUrl : '/MailManager/ckeditor/uploader?Type=Flash'
});
</script>
<span id="sendTimeTip"></span>
</td>
</tr> jsp页面导入:<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %><script type="text/javascript" src="../javascript/ckeditor/ckeditor.js" charset="UTF-8"></script>在制作邮件内容时,动态删除了mailContent节点,然后重新生成一个ckeditor编辑器,怎么都生成不出来,后来知道是没有销毁对象的原因,正确的销毁对象:
if(CKEDITOR.instances['content'])//content为代替textarea的id名称
delete CKEDITOR.instances['content'];
相关文章推荐
- 项目开发总结(联想GIS)-----发送邮件
- 做项目开发和主流技术学习总结
- 独立表单项目开发总结
- 控件activeX开发之项目ffmpeg解码h264——总结
- Android开发项目名称文件夹报错,但是根目录不报错问题解决方案总结
- 项目开发知识点总结
- 基于Redux的ReactNative项目开发总结(一)
- 项目总结(采用领域驱动开发方式)
- MVC开发之排球计分(八)项目开发总结
- java开发微信项目总结(一) 基本概念介绍
- 项目开发中XML 读取错误总结
- 关于php项目的开发回顾总结第六章-----性能决定高低
- 美国外包项目开发总结
- Android视频播放项目总结之 使用第三方Vitamio库,开发万能播放器(一)
- Java Web项目开发项目经验总结
- 项目开发中XML 读取错误总结
- Android项目开发技术总结
- 关于fan客•尚汇项目开发的个人总结
- Windows Phone 7微博客户端开发之项目总结升级篇
- Android电话短信拦截项目总结之 开发工具