use of javax.mail.PasswordAuthentication in project springboot_op by SnailFastGo.
the class MailSender method send.
public void send() throws Exception {
if (this.mail.getContentType() == null) {
this.mail.setContentType(MailContentTypeEnum.HTML.getValue());
}
if (null == this.mail.getContent() || "".equals(this.mail.getContent())) {
throw new Exception("邮件内容没有设置");
}
if (null == this.mail.getTitle() || "".equals(this.mail.getTitle())) {
throw new Exception("邮件标题没有设置");
}
if (null == this.mail.getSendAddress() || this.mail.getSendAddress().isEmpty()) {
throw new Exception("邮件发送地址没有设置");
}
final PropertiesUtil util = new PropertiesUtil("mail");
//设置邮件服务器登陆信息
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", util.getValue("mail.smtp.service"));
props.put("mail.smtp.port", util.getValue("mail.smtp.port"));
props.put("mail.user", util.getValue("mail.from.address"));
props.put("mail.password", util.getValue("mail.from.smtp.pwd"));
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人昵称
String nickName = MimeUtility.encodeText(util.getValue("mail.from.nickname"));
InternetAddress form = new InternetAddress(nickName + " <" + props.getProperty("mail.user") + ">");
message.setFrom(form);
// 设置邮件标题
message.setSubject(mail.getTitle());
//html发送邮件
if (mail.getContentType().equals(MailContentTypeEnum.HTML.getValue())) {
// 设置邮件内容
message.setContent(mail.getContent(), mail.getContentType());
} else //文本发送邮件
if (mail.getContentType().equals(MailContentTypeEnum.TEXT.getValue())) {
// 设置邮件内容
message.setText(mail.getContent());
}
//发送邮箱地址
List<String> targets = mail.getSendAddress();
for (int i = 0; i < targets.size(); i++) {
try {
// 设置收件人的邮箱
InternetAddress to = new InternetAddress(targets.get(i));
message.setRecipient(Message.RecipientType.TO, to);
// 最后当然就是发送邮件啦
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of javax.mail.PasswordAuthentication in project ddf by codice.
the class SmtpClientImplITCase method validateUsernamePassword.
private void validateUsernamePassword(String username, String password) throws UnknownHostException {
SmtpClientImpl emailService = new SmtpClientImpl();
emailService.setHostName("host.com");
emailService.setPortNumber(25);
emailService.setUserName(username);
emailService.setPassword(password);
Session session = emailService.createSession();
PasswordAuthentication actual = session.requestPasswordAuthentication(InetAddress.getByName("127.0.0.1"), 25, "smtp", "prompt", "defaultUserName");
assertThat(actual.getUserName(), is(username));
assertThat(actual.getPassword(), is(password));
}
use of javax.mail.PasswordAuthentication in project local-data-aragopedia by aragonopendata.
the class SendMailSSL method enviar.
public void enviar(final String usuario, final String password, String destinos, String asunto, String texto) {
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(usuario, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(usuario));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinos));
message.setSubject(asunto);
message.setText(texto);
Transport.send(message);
} catch (MessagingException e) {
log.error("Error sending mail:");
log.error("\t" + e.getMessage());
}
}
use of javax.mail.PasswordAuthentication in project oxCore by GluuFederation.
the class MailService method sendMail.
public boolean sendMail(SmtpConfiguration mailSmtpConfiguration, String from, String fromDisplayName, String to, String toDisplayName, String subject, String message, String htmlMessage) {
if (mailSmtpConfiguration == null) {
log.error("Failed to send message from '{}' to '{}' because the SMTP configuration isn't valid!", from, to);
return false;
}
log.debug("Host name: " + mailSmtpConfiguration.getHost() + ", port: " + mailSmtpConfiguration.getPort() + ", connection time out: " + this.connectionTimeout);
String mailFrom = from;
if (StringHelper.isEmpty(mailFrom)) {
mailFrom = mailSmtpConfiguration.getFromEmailAddress();
}
String mailFromName = fromDisplayName;
if (StringHelper.isEmpty(mailFromName)) {
mailFromName = mailSmtpConfiguration.getFromName();
}
Properties props = new Properties();
props.put("mail.smtp.host", mailSmtpConfiguration.getHost());
props.put("mail.smtp.port", mailSmtpConfiguration.getPort());
props.put("mail.from", mailFrom);
props.put("mail.smtp.connectiontimeout", this.connectionTimeout);
props.put("mail.smtp.timeout", this.connectionTimeout);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.ssl.trust", mailSmtpConfiguration.getHost());
if (mailSmtpConfiguration.isRequiresSsl()) {
props.put("mail.smtp.socketFactory.port", mailSmtpConfiguration.getPort());
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
Session session = null;
if (mailSmtpConfiguration.isRequiresAuthentication()) {
props.put("mail.smtp.auth", "true");
final String userName = mailSmtpConfiguration.getUserName();
final String password = mailSmtpConfiguration.getPasswordDecrypted();
session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
} else {
Session.getInstance(props, null);
}
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(mailFrom, mailFromName));
if (StringHelper.isEmpty(toDisplayName)) {
msg.setRecipients(Message.RecipientType.TO, to);
} else {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, toDisplayName));
}
msg.setSubject(subject, "UTF-8");
msg.setSentDate(new Date());
if (StringHelper.isEmpty(htmlMessage)) {
msg.setText(message + "\n", "UTF-8", "plain");
} else {
// Unformatted text version
final MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(message, "UTF-8", "plain");
// HTML version
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(htmlMessage, "UTF-8", "html");
// Create the Multipart. Add BodyParts to it.
final Multipart mp = new MimeMultipart("alternative");
mp.addBodyPart(textPart);
mp.addBodyPart(htmlPart);
// Set Multipart as the message's content
msg.setContent(mp);
}
Transport.send(msg);
} catch (Exception ex) {
log.error("Failed to send message", ex);
return false;
}
return true;
}
use of javax.mail.PasswordAuthentication in project IPK-BrAPI-Validator by plantbreeding.
the class EmailSender method sendEmail.
static void sendEmail(final String message, final String subject, final String emailAddress) {
javax.mail.Session session;
InternetAddress addressFrom = null;
final Properties properties = new Properties();
properties.put("mail.smtp.host", Config.get("mailSmtpHost"));
if (Config.get("mailSmtpPort") != null) {
properties.put("mail.smtp.port", Config.get("mailSmtpPort"));
}
if (Config.get("mailSmtpLogin") == null || Config.get("mailSmtpLogin").isEmpty()) {
session = javax.mail.Session.getDefaultInstance(properties);
try {
addressFrom = new InternetAddress(Config.get("fromEmailAddress"), Config.get("fromPersonalName"));
} catch (final UnsupportedEncodingException e) {
e.printStackTrace();
LOGGER.warn(emailAddress + " : " + e.getMessage());
}
} else {
properties.put("mail.smtp.auth", "true");
final Authenticator authenticator = new Authenticator() {
private PasswordAuthentication authentication;
{
this.authentication = new PasswordAuthentication(Config.get("mailSmtpLogin"), Config.get("mailSmtpPassword"));
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return this.authentication;
}
};
session = javax.mail.Session.getInstance(properties, authenticator);
try {
addressFrom = new InternetAddress(Config.get("fromEmailAddress"), Config.get("fromPersonalName"));
} catch (final UnsupportedEncodingException e) {
e.printStackTrace();
LOGGER.warn(emailAddress + " : " + e.getMessage());
}
}
try {
final Message mail = new MimeMessage(session);
final InternetAddress addressTo = new InternetAddress(emailAddress);
mail.setRecipient(Message.RecipientType.TO, addressTo);
mail.setSubject(subject);
mail.setFrom(addressFrom);
Multipart multipart = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
content.setText(message, "UTF-8", "html");
multipart.addBodyPart(content);
mail.setContent(multipart);
Transport.send(mail);
// System.out.println(message);
} catch (final MessagingException e) {
e.printStackTrace();
LOGGER.warn(emailAddress + " : " + e.getMessage());
}
}
Aggregations