use of javax.mail.internet.AddressException in project ORCID-Source by ORCID.
the class BaseController method validateEmailAddress.
/**
* Validates if the provided string matches an email address pattern.
*
* @param email
* The string to evaluate
* @return true if the provided string matches an email address pattern,
* false otherwise.
*/
protected boolean validateEmailAddress(String email) {
if (StringUtils.isNotBlank(email)) {
try {
InternetAddress addr = new InternetAddress(email);
addr.validate();
return true;
} catch (AddressException ex) {
}
}
return false;
}
use of javax.mail.internet.AddressException in project ORCID-Source by ORCID.
the class CustomEmailController method validateSender.
@RequestMapping(value = "/validate-sender.json", method = RequestMethod.POST)
@ResponseBody
public CustomEmailForm validateSender(@RequestBody CustomEmailForm customEmailForm) {
customEmailForm.getSender().setErrors(new ArrayList<String>());
if (!PojoUtil.isEmpty(customEmailForm.getSender())) {
try {
String sender = customEmailForm.getSender().getValue();
InternetAddress addr = new InternetAddress(sender);
addr.validate();
} catch (AddressException ex) {
customEmailForm.getSender().getErrors().add(getMessage("custom_email.sender.invalid"));
}
}
return customEmailForm;
}
use of javax.mail.internet.AddressException in project opennms by OpenNMS.
the class JavaMailer method buildMessage.
/**
* Build a complete message ready for sending.
*
* @return completed message, ready to be passed to Transport.sendMessage
* @throws org.opennms.javamail.JavaMailerException if any of the underlying operations fail
*/
public Message buildMessage() throws JavaMailerException {
try {
checkEnvelopeAndContents();
MimeMessage message = initializeMessage();
// The next line has been commented, because it prevents the usage of internationalized characters and makes the email unreadable.
// String encodedText = MimeUtility.encodeText(getMessageText(), m_charSet, m_encoding);
String encodedText = getMessageText();
if ((getFileName() == null) && (getInputStream() == null)) {
message.setContent(encodedText, m_contentType + "; charset=" + m_charSet);
} else if (getFileName() == null) {
BodyPart streamBodyPart = new MimeBodyPart();
streamBodyPart.setDataHandler(new DataHandler(new InputStreamDataSource(m_inputStreamName, m_inputStreamContentType, m_inputStream)));
streamBodyPart.setFileName(m_inputStreamName);
streamBodyPart.setHeader("Content-Transfer-Encoding", "base64");
streamBodyPart.setDisposition(Part.ATTACHMENT);
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(streamBodyPart);
message.setContent(mp);
} else {
BodyPart bp = new MimeBodyPart();
bp.setContent(encodedText, m_contentType + "; charset=" + m_charSet);
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(bp);
mp.addBodyPart(createFileAttachment(new File(getFileName())));
message.setContent(mp);
}
message.setHeader("X-Mailer", getMailer());
message.setSentDate(new Date());
message.saveChanges();
return message;
} catch (AddressException e) {
LOG.error("Java Mailer Addressing exception: ", e);
throw new JavaMailerException("Java Mailer Addressing exception: ", e);
} catch (MessagingException e) {
LOG.error("Java Mailer messaging exception: ", e);
throw new JavaMailerException("Java Mailer messaging exception: ", e);
// } catch (UnsupportedEncodingException e) {
// log().error("Java Mailer messaging exception: ", e);
// throw new JavaMailerException("Java Mailer encoding exception: ", e);
}
}
use of javax.mail.internet.AddressException in project javautils by jiadongpo.
the class Sender163Test method sendNow.
public void sendNow() {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
// 没有开外网,程序就跑不动了,163邮箱
props.put("mail.smtp.host", "smtp.suixingpay.com");
session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO 密码需要填写明文 PasswordAuthentication是一个包装类,里面包含了用户名和密码
return new PasswordAuthentication("dc_mail@suixingpay.com", "datamail123");
}
});
session.setDebug(true);
try {
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("dc_mail@suixingpay.com"));
//
// 收件人
InternetAddress toAddress = new InternetAddress(receiver);
// 加收件人
msg.addRecipient(Message.RecipientType.TO, toAddress);
// 收件人
InternetAddress ccAddress = new InternetAddress(cc);
// 加收件人
msg.addRecipient(Message.RecipientType.CC, ccAddress);
msg.setSubject(subject);
msg.setText(mailContent);
Transport.send(msg);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
while ((e = (MessagingException) e.getNextException()) != null) e.printStackTrace();
}
// senNow end
}
use of javax.mail.internet.AddressException in project javautils by jiadongpo.
the class Sender163 method sendNow.
public void sendNow() {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
// 163服务器地址: POP3服务器: pop.163.com SMTP服务器: smtp.163.com IMAP服务器: imap.163.com
// 没有开外网,程序就跑不动了,163邮箱
props.put("mail.smtp.host", "smtp.163.com");
// 腾讯企业邮 接收服务器: imap.exmail.qq.com(使用SSL,端口号993) 发送服务器: smtp.exmail.qq.com(使用SSL,端口号465)
// props.put("mail.smtp.host", "imap.exmail.qq.com");//没有开外网,程序就跑不动了,腾讯企业邮
// 验证信息需要通过Session传给邮件服务器,其中Authenticator负责密码校验,如果不需要验证身份就用null或用单参数的getInstance()。
session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO 密码需要填写明文 PasswordAuthentication是一个包装类,里面包含了用户名和密码
return new PasswordAuthentication("jiadp2012", "*****");
}
});
// 允许调试,因此可以用getDebug()方法取调试信息,消息很多
session.setDebug(true);
try {
// 创建了自己的Session对象后就可以发送消息了,这时需要用到Message消息类型。由于Message是一个抽象类,所以使用时必须使用一个具体的子类型。在大多数情况下,这个子类是javax.mail.internet.MimeMessage。一个MimeMessage是封装了MIME类型数据和报头的消息。消息的报头严格限制为只能使用US-ASCII字符,尽管非ASCII字符可以被编码到某些报头字段中。
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("jiadp2012@163.com"));
// 一旦创建了会话和消息,并为消息填充了内容,就需要用Address类为信件标上地址。同Message类一样,Address类也是抽象类。可以使用javax.mail.internet.InternetAddress类。
//
// 收件人
InternetAddress toAddress = new InternetAddress(receiver);
// 加收件人
msg.addRecipient(Message.RecipientType.TO, toAddress);
// 收件人
InternetAddress ccAddress = new InternetAddress(cc);
// 加收件人
msg.addRecipient(Message.RecipientType.CC, ccAddress);
msg.setSubject(subject);
msg.setText(mailContent);
// 发送消息的最后一步操作是使用Transport类。该类使用特定协议(通常是SMTP语言来发送消息。他是一个抽象类,其操作与Session类有些相似。可以通过只调用表态的send()方法来使用该类的默认版本。
Transport.send(msg);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
while ((e = (MessagingException) e.getNextException()) != null) e.printStackTrace();
}
// senNow end
}
Aggregations