use of com.sun.mail.smtp.SMTPTransport in project adempiere by adempiere.
the class EMail method getTransport.
/**
* Get Transport
* @param session
* @return
* @throws MessagingException
*/
public Transport getTransport(Session session) throws MessagingException {
Transport transport = null;
if (getAuthMechanism().equals(MEMailConfig.AUTHMECHANISM_OAuth)) {
transport = new SMTPTransport(session, null);
transport.connect(getSmtpHost(), getPort(), m_user, "");
} else {
transport = session.getTransport("smtp");
transport.connect();
}
// Log
log.fine("transport=" + transport);
log.fine("transport connected");
// Default Return
return transport;
}
use of com.sun.mail.smtp.SMTPTransport in project opentest by mcdcorp.
the class SendEmailSmtp method run.
@Override
public void run() {
String server = this.readStringArgument("server");
String subject = this.readStringArgument("subject");
String body = this.readStringArgument("body", null);
String bodyHtml = this.readStringArgument("bodyHtml", null);
String userName = this.readStringArgument("userName");
String password = this.readStringArgument("password");
String to = this.readStringArgument("to");
Integer port = this.readIntArgument("port", 25);
Boolean useTls = this.readBooleanArgument("useTls", true);
String cc = this.readStringArgument("cc", null);
String from = this.readStringArgument("from", "sendemail@getopentest.com");
try {
Properties prop = System.getProperties();
prop.put("mail.smtp.host", server);
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.port", port.toString());
Session session = Session.getInstance(prop, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
if (cc != null) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
}
msg.setSubject(subject);
if (bodyHtml != null) {
msg.setContent(bodyHtml, "text/html");
} else if (body != null) {
msg.setText(body);
} else {
throw new RuntimeException("Can't send an email without a body. You must provide it " + "using the \"body\" or \"bodyHtml\" argument.");
}
msg.setSentDate(new Date());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.setStartTLS(useTls);
transport.connect(server, userName, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (Exception exc) {
throw new RuntimeException("Failed to send email", exc);
}
}
use of com.sun.mail.smtp.SMTPTransport in project greenmail by greenmail-mail-test.
the class SMTPCommandTest method mailSenderEmpty.
@Test
public void mailSenderEmpty() throws IOException, MessagingException {
Session smtpSession = greenMail.getSmtp().createSession();
try (SMTPTransport smtpTransport = new SMTPTransport(smtpSession, smtpURL)) {
// Closed by transport
Socket smtpSocket = new Socket(hostAddress, port);
smtpTransport.connect(smtpSocket);
assertThat(smtpTransport.isConnected()).isTrue();
smtpTransport.issueCommand("MAIL FROM: <>", -1);
assertThat("250 OK").isEqualToNormalizingWhitespace(smtpTransport.getLastServerResponse());
}
}
use of com.sun.mail.smtp.SMTPTransport in project greenmail by greenmail-mail-test.
the class SMTPCommandTest method authLogin.
@Test
public void authLogin() throws IOException, MessagingException, UserException {
Session smtpSession = greenMail.getSmtp().createSession();
try (SMTPTransport smtpTransport = new SMTPTransport(smtpSession, smtpURL)) {
// Closed by transport
Socket smtpSocket = new Socket(hostAddress, port);
smtpTransport.connect(smtpSocket);
assertThat(smtpTransport.isConnected()).isTrue();
// Should fail, as user does not exist
smtpTransport.issueCommand("AUTH LOGIN ", 334);
assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 VXNlcm5hbWU6");
smtpTransport.issueCommand(Base64.getEncoder().encodeToString("test".getBytes(StandardCharsets.US_ASCII)), -1);
assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 UGFzc3dvcmQ6");
smtpTransport.issueCommand(Base64.getEncoder().encodeToString("testpass".getBytes(StandardCharsets.US_ASCII)), -1);
assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace(AuthCommand.AUTH_CREDENTIALS_INVALID);
// Try again but create user
greenMail.getUserManager().createUser("test@localhost", "test", "testpass");
smtpTransport.issueCommand("AUTH LOGIN ", 334);
assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 VXNlcm5hbWU6");
smtpTransport.issueCommand(Base64.getEncoder().encodeToString("test".getBytes(StandardCharsets.US_ASCII)), -1);
assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 UGFzc3dvcmQ6");
smtpTransport.issueCommand(Base64.getEncoder().encodeToString("testpass".getBytes(StandardCharsets.US_ASCII)), -1);
assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace(AuthCommand.AUTH_SUCCEDED);
}
}
use of com.sun.mail.smtp.SMTPTransport in project apollo by apolloconfig.
the class DefaultEmailService method send.
@Override
public void send(Email email) {
if (!portalConfig.isEmailEnabled()) {
return;
}
SMTPTransport t = null;
try {
Properties prop = System.getProperties();
Session session = Session.getInstance(prop, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(email.getSenderEmailAddress()));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getRecipientsString(), false));
msg.setSubject(email.getSubject());
msg.setDataHandler(new DataHandler(new HTMLDataSource(email.getBody())));
String host = portalConfig.emailConfigHost();
String user = portalConfig.emailConfigUser();
String password = portalConfig.emailConfigPassword();
t = (SMTPTransport) session.getTransport("smtp");
t.connect(host, user, password);
msg.saveChanges();
t.sendMessage(msg, msg.getAllRecipients());
logger.debug("email response: {}", t.getLastServerResponse());
} catch (Exception e) {
logger.error("send email failed.", e);
Tracer.logError("send email failed.", e);
} finally {
if (t != null) {
try {
t.close();
} catch (Exception e) {
// nothing
}
}
}
}
Aggregations