Search in sources :

Example 1 with Address

use of javax.mail.Address in project camel by apache.

the class MailProducerTest method testProducerBodyIsMimeMessage.

@Test
public void testProducerBodyIsMimeMessage() throws Exception {
    Mailbox.clearAll();
    getMockEndpoint("mock:result").expectedMessageCount(1);
    Address from = new InternetAddress("fromCamelTest@localhost");
    Address to = new InternetAddress("recipient2@localhost");
    Session session = Session.getDefaultInstance(System.getProperties());
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(from);
    mimeMessage.addRecipient(RecipientType.TO, to);
    mimeMessage.setSubject("This is the subject.");
    mimeMessage.setText("This is the message");
    template.sendBodyAndHeader("direct:start", mimeMessage, "To", "someone@localhost");
    assertMockEndpointsSatisfied();
    // need to check the message header
    Exchange exchange = getMockEndpoint("mock:result").getExchanges().get(0);
    assertNotNull("The message id should not be null", exchange.getIn().getHeader(MailConstants.MAIL_MESSAGE_ID));
    Mailbox box = Mailbox.get("someone@localhost");
    assertEquals(0, box.size());
    // Check if the mimeMessagea has override body and headers
    Mailbox box2 = Mailbox.get("recipient2@localhost");
    assertEquals(1, box2.size());
}
Also used : Exchange(org.apache.camel.Exchange) InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) Mailbox(org.jvnet.mock_javamail.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Session(javax.mail.Session) Test(org.junit.Test)

Example 2 with Address

use of javax.mail.Address in project camel by apache.

the class MailRouteTest method assertMailboxReceivedMessages.

protected void assertMailboxReceivedMessages(String name) throws IOException, MessagingException {
    Mailbox mailbox = Mailbox.get(name);
    assertEquals(name + " should have received 1 mail", 1, mailbox.size());
    Message message = mailbox.get(0);
    assertNotNull(name + " should have received at least one mail!", message);
    assertEquals("hello world!", message.getContent());
    assertEquals("camel@localhost", message.getFrom()[0].toString());
    boolean found = false;
    for (Address adr : message.getRecipients(RecipientType.TO)) {
        if (name.equals(adr.toString())) {
            found = true;
        }
    }
    assertTrue("Should have found the recpient to in the mail: " + name, found);
}
Also used : Mailbox(org.jvnet.mock_javamail.Mailbox) Message(javax.mail.Message) Address(javax.mail.Address)

Example 3 with Address

use of javax.mail.Address in project jodd by oblac.

the class ReceivedEmail method parseMessage.

/**
	 * Parse java <code>Message</code> and extracts all data for the received message.
	 */
@SuppressWarnings("unchecked")
protected void parseMessage(Message msg) throws MessagingException, IOException {
    // flags
    setFlags(msg.getFlags());
    // msg no
    setMessageNumber(msg.getMessageNumber());
    // single from
    Address[] addresses = msg.getFrom();
    if (addresses != null && addresses.length > 0) {
        setFrom(new EmailAddress(addresses[0]));
    }
    // common field
    setTo(EmailAddress.createFrom(msg.getRecipients(Message.RecipientType.TO)));
    setCc(EmailAddress.createFrom(msg.getRecipients(Message.RecipientType.CC)));
    setBcc(EmailAddress.createFrom(msg.getRecipients(Message.RecipientType.BCC)));
    // reply to
    setReplyTo(EmailAddress.createFrom(msg.getReplyTo()));
    setSubject(msg.getSubject());
    setReceiveDate(parseReceiveDate(msg));
    setSentDate(parseSendDate(msg));
    // copy headers
    Enumeration<Header> headers = msg.getAllHeaders();
    while (headers.hasMoreElements()) {
        Header header = headers.nextElement();
        setHeader(header.getName(), header.getValue());
    }
    // content
    processPart(this, msg);
}
Also used : Address(javax.mail.Address) Header(javax.mail.Header)

Example 4 with Address

use of javax.mail.Address in project Xponents by OpenSextant.

the class MessageConverter method setMailAttributes.

/**
     * Copy innate Message metadata into the ConvertedDocument properties to save that metadata in the normal place.
     * This metadata will also be replicated down through children items to reflect the fact the attachment was sent via this message.
     *
     * @param msgdoc  doc conversion
     * @param message  original mail message
     * @throws MessagingException on err
     */
private void setMailAttributes(ConvertedDocument msgdoc, Message message) throws MessagingException {
    String msg_id = getMessageID(message);
    if (msg_id == null) {
        return;
    }
    msgdoc.id = getShorterMessageID(msg_id);
    String mailSubj = message.getSubject();
    msgdoc.addTitle(mailSubj);
    Address[] sender = message.getFrom();
    String sender0 = null;
    if (sender != null && sender.length > 0) {
        sender0 = sender[0].toString();
        msgdoc.addAuthor(sender0);
    }
    Date d = message.getSentDate();
    String dt = (d != null ? d.toString() : "");
    msgdoc.addCreateDate(d != null ? d : msgdoc.filetime);
    msgdoc.addUserProperty(MAIL_KEY_PREFIX + "msgid", msg_id);
    msgdoc.addUserProperty(MAIL_KEY_PREFIX + "sender", sender0);
    msgdoc.addUserProperty(MAIL_KEY_PREFIX + "date", dt);
    msgdoc.addUserProperty(MAIL_KEY_PREFIX + "subject", mailSubj);
}
Also used : Address(javax.mail.Address) Date(java.util.Date)

Example 5 with Address

use of javax.mail.Address in project Axe by DongyuCai.

the class SimpleMailSender method sendHtmlMail.

/**
	 * 以HTML格式发送邮件
	 * 
	 * @param mailInfo
	 *            待发送的邮件信息
	 */
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
    // 判断是否需要身份认证
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    // 如果需要身份认证,则创建一个密码验证器
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
    try {
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 创建邮件发送者地址
        Address from = new InternetAddress(mailInfo.getFromAddress());
        // 设置邮件消息的发送者
        mailMessage.setFrom(from);
        // 创建邮件的接收者地址,并设置到邮件消息中
        Address to = new InternetAddress(mailInfo.getToAddress());
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        // 设置邮件消息的主题
        mailMessage.setSubject(mailInfo.getSubject());
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        mailMessage.setContent(mainPart);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Aggregations

Address (javax.mail.Address)194 InternetAddress (javax.mail.internet.InternetAddress)150 MimeMessage (javax.mail.internet.MimeMessage)67 MessagingException (javax.mail.MessagingException)58 ArrayList (java.util.ArrayList)36 Date (java.util.Date)29 Test (org.junit.Test)26 IOException (java.io.IOException)25 AddressException (javax.mail.internet.AddressException)25 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)22 Message (javax.mail.Message)22 Properties (java.util.Properties)19 MimeBodyPart (javax.mail.internet.MimeBodyPart)19 Session (javax.mail.Session)17 MimeMultipart (javax.mail.internet.MimeMultipart)14 Account (com.zimbra.cs.account.Account)12 Locale (java.util.Locale)12 HashMap (java.util.HashMap)11 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)10 Multipart (javax.mail.Multipart)10