| | |
| | | import net.sf.json.JSONObject; |
| | | |
| | | public class MailSenderUtil { |
| | | public static boolean sendTextMail(MailSendInfo mailInfo) { |
| | | MyAuthenticator authenticator = null; |
| | | Properties pro = mailInfo.getProperties(); |
| | | if (mailInfo.isValidate()) { |
| | | authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); |
| | | } |
| | | Session sendMailSession = Session.getDefaultInstance(pro, authenticator); |
| | | try { |
| | | Message mailMessage = new MimeMessage(sendMailSession); |
| | | Address from = new InternetAddress(mailInfo.getFromAddress()); |
| | | mailMessage.setFrom(from); |
| | | Address to = new InternetAddress(mailInfo.getToAddress()); |
| | | mailMessage.setRecipient(Message.RecipientType.TO, to); |
| | | mailMessage.setSubject(mailInfo.getSubject()); |
| | | mailMessage.setSentDate(new Date()); |
| | | String mailContent = mailInfo.getContent(); |
| | | mailMessage.setText(mailContent); |
| | | Transport.send(mailMessage); |
| | | return true; |
| | | } catch (MessagingException ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | public static boolean sendTextMail(MailSendInfo mailInfo) { |
| | | MyAuthenticator authenticator = null; |
| | | Properties pro = mailInfo.getProperties(); |
| | | if (mailInfo.isValidate()) { |
| | | authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); |
| | | } |
| | | Session sendMailSession = Session.getDefaultInstance(pro, authenticator); |
| | | try { |
| | | Message mailMessage = new MimeMessage(sendMailSession); |
| | | Address from = new InternetAddress(mailInfo.getFromAddress()); |
| | | mailMessage.setFrom(from); |
| | | Address to = new InternetAddress(mailInfo.getToAddress()); |
| | | mailMessage.setRecipient(Message.RecipientType.TO, to); |
| | | mailMessage.setSubject(mailInfo.getSubject()); |
| | | mailMessage.setSentDate(new Date()); |
| | | String mailContent = mailInfo.getContent(); |
| | | mailMessage.setText(mailContent); |
| | | Transport.send(mailMessage); |
| | | return true; |
| | | } catch (MessagingException ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public static boolean sendHtmlMail(MailSendInfo mailInfo) { |
| | | MyAuthenticator authenticator = null; |
| | | Properties pro = mailInfo.getProperties(); |
| | | if (mailInfo.isValidate()) { |
| | | authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); |
| | | } |
| | | Session sendMailSession = Session.getDefaultInstance(pro, authenticator); |
| | | try { |
| | | Message mailMessage = new MimeMessage(sendMailSession); |
| | | Address from = new InternetAddress(mailInfo.getFromAddress()); |
| | | mailMessage.setFrom(from); |
| | | Address to = new InternetAddress(mailInfo.getToAddress()); |
| | | mailMessage.setRecipient(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); |
| | | return true; |
| | | } catch (MessagingException ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | public static boolean sendHtmlMail(MailSendInfo mailInfo) { |
| | | MyAuthenticator authenticator = null; |
| | | Properties pro = mailInfo.getProperties(); |
| | | if (mailInfo.isValidate()) { |
| | | authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); |
| | | } |
| | | Session sendMailSession = Session.getDefaultInstance(pro, authenticator); |
| | | try { |
| | | Message mailMessage = new MimeMessage(sendMailSession); |
| | | Address from = new InternetAddress(mailInfo.getFromAddress()); |
| | | mailMessage.setFrom(from); |
| | | Address to = new InternetAddress(mailInfo.getToAddress()); |
| | | mailMessage.setRecipient(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); |
| | | return true; |
| | | } catch (MessagingException ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public static boolean sendEmail(String email, String title, String content) { |
| | | String username = "he15901227708@163.com"; |
| | | String pwd = "hexiaohui1011"; |
| | | MailSendInfo mailInfo = new MailSendInfo(); |
| | | mailInfo.setMailServerHost("smtp." + username.split("@")[1]); |
| | | mailInfo.setMailServerPort("25"); |
| | | mailInfo.setValidate(true); |
| | | public static boolean sendEmail(String email, String title, String content) { |
| | | String username = "he15901227708@163.com"; |
| | | String pwd = "hexiaohui1011"; |
| | | MailSendInfo mailInfo = new MailSendInfo(); |
| | | mailInfo.setMailServerHost("smtp." + username.split("@")[1]); |
| | | mailInfo.setMailServerPort("25"); |
| | | mailInfo.setValidate(true); |
| | | |
| | | mailInfo.setUserName(username); |
| | | mailInfo.setPassword(pwd); |
| | | mailInfo.setFromAddress(username); |
| | | mailInfo.setToAddress(email); |
| | | mailInfo.setSubject(title); |
| | | StringBuffer buffer = new StringBuffer(); |
| | | buffer.append(content); |
| | | mailInfo.setContent(buffer.toString()); |
| | | mailInfo.setUserName(username); |
| | | mailInfo.setPassword(pwd); |
| | | mailInfo.setFromAddress(username); |
| | | mailInfo.setToAddress(email); |
| | | mailInfo.setSubject(title); |
| | | StringBuffer buffer = new StringBuffer(); |
| | | buffer.append(content); |
| | | mailInfo.setContent(buffer.toString()); |
| | | |
| | | return sendTextMail(mailInfo); |
| | | } |
| | | return sendTextMail(mailInfo); |
| | | } |
| | | |
| | | public static boolean sendEmail(String toEmail, String fromEmail, String fromEmailPwd, String title, |
| | | String content) { |
| | | try { |
| | | String url = String.format( |
| | | "http://193.112.34.40:9001/BuWan/email/sendEmail?fromAccount=%s&fromPwd=%s&toAccount=%s&title=%s&content=%s", |
| | | URLEncoder.encode(fromEmail, "UTF-8"), URLEncoder.encode(fromEmailPwd, "UTF-8"), |
| | | URLEncoder.encode(toEmail, "UTF-8"), URLEncoder.encode(title, "UTF-8"), |
| | | URLEncoder.encode(content, "UTF-8")); |
| | | String result = HttpUtil.get(url); |
| | | JSONObject data = JSONObject.fromObject(result); |
| | | if (data.optInt("code") == 0) |
| | | return true; |
| | | else |
| | | return false; |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | public static boolean sendEmail(String toEmail, String fromEmail, String fromEmailPwd, String title, |
| | | String content) { |
| | | try { |
| | | String url = String.format( |
| | | "http://193.112.34.40:9001/BuWan/email/sendEmail?fromAccount=%s&fromPwd=%s&toAccount=%s&title=%s&content=%s", |
| | | URLEncoder.encode(fromEmail, "UTF-8"), URLEncoder.encode(fromEmailPwd, "UTF-8"), |
| | | URLEncoder.encode(toEmail, "UTF-8"), URLEncoder.encode(title, "UTF-8"), |
| | | URLEncoder.encode(content, "UTF-8")); |
| | | System.out.println(url); |
| | | String result = HttpUtil.get(url); |
| | | JSONObject data = JSONObject.fromObject(result); |
| | | if (data.optInt("code") == 0) |
| | | return true; |
| | | else |
| | | return false; |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return false; |
| | | return false; |
| | | |
| | | // String username = fromEmail; |
| | | // String pwd = fromEmailPwd; |
| | | // MailSendInfo mailInfo = new MailSendInfo(); |
| | | // mailInfo.setMailServerHost("smtp." + username.split("@")[1]); |
| | | // mailInfo.setMailServerPort("25"); |
| | | // mailInfo.setValidate(true); |
| | | // mailInfo.setUserName(username); |
| | | // mailInfo.setPassword(pwd); |
| | | // mailInfo.setFromAddress(username); |
| | | // mailInfo.setToAddress(toEmail); |
| | | // mailInfo.setSubject(title); |
| | | // StringBuffer buffer = new StringBuffer(); |
| | | // buffer.append(content); |
| | | // mailInfo.setContent(buffer.toString()); |
| | | // return sendTextMail(mailInfo); |
| | | } |
| | | // String username = fromEmail; |
| | | // String pwd = fromEmailPwd; |
| | | // MailSendInfo mailInfo = new MailSendInfo(); |
| | | // mailInfo.setMailServerHost("smtp." + username.split("@")[1]); |
| | | // mailInfo.setMailServerPort("25"); |
| | | // mailInfo.setValidate(true); |
| | | // mailInfo.setUserName(username); |
| | | // mailInfo.setPassword(pwd); |
| | | // mailInfo.setFromAddress(username); |
| | | // mailInfo.setToAddress(toEmail); |
| | | // mailInfo.setSubject(title); |
| | | // StringBuffer buffer = new StringBuffer(); |
| | | // buffer.append(content); |
| | | // mailInfo.setContent(buffer.toString()); |
| | | // return sendTextMail(mailInfo); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String fromEmail="ohaicao@163.com"; |
| | | String fromEmailPwd="admin123456"; |
| | | String title="测试标题"; |
| | | String content="测试内容"; |
| | | sendEmail("liuhongrui@banliapp.com",fromEmail,fromEmailPwd,title,content); |
| | | } |
| | | } |