use of javax.mail.Authenticator 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 javax.mail.Authenticator in project tomee by apache.
the class MailSessionFactory method create.
public Session create() {
final String password = properties.getProperty("password");
Authenticator auth = null;
if (password != null) {
final String protocol = properties.getProperty("mail.transport.protocol", "smtp");
String user = properties.getProperty("mail." + protocol + ".user");
if (user == null) {
user = properties.getProperty("mail.user");
}
if (user != null) {
final PasswordAuthentication pa = new PasswordAuthentication(user, password);
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
}
if (useDefault) {
if (auth != null) {
return Session.getDefaultInstance(properties, auth);
}
return Session.getDefaultInstance(properties);
}
if (auth != null) {
return Session.getInstance(properties, auth);
}
return Session.getInstance(properties);
}
Aggregations