Search in sources :

Example 86 with MimeMessage

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));
}
Also used : CapybaraPortingLayer(org.jenkinsci.test.acceptance.po.CapybaraPortingLayer) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) CapybaraPortingLayerImpl(org.jenkinsci.test.acceptance.po.CapybaraPortingLayerImpl)

Example 87 with MimeMessage

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());
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Session(javax.mail.Session)

Example 88 with MimeMessage

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);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Session(javax.mail.Session)

Example 89 with MimeMessage

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;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage)

Example 90 with MimeMessage

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());
    }
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) URLName(javax.mail.URLName) Properties(java.util.Properties) SMTPTransport(com.sun.mail.smtp.SMTPTransport) SMTPSSLTransport(com.sun.mail.smtp.SMTPSSLTransport) MimeMessage(javax.mail.internet.MimeMessage) ServerSettings(org.bimserver.models.store.ServerSettings) UserException(org.bimserver.shared.exceptions.UserException) Transport(javax.mail.Transport) SMTPSSLTransport(com.sun.mail.smtp.SMTPSSLTransport) SMTPTransport(com.sun.mail.smtp.SMTPTransport) Session(javax.mail.Session)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70