现在的位置: 首页 > 综合 > 正文

JAVA群发邮件代码

2013年10月16日 ⁄ 综合 ⁄ 共 4104字 ⁄ 字号 评论关闭
public class SendEmailUtil { /** * 取得邮件模板 * * @param path 模板路径 * @return */ public static String getMailTemplate(final String path) { String content = null; content = FileUtil.readFile(path); return content; } public static void main(final String[] args) { final String mailbody = getMailTemplate("F:/test/123.html"); final SendEmailUtil themail = new SendEmailUtil("mail.163.com");// 设置发送邮件服务器smtp themail.setNeedAuth(true);// 设置smtp是否需要认证 themail.setSubject("你好");// 设置邮件主题 themail.setBody(mailbody);// 内容 themail.setTo("gaozhenyi@100ruyi.com");// 收件人 themail.setFrom("ruyi100_cx@163.com");// 发件人 // themail.addFileAffix("E:\\approval.log");// 添加附件 themail.setNamePass("ruyi100_cx@163.com", "ruyi100");// 设置发送邮件服务器smtp中你的登录名及密码 themail.sendout(); } private MimeMessage mimeMsg; // MIME邮件对象 private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 private String password; private Properties props; // 系统属性 private Session session; // 邮件会话对象 private String username; // smtp认证用户名和密码 public SendEmailUtil(final String smtp) { this.setSmtpHost(smtp); this.createMimeMessage(); } public boolean addFileAffix(final String filename) { System.out.println("增加邮件附件:" + filename); try { final BodyPart bp = new MimeBodyPart(); final FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); this.mp.addBodyPart(bp); return true; } catch (final Exception e) { System.err.println("增加邮件附件:" + filename + "发生错误!" + e); return false; } } public boolean createMimeMessage() { try { System.out.println("准备获取邮件会话对象!"); this.session = Session.getDefaultInstance(this.props, null); // 获得邮件会话对象 this.session.setDebug(true); } catch (final Exception e) { System.err.println("获取邮件会话对象时发生错误!" + e); return false; } System.out.println("准备创建MIME邮件对象!"); try { this.mimeMsg = new MimeMessage(this.session); // 创建MIME邮件对象 this.mp = new MimeMultipart(); return true; } catch (final Exception e) { System.err.println("创建MIME邮件对象失败!" + e); return false; } } public boolean sendout() { try { this.mimeMsg.setContent(this.mp); this.mimeMsg.saveChanges(); System.out.println("正在发送邮件...."); final Session mailSession = Session.getInstance(this.props, null); final Transport transport = mailSession.getTransport("smtp"); transport.connect((String) this.props.get("mail.smtp.host"), 25, this.username, this.password); System.out.println("发送邮件的smtp服务器为:" + (String) this.props.get("mail.smtp.host")); transport.sendMessage(this.mimeMsg, this.mimeMsg.getRecipients(Message.RecipientType.TO)); // transport.send(mimeMsg); System.out.println("发送邮件成功!"); transport.close(); return true; } catch (final Exception e) { System.err.println("邮件发送失败!" + e); return false; } } public boolean setBody(final String mailBody) { try { final BodyPart bp = new MimeBodyPart(); bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=utf-8>" + mailBody, "text/html;charset=utf-8"); this.mp.addBodyPart(bp); return true; } catch (final Exception e) { System.err.println("设置邮件正文时发生错误!" + e); return false; } } public boolean setCopyTo(final String copyto) { if (copyto == null) { return false; } try { this.mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto)); return true; } catch (final Exception e) { return false; } } public boolean setFrom(final String from) { System.out.println("设置发信人!"); try { this.mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人 return true; } catch (final Exception e) { return false; } } public void setNamePass(final String name, final String pass) { this.username = name; this.password = pass; } public void setNeedAuth(final boolean need) { System.out.println("设置smtp身份认证:mail.smtp.auth = " + need); if (this.props == null) { this.props = System.getProperties(); } if (need) { this.props.put("mail.smtp.auth", "true"); } else { this.props.put("mail.smtp.auth", "false"); } } public void setSmtpHost(final String hostName) { System.out.println("设置系统属性:mail.smtp.host = " + hostName); if (this.props == null) { this.props = System.getProperties(); // 获得系统属性对象 } this.props.put("mail.smtp.host", hostName); // 设置SMTP主机 } public boolean setSubject(final String mailSubject) { System.out.println("设置邮件主题!"); try { this.mimeMsg.setSubject(mailSubject); return true; } catch (final Exception e) { System.err.println("设置邮件主题发生错误!"); return false; } } public boolean setTo(final String to) { if (to == null) { return false; } try { this.mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); return true; } catch (final Exception e) { return false; } } }

抱歉!评论已关闭.