use of org.apache.commons.net.smtp.SMTPClient in project nhin-d by DirectProject.
the class DSNMailSender_sendMailTest method testSendMail_mailSent_noExceptions.
@Test
public void testSendMail_mailSent_noExceptions() throws Exception {
MimeMessage msg = TestUtils.readMimeMessageFromFile("MessageWithAttachment.txt");
final SMTPClient client = mock(SMTPClient.class);
when(client.setSender((String) any())).thenReturn(true);
when(client.addRecipient((String) any())).thenReturn(true);
when(client.sendMessageData()).thenReturn(new StringWriter());
when(client.getReplyCode()).thenReturn(250);
final SMTPClientFactory factory = createFactory(client);
DSNMailSender sender = new DSNMailSender("smtp://localhost", factory);
Exchange exchange = new DefaultExchange(mock(CamelContext.class));
exchange.getIn().setBody(msg);
sender.sendMail(exchange);
verify(client, times(1)).getReplyCode();
}
use of org.apache.commons.net.smtp.SMTPClient in project gerrit by GerritCodeReview.
the class SmtpEmailSender method send.
@Override
public void send(final Address from, Collection<Address> rcpt, final Map<String, EmailHeader> callerHeaders, String textBody, @Nullable String htmlBody) throws EmailException {
if (!isEnabled()) {
throw new EmailException("Sending email is disabled");
}
StringBuilder rejected = new StringBuilder();
try {
final SMTPClient client = open();
try {
if (!client.setSender(from.email())) {
throw new EmailException("Server " + smtpHost + " rejected from address " + from.email());
}
/* Do not prevent the email from being sent to "good" users simply
* because some users get rejected. If not, a single rejected
* project watcher could prevent email for most actions on a project
* from being sent to any user! Instead, queue up the errors, and
* throw an exception after sending the email to get the rejected
* error(s) logged.
*/
for (Address addr : rcpt) {
if (!client.addRecipient(addr.email())) {
String error = client.getReplyString();
rejected.append("Server ").append(smtpHost).append(" rejected recipient ").append(addr).append(": ").append(error);
}
}
try (Writer messageDataWriter = client.sendMessageData()) {
if (messageDataWriter == null) {
/* Include rejected recipient error messages here to not lose that
* information. That piece of the puzzle is vital if zero recipients
* are accepted and the server consequently rejects the DATA command.
*/
throw new EmailException(rejected.append("Server ").append(smtpHost).append(" rejected DATA command: ").append(client.getReplyString()).toString());
}
render(messageDataWriter, callerHeaders, textBody, htmlBody);
if (!client.completePendingCommand()) {
throw new EmailException("Server " + smtpHost + " rejected message body: " + client.getReplyString());
}
client.logout();
if (rejected.length() > 0) {
throw new EmailException(rejected.toString());
}
}
} finally {
client.disconnect();
}
} catch (IOException e) {
throw new EmailException("Cannot send outgoing email", e);
}
}
Aggregations