Search in sources :

Example 1 with MailSenderException

use of org.codehaus.plexus.mailsender.MailSenderException in project maven-plugins by apache.

the class ProjectJavamailMailSender method send.

// ----------------------------------------------------------------------
// MailSender Implementation
// ----------------------------------------------------------------------
public void send(MailMessage mail) throws MailSenderException {
    verify(mail);
    try {
        Authenticator auth = null;
        if (getUsername() != null) {
            auth = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(getUsername(), getPassword());
                }
            };
        }
        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(getLogger().isDebugEnabled());
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(mail.getFrom().getRfc2822Address());
        msg.setFrom(addressFrom);
        if (mail.getToAddresses().size() > 0) {
            InternetAddress[] addressTo = new InternetAddress[mail.getToAddresses().size()];
            int count = 0;
            for (Object o : mail.getToAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressTo[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
        }
        if (mail.getCcAddresses().size() > 0) {
            InternetAddress[] addressCc = new InternetAddress[mail.getCcAddresses().size()];
            int count = 0;
            for (Object o : mail.getCcAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressCc[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.CC, addressCc);
        }
        if (mail.getBccAddresses().size() > 0) {
            InternetAddress[] addressBcc = new InternetAddress[mail.getBccAddresses().size()];
            int count = 0;
            for (Object o : mail.getBccAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressBcc[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.BCC, addressBcc);
        }
        // Setting the Subject and Content Type
        msg.setSubject(mail.getSubject());
        msg.setContent(mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType());
        if (mail.getSendDate() != null) {
            msg.setHeader("Date", DateFormatUtils.getDateHeader(mail.getSendDate()));
        } else {
            msg.setHeader("Date", DateFormatUtils.getDateHeader(new Date()));
        }
        // Send the message
        Transport.send(msg);
    } catch (MessagingException e) {
        throw new MailSenderException("Error while sending mail.", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MailSenderException(org.codehaus.plexus.mailsender.MailSenderException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MailMessage(org.codehaus.plexus.mailsender.MailMessage) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Example 2 with MailSenderException

use of org.codehaus.plexus.mailsender.MailSenderException in project maven-plugins by apache.

the class AnnouncementMailMojo method sendMessage.

/**
     * Send the email.
     *
     * @throws MojoExecutionException if the mail could not be sent
     */
protected void sendMessage() throws MojoExecutionException {
    File file = new File(announcementDirectory, announcementFile);
    String email = "";
    final MailSender ms = getActualMailSender();
    final String fromName = ms.getName();
    final String fromAddress = ms.getEmail();
    if (fromAddress == null || fromAddress.equals("")) {
        throw new MojoExecutionException("Invalid mail sender: name and email is mandatory (" + ms + ").");
    }
    getLog().info("Using this sender for email announcement: " + fromAddress + " < " + fromName + " > ");
    try {
        MailMessage mailMsg = new MailMessage();
        mailMsg.setSubject(getSubject());
        mailMsg.setContent(readAnnouncement(file));
        mailMsg.setContentType(this.mailContentType);
        mailMsg.setFrom(fromAddress, fromName);
        for (Object o1 : getToAddresses()) {
            email = o1.toString();
            getLog().info("Sending mail to " + email + "...");
            mailMsg.addTo(email, "");
        }
        if (getCcAddresses() != null) {
            for (Object o : getCcAddresses()) {
                email = o.toString();
                getLog().info("Sending cc mail to " + email + "...");
                mailMsg.addCc(email, "");
            }
        }
        if (getBccAddresses() != null) {
            for (Object o : getBccAddresses()) {
                email = o.toString();
                getLog().info("Sending bcc mail to " + email + "...");
                mailMsg.addBcc(email, "");
            }
        }
        mailer.send(mailMsg);
        getLog().info("Sent...");
    } catch (MailSenderException e) {
        throw new MojoExecutionException("Failed to send email < " + email + " >", e);
    }
}
Also used : MailMessage(org.codehaus.plexus.mailsender.MailMessage) MailSenderException(org.codehaus.plexus.mailsender.MailSenderException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ProjectJavamailMailSender(org.apache.maven.plugins.announcement.mailsender.ProjectJavamailMailSender) File(java.io.File)

Aggregations

MailMessage (org.codehaus.plexus.mailsender.MailMessage)2 MailSenderException (org.codehaus.plexus.mailsender.MailSenderException)2 File (java.io.File)1 Date (java.util.Date)1 Authenticator (javax.mail.Authenticator)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 PasswordAuthentication (javax.mail.PasswordAuthentication)1 Session (javax.mail.Session)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 ProjectJavamailMailSender (org.apache.maven.plugins.announcement.mailsender.ProjectJavamailMailSender)1