use of org.codehaus.plexus.mailsender.MailMessage 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);
}
}
use of org.codehaus.plexus.mailsender.MailMessage 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);
}
}
Aggregations