Search in sources :

Example 6 with Address

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

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)

Example 7 with Address

use of javax.mail.Address in project hudson-2.x by hudson.

the class MailSender method execute.

public boolean execute(AbstractBuild<?, ?> build, BuildListener listener) throws InterruptedException {
    try {
        MimeMessage mail = getMail(build, listener);
        if (mail != null) {
            // if the previous e-mail was sent for a success, this new e-mail
            // is not a follow up
            AbstractBuild<?, ?> pb = build.getPreviousBuild();
            if (pb != null && pb.getResult() == Result.SUCCESS) {
                mail.removeHeader("In-Reply-To");
                mail.removeHeader("References");
            }
            Address[] allRecipients = mail.getAllRecipients();
            if (allRecipients != null) {
                StringBuilder buf = new StringBuilder("Sending e-mails to:");
                for (Address a : allRecipients) buf.append(' ').append(a);
                listener.getLogger().println(buf);
                Mailer.descriptor().send((HudsonMimeMessage) mail);
                build.addAction(new MailMessageIdAction(mail.getMessageID()));
            } else {
                listener.getLogger().println(Messages.MailSender_ListEmpty());
            }
        }
    } catch (MessagingException e) {
        e.printStackTrace(listener.error(e.getMessage()));
    } finally {
        CHECKPOINT.report();
    }
    return true;
}
Also used : Address(javax.mail.Address) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException)

Example 8 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 9 with Address

use of javax.mail.Address in project jforum2 by rafaelsteil.

the class Spammer method dispatchMessages.

public boolean dispatchMessages() {
    try {
        int sendDelay = SystemGlobals.getIntValue(ConfigKeys.MAIL_SMTP_DELAY);
        if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_AUTH)) {
            if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
                boolean ssl = SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_SSL);
                Transport transport = this.session.getTransport(ssl ? "smtps" : "smtp");
                try {
                    String host = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
                    transport.connect(host, username, password);
                    if (transport.isConnected()) {
                        for (Iterator userIter = this.users.iterator(); userIter.hasNext(); ) {
                            User user = (User) userIter.next();
                            if (this.needCustomization) {
                                this.defineUserMessage(user);
                            }
                            Address address = new InternetAddress(user.getEmail());
                            logger.debug("Sending mail to: " + user.getEmail());
                            this.message.setRecipient(Message.RecipientType.TO, address);
                            transport.sendMessage(this.message, new Address[] { address });
                            if (sendDelay > 0) {
                                try {
                                    Thread.sleep(sendDelay);
                                } catch (InterruptedException ie) {
                                    logger.error("Error while Thread.sleep." + ie, ie);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new MailException(e);
                } finally {
                    try {
                        transport.close();
                    } catch (Exception e) {
                    }
                }
            }
        } else {
            for (Iterator iter = this.users.iterator(); iter.hasNext(); ) {
                User user = (User) iter.next();
                if (this.needCustomization) {
                    this.defineUserMessage(user);
                }
                Address address = new InternetAddress(user.getEmail());
                logger.debug("Sending mail to: " + user.getEmail());
                this.message.setRecipient(Message.RecipientType.TO, address);
                Transport.send(this.message, new Address[] { address });
                if (sendDelay > 0) {
                    try {
                        Thread.sleep(sendDelay);
                    } catch (InterruptedException ie) {
                        logger.error("Error while Thread.sleep." + ie, ie);
                    }
                }
            }
        }
    } catch (MessagingException e) {
        logger.error("Error while dispatching the message." + e, e);
    }
    return true;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) User(net.jforum.entities.User) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Iterator(java.util.Iterator) MailException(net.jforum.exceptions.MailException) Transport(javax.mail.Transport) MessagingException(javax.mail.MessagingException) MailException(net.jforum.exceptions.MailException)

Example 10 with Address

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

the class MailSender method removeDumplicate.

/**
	 * 过滤邮件中的收件人,抄送人,密送人中重复的邮箱地址,而只保留一条
	 * @throws AddressException
	 * @throws EmailException ;
	 */
@SuppressWarnings("unchecked")
public void removeDumplicate() throws AddressException, EmailException {
    Address from = email.getFromAddress();
    List<Address> toList = MailUtils.removeDuplicate(from, email.getToAddresses());
    List<Address> ccList = MailUtils.removeDuplicate(from, email.getCcAddresses());
    List<Address> bccList = MailUtils.removeDuplicate(from, email.getBccAddresses());
    ccList = MailUtils.removeDuplicate(toList, ccList);
    bccList = MailUtils.removeDuplicate(toList, bccList);
    bccList = MailUtils.removeDuplicate(ccList, bccList);
    if (toList != null && toList.size() > 0) {
        email.setTo(toList);
    }
    if (ccList != null && ccList.size() > 0) {
        email.setCc(ccList);
    }
    if (bccList != null && bccList.size() > 0) {
        email.setBcc(bccList);
    }
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress)

Aggregations

Address (javax.mail.Address)86 InternetAddress (javax.mail.internet.InternetAddress)66 MessagingException (javax.mail.MessagingException)34 MimeMessage (javax.mail.internet.MimeMessage)32 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)21 ArrayList (java.util.ArrayList)19 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 ItemId (com.zimbra.cs.service.util.ItemId)8 Header (javax.mail.Header)8 NHINDAddress (org.nhindirect.stagent.NHINDAddress)8 Message (javax.mail.Message)7 MimeMultipart (javax.mail.internet.MimeMultipart)7 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)6 Invite (com.zimbra.cs.mailbox.calendar.Invite)6 Properties (java.util.Properties)6