/** * 验证Session,进行发送信息 */ private static void createMessage(Session session){ Message message = new MimeMessage(session);// 2, 创建代表邮件的对象Message try { message.setFrom(new InternetAddress(username));// 设置发件人 message.addRecipient(RecipientType.TO, new InternetAddress(Receiver)); // 设置收件人 message.setSubject(title);// 设置标题 message.setSentDate(new Date());// 设置发送时间 // 设置正文(有链接选择text/html;charset=utf-8) message.setContent(contents, "text/html;charset=utf-8"); Transport.send(message);// 3,发送邮件Transport } catch (Exception e) { e.printStackTrace(); } }
public void fill(MimeMessage message) throws EmailException { try { message.setFrom(from()); message.setRecipients(RecipientType.TO, to().toArray(new Address[0])); message.setRecipients(RecipientType.CC, cc().toArray(new Address[0])); message.setRecipients(RecipientType.BCC, bcc().toArray(new Address[0])); message.setSubject(subject()); message.setContent(content(), contentType()); } catch (MessagingException exception) { LOGGER.error("Fill mime message", exception); throw new EmailException(exception); } }
public void onCreateBestellung(@Observes @NeueBestellung Bestellung bestellung) { final Kunde kunde = bestellung.getKunde(); final String empfaengerMail = kunde.getEmail(); if (absenderMail == null || empfaengerMail == null) { return; } final String vorname = kunde.getVorname() == null ? "" : kunde.getVorname(); final String empfaengerName = vorname + " " + kunde.getName(); final MimeMessage message = new MimeMessage(session); try { final InternetAddress absenderObj = new InternetAddress(absenderMail, absenderName); message.setFrom(absenderObj); // Empfaenger setzen final InternetAddress empfaenger = new InternetAddress(empfaengerMail, empfaengerName); message.setRecipient(RecipientType.TO, empfaenger); // Subject setzen message.setSubject("Neue Bestellung Nr. " + bestellung.getID()); // Text setzen mit MIME Type "text/plain" final StringBuilder sb = new StringBuilder(256); sb.append("<h3>Neue Bestellung Nr. <b>" + bestellung.getID() + "</b></h3>" + NEWLINE); for (Position p : bestellung.getPositionen()) { sb.append(p.getAnzahl() + "\t" + p.getArtikel().getBezeichnung() + "<br/>" + NEWLINE); } final String text = sb.toString(); LOGGER.trace(text); message.setContent(text, "text/html;charset=iso-8859-1"); Transport.send(message); } catch (MessagingException | UnsupportedEncodingException e) { LOGGER.error(e.getMessage()); return; } }
/** * 用来发送邮件 * * @param session 与发送邮件的主机的连接(会话) * @param email 写好的邮件 * @throws Exception 出现异常 */ public static void sendEmail(Session session, Email email) throws Exception { // 获取发送邮件的信息类 MimeMessage message = new MimeMessage(session); // 设置发送方邮件地址 message.setFrom(new InternetAddress(email.getFrom())); // 设置发送类型和被发送方的邮件地址 if (!email.getTo().isEmpty()) { message.setRecipients(RecipientType.TO, email.getTo()); } if (!email.getCc().isEmpty()) { message.setRecipients(RecipientType.CC, email.getCc()); } if (!email.getBcc().isEmpty()) { message.setRecipients(RecipientType.BCC, email.getBcc()); } // 设置邮件主题 message.setSubject(email.getSubject(), "utf-8"); // 设置邮件内容 MimeMultipart content = new MimeMultipart(); // 邮件正文 MimeBodyPart text = new MimeBodyPart(); text.setContent(email.getContent(), email.getType()); content.addBodyPart(text); // 设置附件 if (email.getAttachments() != null) { for (AttachmentBean attachment : email.getAttachments()) { MimeBodyPart part = new MimeBodyPart(); part.attachFile(attachment.getFile()); part.setFileName(MimeUtility.encodeText(attachment .getFileName())); if (attachment.getCid() != null) { part.setContentID(attachment.getCid()); } content.addBodyPart(part); } } // 将邮件内容添加到信息中 message.setContent(content); // 发送邮件 Transport.send(message); }
public void email(String text, TregminePlayer p){ final String username = tregmine.getConfig().getString("support.user"); final String password = tregmine.getConfig().getString("support.password"); final List<?> to = tregmine.getConfig().getList("support.to"); String[] recipients = to.toArray(new String[to.size()]); String time = String.format("[%tm/%td/%ty - %tH:%tM:%tS] ", new Date(), new Date(),new Date(),new Date(),new Date(),new Date()); Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password); } }); try { InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } Message message = new MimeMessage(session); message.setRecipients(RecipientType.TO, addressTo); message.setSubject("[Help Request] from " + p.getName()); message.setText( "Help request from: " + p.getName() + " at " + time + "\n" + "\n" + "Message: " + text + "\n" ); Transport.send(message); p.sendMessage(ChatColor.GREEN + "Help request sent, we will get back to you ASAP :)"); } catch (MessagingException e) { p.sendMessage(ChatColor.RED + "Error sending message, please try again."); throw new RuntimeException(e); } }