Search in sources :

Example 11 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 12 with Address

use of javax.mail.Address in project remusic by aa112901.

the class CommonUtils method sendTextMail.

/**
     * 以文本格式发送邮件
     * @param title 待发送的邮件的信息
     */
public static boolean sendTextMail(String title, String content) {
    try {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props, null);
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.163.com", 25, "remusic_log@163.com", "remusiclog1");
        Message mailMessage = new SMTPMessage(session);
        Address from = new InternetAddress("remusic_log@163.com");
        mailMessage.setFrom(from);
        Address to = new InternetAddress("remusic_log@163.com");
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        mailMessage.setSubject(title);
        mailMessage.setSentDate(new Date());
        mailMessage.setText(content);
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) SMTPMessage(com.sun.mail.smtp.SMTPMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Transport(javax.mail.Transport) Date(java.util.Date) Session(javax.mail.Session)

Example 13 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 14 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 15 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)89 InternetAddress (javax.mail.internet.InternetAddress)69 MessagingException (javax.mail.MessagingException)35 MimeMessage (javax.mail.internet.MimeMessage)34 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)21 ArrayList (java.util.ArrayList)20 Date (java.util.Date)19 MimeBodyPart (javax.mail.internet.MimeBodyPart)13 Account (com.zimbra.cs.account.Account)12 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)10 IOException (java.io.IOException)9 Locale (java.util.Locale)9 Message (javax.mail.Message)9 ItemId (com.zimbra.cs.service.util.ItemId)8 Header (javax.mail.Header)8 NHINDAddress (org.nhindirect.stagent.NHINDAddress)8 AddressException (javax.mail.internet.AddressException)7 MimeMultipart (javax.mail.internet.MimeMultipart)7 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)6 Invite (com.zimbra.cs.mailbox.calendar.Invite)6