Search in sources :

Example 46 with Address

use of javax.mail.Address in project hub-alert by blackducksoftware.

the class EmailMessagingService method createMessage.

private Message createMessage(final String emailAddress, final String subjectLine, final Session session, final MimeMultipart mimeMultipart, final EmailProperties emailProperties) throws MessagingException {
    final List<InternetAddress> addresses = new ArrayList<>();
    try {
        addresses.add(new InternetAddress(emailAddress));
    } catch (final AddressException e) {
        logger.warn(String.format("Could not create the address from %s: %s", emailAddress, e.getMessage()));
    }
    if (addresses.isEmpty()) {
        throw new RuntimeException("There were no valid email addresses supplied.");
    }
    final Message message = new MimeMessage(session);
    message.setContent(mimeMultipart);
    message.setFrom(new InternetAddress(emailProperties.getJavamailOption(EmailProperties.JAVAMAIL_FROM_KEY)));
    message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()]));
    message.setSubject(subjectLine);
    return message;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList)

Example 47 with Address

use of javax.mail.Address in project quickstart by wildfly.

the class Email method send.

/**
 * Method to send the email based upon values entered in the JSF view. Exception should be handled in a production usage but
 * is not handled in this example.
 *
 * @throws Exception
 */
public void send() throws Exception {
    Message message = new MimeMessage(mySession);
    message.setFrom(new InternetAddress(from));
    Address toAddress = new InternetAddress(to);
    message.addRecipient(Message.RecipientType.TO, toAddress);
    message.setSubject(subject);
    message.setContent(body, "text/plain");
    Transport.send(message);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage)

Example 48 with Address

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

the class MailUtils method dumpMessage.

/**
     * Gets a log dump of the given message that can be used for tracing etc.
     *
     * @param message the Mail message
     * @return a log string with important fields dumped
     */
public static String dumpMessage(Message message) {
    if (message == null) {
        return "null";
    }
    try {
        StringBuilder sb = new StringBuilder();
        int number = message.getMessageNumber();
        sb.append("messageNumber=[").append(number).append("]");
        Address[] from = message.getFrom();
        if (from != null) {
            for (Address adr : from) {
                sb.append(", from=[").append(adr).append("]");
            }
        }
        Address[] to = message.getRecipients(Message.RecipientType.TO);
        if (to != null) {
            for (Address adr : to) {
                sb.append(", to=[").append(adr).append("]");
            }
        }
        String subject = message.getSubject();
        if (subject != null) {
            sb.append(", subject=[").append(subject).append("]");
        }
        Date sentDate = message.getSentDate();
        if (sentDate != null) {
            sb.append(", sentDate=[").append(DateFormat.getDateTimeInstance().format(sentDate)).append("]");
        }
        Date receivedDate = message.getReceivedDate();
        if (receivedDate != null) {
            sb.append(", receivedDate=[").append(DateFormat.getDateTimeInstance().format(receivedDate)).append("]");
        }
        return sb.toString();
    } catch (MessagingException e) {
        // ignore the error and just return tostring 
        return message.toString();
    }
}
Also used : Address(javax.mail.Address) MessagingException(javax.mail.MessagingException) Date(java.util.Date)

Example 49 with Address

use of javax.mail.Address in project KJFrameForAndroid by kymjs.

the class SimpleMailSender method sendTextMail.

/**
     * 以文本格式发送邮件
     * 
     * @param mailInfo
     *            待发送的邮件的信息
     */
public boolean sendTextMail(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());
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        // 设置邮件消息的主题
        mailMessage.setSubject(mailInfo.getSubject());
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        // 设置邮件消息的主要内容
        String mailContent = mailInfo.getContent();
        mailMessage.setText(mailContent);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session)

Example 50 with Address

use of javax.mail.Address in project translationstudio8 by heartsome.

the class MailSender method setReplyTo.

/**
	 * 设置回复人,如果有多个回复人,用逗号分隔
	 * @param reply
	 * @throws MessagingException
	 * @throws EmailException ;
	 */
public void setReplyTo(String reply) throws MessagingException, EmailException {
    if (reply == null) {
        return;
    }
    Address[] address = InternetAddress.parse(reply);
    for (Address i : address) {
        Map<String, String> map = getEmailInfo(i.toString());
        email.addReplyTo(map.get("email"), map.get("name"));
    }
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress)

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