use of javax.mail.Transport in project zm-mailbox by Zimbra.
the class SmtpTransportTest method dataException.
@Test(timeout = 3000)
public void dataException() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("354 OK").swallowUntil("\r\n.\r\n").build().start(PORT);
Session session = JMSession.getSession();
Transport transport = session.getTransport("smtp");
transport.connect("localhost", PORT, null, null);
String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))) {
@Override
public void writeTo(OutputStream os, String[] ignoreList) throws MessagingException {
// exception while encoding
throw new MessagingException();
}
};
try {
transport.sendMessage(msg, msg.getAllRecipients());
Assert.fail();
} catch (SendFailedException expected) {
} finally {
transport.close();
}
server.shutdown(1000);
Assert.assertEquals("EHLO localhost\r\n", server.replay());
Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
Assert.assertEquals("DATA\r\n", server.replay());
Assert.assertNull(server.replay());
}
use of javax.mail.Transport in project zm-mailbox by Zimbra.
the class SmtpTransportTest method rcptToError.
@Test(timeout = 3000)
public void rcptToError() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("550 error").recvLine().build().start(PORT);
Session session = JMSession.getSession();
session.getProperties().setProperty("mail.smtp.sendpartial", "true");
Transport transport = session.getTransport("smtp");
transport.connect("localhost", PORT, null, null);
String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
try {
transport.sendMessage(msg, msg.getAllRecipients());
Assert.fail();
} catch (SendFailedException e) {
Assert.assertEquals(0, e.getValidSentAddresses().length);
Assert.assertEquals(0, e.getValidUnsentAddresses().length);
Assert.assertEquals(1, e.getInvalidAddresses().length);
} finally {
transport.close();
}
server.shutdown(1000);
Assert.assertEquals("EHLO localhost\r\n", server.replay());
Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
Assert.assertEquals("QUIT\r\n", server.replay());
Assert.assertNull(server.replay());
}
use of javax.mail.Transport in project zm-mailbox by Zimbra.
the class SmtpInject method main.
public static void main(String[] args) {
CliUtil.toolSetup();
CommandLine cl = parseArgs(args);
if (cl.hasOption("h")) {
usage(null);
}
String file = null;
if (!cl.hasOption("f")) {
usage("no file specified");
} else {
file = cl.getOptionValue("f");
}
try {
ByteUtil.getContent(new File(file));
} catch (IOException ioe) {
usage(ioe.getMessage());
}
String host = null;
if (!cl.hasOption("a")) {
usage("no smtp server specified");
} else {
host = cl.getOptionValue("a");
}
String sender = null;
if (!cl.hasOption("s")) {
usage("no sender specified");
} else {
sender = cl.getOptionValue("s");
}
String recipient = null;
if (!cl.hasOption("r")) {
usage("no recipient specified");
} else {
recipient = cl.getOptionValue("r");
}
boolean trace = false;
if (cl.hasOption("T")) {
trace = true;
}
boolean tls = false;
if (cl.hasOption("t")) {
tls = true;
}
boolean auth = false;
String user = null;
String password = null;
if (cl.hasOption("A")) {
auth = true;
if (!cl.hasOption("u")) {
usage("auth enabled, no user specified");
} else {
user = cl.getOptionValue("u");
}
if (!cl.hasOption("p")) {
usage("auth enabled, no password specified");
} else {
password = cl.getOptionValue("p");
}
}
if (cl.hasOption("v")) {
mLog.info("SMTP server: " + host);
mLog.info("Sender: " + sender);
mLog.info("Recipient: " + recipient);
mLog.info("File: " + file);
mLog.info("TLS: " + tls);
mLog.info("Auth: " + auth);
if (auth) {
mLog.info("User: " + user);
char[] dummyPassword = new char[password.length()];
Arrays.fill(dummyPassword, '*');
mLog.info("Password: " + new String(dummyPassword));
}
}
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
if (auth) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
if (tls) {
props.put("mail.smtp.starttls.enable", "true");
} else {
props.put("mail.smtp.starttls.enable", "false");
}
// Disable certificate checking so we can test against
// self-signed certificates
props.put("mail.smtp.ssl.socketFactory", SocketFactories.dummySSLSocketFactory());
Session session = Session.getInstance(props, null);
session.setDebug(trace);
try {
// create a message
MimeMessage msg = new ZMimeMessage(session, new ZSharedFileInputStream(file));
InternetAddress[] address = { new JavaMailInternetAddress(recipient) };
msg.setFrom(new JavaMailInternetAddress(sender));
// attach the file to the message
Transport transport = session.getTransport("smtp");
transport.connect(null, user, password);
transport.sendMessage(msg, address);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
System.exit(1);
}
}
use of javax.mail.Transport in project zm-mailbox by Zimbra.
the class MailSender method relayMessage.
/**
* Send the MimeMessage via external relay MTA configured on the server.
* @param mm
* @throws ServiceException
*/
public static void relayMessage(MimeMessage mm) throws MessagingException, ServiceException {
Session session = JMSession.getRelaySession();
ZimbraLog.smtp.debug("Sending message %s with properties: %s", mm.getMessageID(), session.getProperties());
Transport transport = session.getTransport("smtp");
try {
transport.connect();
transport.sendMessage(mm, mm.getAllRecipients());
} finally {
transport.close();
}
}
use of javax.mail.Transport 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;
}
Aggregations