首页 > 代码库 > 邮件推送 Java代码
邮件推送 Java代码
package mail; /** * @Description:邮件信息类 * * @ClassName: SimpleMail */ public class Mail { /** * 主题 */ private String subject; /** * 内容 */ private String content; /** * @return the subject */ public String getSubject() { return subject; } /** * @param subject * the subject to set */ public void setSubject(String subject) { this.subject = subject; } /** * @return the content */ public String getContent() { return content; } /** * @param content * the content to set */ public void setContent(String content) { this.content = content; } }
package mail; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * @Description: 邮箱登录类 * * @ClassName: MailAuthenticator */ public class MailAuthenticator extends Authenticator { /** * 用户名(登录邮箱) */ private String username; /** * 密码 */ private String password; /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @param username * @param password */ public MailAuthenticator(String username, String password) { this.username = username; this.password = password; } @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }
package mail; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * @Description: 邮件发送常量类 * * @ClassName: MailConstant */ public class MailConstant { public static final String MAIL_USER = "*******"; //公共邮箱 public static final String MAIL_PWD = "*******"; //公共邮箱密码 public static final boolean MAIL_IFDEBUG = true; public static final String MAIL_CONTENT_CHARSET = "text/html;charset=utf-8"; public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E"); public static final String MAIL_TITLE = "****8" + sdf.format(new Date());//邮件标题 public static Object getMailContent(List<MessageEntity> list){ StringBuffer sb = new StringBuffer(); sb.append("<div style=‘width:1024px;height:auto;margin:0px auto;background-color:#66CCFF;font-size:14px;font-family:微软雅黑;border-radius:5px;padding:5px;‘><center><h1>"); sb.append("</h1></center><div style=‘margin-left:20px;margin-bottom:10px;‘><b>邮件标题</b><br/><br/>"); sb.append("<table border=‘1‘>"); for(int i=0;i<list.size();i++){ sb.append("<tr>" + "<td>" + list.get(i).getName() + "</td>" + "<td>" + list.get(i).getpName() + "</td>" + "<td>" + list.get(i).getAge() + "</td>" + "<td>" + list.get(i).getAddress() + "</td>" + "</tr>"); } sb.append("</table>"); sb.append("</div></div>"); return sb.toString(); } }
package mail; import java.io.File; import java.util.Date; import java.util.List; import java.util.Properties; import java.util.Vector; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; 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; /** * @Description:邮件发送类 * */ public class MailSender { /** * 发送邮件的props文件 */ private final transient Properties props = new Properties(); /** * 邮件服务器登录验证 */ private transient MailAuthenticator authenticator; /** * 邮箱session */ private transient Session session; public MailSender(){ } /** * 初始化邮件发送器 * * @param smtpHostName * SMTP邮件服务器地址 * @param username * 发送邮件的用户名(地址) * @param password * 发送邮件的密码 */ public MailSender(final String smtpHostName, final String username, final String password) { init(username, password, smtpHostName); } /** * 初始化邮件发送器 * * @param username * 发送邮件的用户名(地址),并以此解析SMTP服务器地址 * @param password * 发送邮件的密码 */ public MailSender(final String username, final String password) { // 通过邮箱地址解析出smtp服务器,对大多数邮箱都管用 final String smtpHostName = "smtp." + username.split("@")[1]; init(username, password, smtpHostName); } /** * @Description: 初始化 * * @param username * 发送邮件的用户名(地址) * @param password * 密码 * @param smtpHostName * SMTP主机地址 * */ private void init(String username, String password, String smtpHostName) { // 初始化props props.put("mail.smtp.host", smtpHostName); props.put("mail.smtp.auth", "true"); // 验证 authenticator = new MailAuthenticator(username, password); // 创建session session = Session.getInstance(props, authenticator); // 打印一些调试信息 session.setDebug(MailConstant.MAIL_IFDEBUG); } /** * @Description: 发送邮件 * * @param recipient * 收件人邮箱地址 * @param subject * 邮件主题 * @param content * 邮件内容 * @throws AddressException * @throws MessagingException * * @Title: MailSender.java */ public void send(String recipient, String subject, Object content) throws Exception { send(recipient, subject, content, null); } /** * 发送邮件 * * @param recipient * 收件人邮箱地址 * @param subject * 邮件主题 * @param content * 邮件内容 * @param files * 附件 * @throws Exception */ public void send(String recipient, String subject, Object content, Vector<File> files) throws Exception { // 创建mime类型邮件 final MimeMessage message = new MimeMessage(session); // 设置发信人 message.setFrom(new InternetAddress(authenticator.getUsername(),"*****邮件标题****","UTF-8")); // 设置收件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress( recipient)); // 设置主题 message.setSubject(subject); // 设置邮件内容 if (null == files || files.size() == 0) { message.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET); } else { //创建 Mimemultipart添加内容(可包含多个附件) MimeMultipart multipart = new MimeMultipart(); //MimeBodyPart(用于信件内容/附件) BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET); //添加到MimeMultipart对象中 multipart.addBodyPart(bodyPart); for (int i = 0; i < files.size(); i++) { File file = (File) files.elementAt(i); String fname = file.getName(); //创建FileDAtaSource(用于添加附件) FileDataSource fds = new FileDataSource(file); BodyPart fileBodyPart = new MimeBodyPart(); // 字符流形式装入文件 fileBodyPart.setDataHandler(new DataHandler(fds)); // 设置附件文件名 fileBodyPart.setFileName(fname); multipart.addBodyPart(fileBodyPart); message.setContent(multipart); } } // 设置发信时间 message.setSentDate(new Date()); // 存储邮件信息 message.saveChanges(); // message.setFileName(filename) // 发送邮件 Transport.send(message); } /** * @Description: 群发邮件 * * @param recipients * 收件人们 * @param subject * 主题 * @param content * 内容 * @throws AddressException * @throws MessagingException */ public void send(List<String> recipients, String subject, Object content) throws Exception { send(recipients, subject, content, null); } /** * 群发邮件 * * @param recipients * 收件人们 * @param subject * 主题 * @param content * 内容 * @param files * 附件 * @throws Exception */ public void send(List<String> recipients, String subject, Object content, Vector<File> files) throws Exception { // 创建mime类型邮件 final MimeMessage message = new MimeMessage(session); // 设置发信人 message.setFrom(new InternetAddress(authenticator.getUsername())); // 设置收件人们 final int num = recipients.size(); InternetAddress[] addresses = new InternetAddress[num]; for (int i = 0; i < num; i++) { addresses[i] = new InternetAddress(recipients.get(i)); } message.setRecipients(Message.RecipientType.TO, addresses); // 设置主题 message.setSubject(subject); // 设置邮件内容 if (null == files || files.size() == 0) { message.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET); } else { //创建 Mimemultipart添加内容(可包含多个附件) MimeMultipart multipart = new MimeMultipart(); //MimeBodyPart(用于信件内容/附件) BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET); //添加到MimeMultipart对象中 multipart.addBodyPart(bodyPart); for (int i = 0; i < files.size(); i++) { File file = (File) files.elementAt(i); String fname = file.getName(); //创建FileDAtaSource(用于添加附件) FileDataSource fds = new FileDataSource(file); BodyPart fileBodyPart = new MimeBodyPart(); // 字符流形式装入文件 fileBodyPart.setDataHandler(new DataHandler(fds)); // 设置附件文件名 fname = new String(fname.getBytes("UTF-8"), "ISO-8859-1"); fileBodyPart.setFileName(fname); multipart.addBodyPart(fileBodyPart); message.setContent(multipart); } } // 设置发信时间 message.setSentDate(new Date()); // 存储邮件信息 message.saveChanges(); // 发送邮件 Transport.send(message); } /** * @Description: 发送邮件 * * @param recipient * 收件人邮箱地址 * @param mail * 邮件对象 * @throws Exception * * @Title: MailSender.java */ public void send(String recipient, Mail mail) throws Exception { send(recipient, mail.getSubject(), mail.getContent()); } /** * @Description: 群发邮件 * * @param recipients * 收件人们 * @param mail * 邮件对象 * @throws Exception * * @Title: MailSender.java */ public void send(List<String> recipients, Mail mail) throws Exception { send(recipients, mail.getSubject(), mail.getContent()); } /** * 群发邮件 * * @param recipients * 收件人们 * @param mail * 邮件对象 * @param files * 附件 * @throws Exception */ public void send(List<String> recipients, Mail mail, Vector<File> files) throws Exception { send(recipients, mail.getSubject(), mail.getContent(), files); } }
package mail; /** * @Description: 随机码工具类 * * @ClassName: RandomCodeUtil * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-5-28 上午11:07:34 * @version V2.0 */ public class RandomCodeUtil { /** * 随机码集合 */ private static final String[] randCode = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m" }; /** * @Description: 产生指定长度的随机码 * * @param codeLength * @return * * @Title: RandomCodeUtil.java */ public static String randomCode(Integer codeLength) throws Exception { try { StringBuffer code = new StringBuffer(); if(null == codeLength || 0 == codeLength){ codeLength = 4; } for (int i = 0; i < codeLength; i++) { code.append(randCode[(int)Math.floor(Math.random() * 36)]); } return code.toString(); } catch (Exception e) { throw new RuntimeException("Random Code Error"); } } /** * @Description: 生成长度为4的随机码 * * @return * @throws Exception * * @Title: RandomCodeUtil.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-5-28 下午01:19:33 * @version V2.0 */ public static String randomCode() throws Exception{ return randomCode(null); } }
邮件推送 Java代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。