use of javax.mail.internet.MimeMessage in project acceptance-test-harness by jenkinsci.
the class MailService method assertMail.
/**
* Checks that the mail has arrived.
*/
public void assertMail(final Pattern subject, String recipient, Pattern body) throws MessagingException, IOException {
CapybaraPortingLayer hackish = new CapybaraPortingLayerImpl(World.get().getInjector());
MimeMessage msg = hackish.waitFor().withMessage("Email whose subject matches: %s", subject).pollingEvery(5, TimeUnit.SECONDS).until(new MailArrives(subject));
String actualRecipients = Joiner.on(' ').join(msg.getRecipients(TO));
assertThat("recipient", actualRecipients, is(recipient));
Object c = msg.getContent();
if (c instanceof MimeMultipart) {
MimeMultipart content = (MimeMultipart) c;
c = content.getBodyPart(0).getContent();
}
assertThat(c.toString(), containsRegexp(body));
}
use of javax.mail.internet.MimeMessage in project cryptonomica by Cryptonomica.
the class ReceiveMailService method processReceivedEmail.
public static void processReceivedEmail(HttpServletRequest request) throws IOException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
MimeMessage message = new MimeMessage(session, request.getInputStream());
// Call getContentType() to extract the message content type.
// The getContent() method returns an object that implements the Multipart interface.
// Call getCount() to determine the number of parts
// Call getBodyPart(int index) to return a particular body part.
// or:
String messageString = IOUtils.toString(message.getInputStream(), String.valueOf(StandardCharsets.UTF_8));
LOG.warning("Received e-mail message from: " + message.getFrom()[0]);
LOG.warning(messageString);
String githubEmaiAddress = ApiKeysUtils.getApiKey("githubEmaiAddress");
if (message.getFrom()[0].toString().equalsIgnoreCase(githubEmaiAddress)) {
// String approvedHeaderValue = message.getHeader("Approved")[0];
processMailFromGithub(message);
}
} catch (MessagingException e) {
LOG.severe(e.getMessage());
}
}
use of javax.mail.internet.MimeMessage in project cryptonomica by Cryptonomica.
the class SendCustomMessageToEmailGAE method doPost.
/**
* --- Send mail
* params:
* "email"
* "emailCC"
* "messageText"
* "messageSubject"
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String emailCC = request.getParameter("emailCC");
String messageSubject = request.getParameter("messageSubject");
String messageText = request.getParameter("messageText");
//
LOG.warning("email: " + email);
LOG.warning("messageText: " + messageText);
LOG.warning("messageSubject: " + messageSubject);
// get session:
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
//
try {
Message message = new MimeMessage(session);
InternetAddress from = new InternetAddress(// String.format("noreply@%s.appspotmail.com", SystemProperty.applicationId.get()),
"admin@international-arbitration.org.uk", "Cryptonomica");
message.setFrom(from);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, ""));
message.addRecipient(Message.RecipientType.BCC, new InternetAddress("admin@international-arbitration.org.uk", ""));
if (emailCC != null) {
message.addRecipient(Message.RecipientType.CC, new InternetAddress(emailCC, ""));
}
message.setSubject(messageSubject);
message.setText(messageText);
// <<------------------------|
Transport.send(message);
} catch (MessagingException e) {
LOG.log(Level.WARNING, String.format("Failed to send an mail to %s", email), e);
throw new RuntimeException(e);
}
}
use of javax.mail.internet.MimeMessage in project uavstack by uavorg.
the class JavaMailAction method buildMailMessage.
private MimeMessage buildMailMessage(Session session, NotificationEvent notifyEvent, String title, String mailTemplatePath) throws MessagingException {
MimeMessage message = new MimeMessage(session);
// 邮件标题
message.setSubject(title);
String html = IOHelper.readTxtFile(mailTemplatePath, "utf-8");
html = buildMailBody(html, notifyEvent);
// 正文
message.setContent(html, "text/html;charset=utf-8");
// 发件人
message.setFrom(username);
String addressStr = notifyEvent.getArg(cName);
String[] toAddr = addressStr.split(",");
Address[] receiver = new Address[toAddr.length];
int i = 0;
for (String addr : toAddr) {
if (!StringHelper.isEmpty(addr)) {
receiver[i] = new InternetAddress(addr);
i++;
}
}
// 收件人
message.setRecipients(MimeMessage.RecipientType.TO, receiver);
message.saveChanges();
return message;
}
use of javax.mail.internet.MimeMessage in project BIMserver by opensourceBIM.
the class EmailMessage method send.
public void send() throws MessagingException, UserException {
Properties props = new Properties();
ServerSettings serverSettings = bimServer.getServerSettingsCache().getServerSettings();
props.put("mail.smtp.localhost", "bimserver.org");
String smtpProps = serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS ? "mail.smtps.port" : "mail.smtp.port";
props.put("mail.smtp.connectiontimeout", 10000);
props.put("mail.smtp.timeout", 10000);
props.put("mail.smtp.writetimeout", 10000);
props.put(smtpProps, serverSettings.getSmtpPort());
if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
props.put("mail.smtp.starttls.enable", "true");
}
Session mailSession = Session.getInstance(props);
Transport transport = null;
try {
if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTP) {
transport = new SMTPTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else {
throw new RuntimeException("Unimplemented SMTP protocol: " + serverSettings.getSmtpProtocol());
}
transport.connect(serverSettings.getSmtpServer(), serverSettings.getSmtpPort(), serverSettings.getSmtpUsername(), serverSettings.getSmtpPassword());
Message message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setRecipients(to, addressTo);
message.setContent(body, contentType);
message.setFrom(from);
transport.sendMessage(message, addressTo);
} catch (MessagingException e) {
LOGGER.error("Error sending email " + body + " " + e.getMessage());
throw new UserException("Error sending email " + e.getMessage());
}
}
Aggregations