1.邮件发送类 : package com.taihuatalk.taihua.common.common; /** *//** *//** *//** * Author:李鹏 * CreateDate:2006-06-25 22:47:00 * Modifier:李鹏 * ModifyDate:2006-06-25 22:47:00 * Version:1.0 * Copyright(c)2006 泰华网络 沪ICP备06××××××号 * All right reserved. */
import javax.mail.Transport; import javax.mail.internet.InternetAddress; import java.util.Date; import javax.mail.Message.RecipientType; import javax.mail.internet.MimeMessage; import javax.mail.Message; import javax.mail.Session; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.internet.MimeBodyPart; import javax.mail.Multipart; import javax.mail.internet.MimeMultipart; import org.apache.log4j.Logger; /** *//** *//** *//** * 发送Email类 */
public class SendEmail { Logger logger=Logger.getLogger(this.getClass()); /** *//** *//** *//** * Email发送方法 * @param toemails 需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开 * @param content 邮件内容 * @param subject 邮件主题 * @return 邮件成功发送则返回true,否则返回false */ public boolean sendSimpleEmail(String toemails,String content,String subject){ boolean result=false; logger.info("进入发送Email类"); try { //创建属性对象 Properties props = new Properties(); //设置邮件传输协议为:smtp props.put("mail.transpost.protocol","smtp"); //设置邮件服务器地址 props.put("mail.smtp.host", "smtp.163.com"); //设置邮件验证为真 props.put("mail.smtp.auth", "true"); //设置邮件服务器端口 props.put("mail.smtp.port","25");
//调用验证类进行验证,需要参数发件人用户名和密码 logger.info("验证类实例化=============="); CheckSendEmail auth=new CheckSendEmail("lip009","lip009");
logger.info("验证通过"); //创建session对象 Session sendMailSession; sendMailSession = Session.getInstance(props, auth); //设置输出调试信息 sendMailSession.setDebug(true);
logger.info("开始创建消息对象"); //创建信息对象 Message newMessage = new MimeMessage(sendMailSession);
//输入发送信息 //设置发信人地址 logger.info("设置发信人地址"); newMessage.setFrom(new InternetAddress("link2friend@163.com"));
//设置收信人地址,只支持单用户发送 // newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("lip0091981@163.com"));
//设置收信人地址,可以支持多用户发送 logger.info("设置收信人地址"); newMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toemails)); //附件 //============================================================================== //msgText是信件的正文,共有两行 // String msgText = content;
//msgAttachment是一段字符串作为附件内容 // String msgAttachment = "This is an attachment string!";
// MimeBodyPart mbp1 = new MimeBodyPart();
// mbp1.setText(msgText); //把前面定义的msgText中的文字设定为邮件正文的内容
//创建附件部分 // MimeBodyPart mbp2 = new MimeBodyPart();
//使用setText(text, charset)来加入附件 // mbp2.setText(msgAttachment, "gb2312");
//创建Multipart // Multipart mp = new MimeMultipart();
// mp.addBodyPart(mbp1); // mp.addBodyPart(mbp2);
// 添加 Multipart到Message中 // newMessage.setContent(mp); //============================================================================== //设置信件文本格式(当设置了附件,这里就不能有) logger.info("设置格式"); newMessage.setContent("SendMail", "text/html");
//设置信件主题 logger.info("设置主题"); newMessage.setSubject(subject);
//设置信件发送日期 logger.info("设置发送日期"); newMessage.setSentDate(new Date());
//设置信件正文(当设置了附件,这里就不能有) newMessage.setText(content); logger.info("设置完消息"); //创建对象 Transport transport; transport = sendMailSession.getTransport("smtp"); logger.info("将要发送");
//发送 result=true; transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功! logger.info("恭喜你!您的邮件已经成功发送!"); } catch (MessagingException ex) { // ex.printStackTrace(); } return result; } } 2.邮件帐号密码验证类: package com.taihuatalk.taihua.common.common; /** *//** *//** *//** * Author:李鹏 * CreateDate:2006-06-25 22:47:00 * Modifier:李鹏 * ModifyDate:2006-06-25 22:47:00 * Version:1.0 * Copyright(c)2006 泰华网络 沪ICP备06××××××号 * All right reserved. */
import javax.mail.*;
/** *//** *//** *//** * Email发送帐号密码验证类 */
public class CheckSendEmail extends Authenticator{ private String m_username = null; private String m_userpass = null; public void setUsername(String username) { m_username = username; } public void setUserpass(String userpass) { m_userpass = userpass; } public CheckSendEmail(String username, String userpass) { super(); setUsername(username); setUserpass(userpass); } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(m_username,m_userpass); } } JavaMail操作的总结(1) 2004-08-15 gztoby加入身份验证信息的SMTP mail发送 2005-04-06 sykpboy邮件发送程序(可以同时发给多个地址、可以带附件) 2004-11-09 naipengzhao用JavaMail写简易电子邮件发送系统[转载] 2004-07-29 lwqJ在JSP中使用JavaMail(下) 2004-07-01 arielxp
|