java jdk email 博客分类: java utils
程序员文章站
2024-03-14 08:47:40
...
public class SimpleMailSender { public SimpleMailSender() { } public static boolean sendTextMail(MailSenderInfo mailInfo) { JavaingAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new JavaingAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro, authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); javax.mail.Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); javax.mail.Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(javax.mail.Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); Transport.send(mailMessage); } catch (MessagingException ex) { ex.printStackTrace(); return false; } return true; } public static boolean sendHtmlMail(MailSenderInfo mailInfo) { JavaingAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new JavaingAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro, authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); javax.mail.Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); javax.mail.Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(javax.mail.Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); Transport.send(mailMessage); } catch (MessagingException ex) { ex.printStackTrace(); return false; } return true; } } public class MailSenderInfo { private String mailServerHost; private String mailServerPort; private String fromAddress; private String toAddress; private String userName; private String password; private boolean validate; private String subject; private String content; private String attachFileNames[]; public MailSenderInfo() { mailServerPort = "25"; validate = false; } public Properties getProperties() { Properties properties = new Properties(); properties.put("mail.smtp.host", mailServerHost); properties.put("mail.smtp.port", mailServerPort); properties.put("mail.smtp.auth", validate ? "true" : "false"); return properties; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String fileNames[]) { attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { content = textContent; } }
推荐阅读
-
java TimeZone 时区 博客分类: java utils
-
java jdk email 博客分类: java utils
-
排序算法 博客分类: java内功 简单排序算法
-
如何动态打开verbose class 博客分类: java java
-
Java Annotation注解 博客分类: java java
-
java : mozilla rhino jsc 编译 博客分类: javajavascript javarhinojsc
-
排序算法 博客分类: java内功 简单排序算法
-
java : mozilla rhino js 打开文件 博客分类: javajavascript javajsrhinoopen
-
java commons-lang 博客分类: java java
-
java TimeZone 时区 博客分类: java utils