Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can send email through sockets, but not with JavaMail?

843834Jun 20 2009 — edited Jun 20 2009
Hi to all! I've a program that sends email. After a lot of tries, I gave up using JavaMail, because always throws java.net.ConnectException: Connection timed out: connect. So I tried to do it using sockets, writing directly to it, and guess what... it worked! My question is, why with sockets can connect and do everything but not with JavaMail? I'm posting both codes, please tell me what is wrong:

Sockets
StringBuffer retBuff = new StringBuffer();

//BufferedReader msg;
//msg = new BufferedReader(msgFileReader);
smtpPipe = new Socket(mailHost, 25);
smtpPipe.setSoTimeout(120000);
if (smtpPipe == null) {
	return retBuff;
}
inn = smtpPipe.getInputStream();
outt = smtpPipe.getOutputStream();
in = new BufferedReader(new InputStreamReader(inn));
out = new PrintWriter(new OutputStreamWriter(outt), true);
if (inn == null || outt == null) {
	retBuff.append("Failed to open streams to socket.");
	return retBuff;
}
String initialID = in.readLine();
retBuff.append(initialID);
retBuff.append("HELO " + localhost.getHostName());
out.println("HELO " + localhost.getHostName());
String welcome = in.readLine();
retBuff.append(welcome);
retBuff.append("MAIL From:<" + from + ">");
out.println("MAIL From:<" + from + ">");
String senderOK = in.readLine();
retBuff.append(senderOK);
for (int i = 0; i < to.length; i++) {
	retBuff.append("RCPT TO:<" + to[i] + ">");
	out.println("RCPT TO:<" + to[i] + ">");
	String recipientOK = in.readLine();
	retBuff.append(recipientOK);
}

retBuff.append("DATA");
out.println("DATA");

out.println("From: Me <" + from + ">");
out.println("Subject: " + subject);
out.println("Mime-Version: 1.0;");
out.println("Content-Type: text/html; charset=\"ISO-8859-1\";");
//out.println("Content-Type: multipart/mixed; charset=\"ISO-8859-1\";");
//out.println("Content-Transfer-Encoding: 7bit;");

//String line;
//while ((line = msg.readLine()) != null) {
//	out.println(line);
//}
out.println(msg);
retBuff.append(".");
out.println(".");
String acceptedOK = in.readLine();
retBuff.append(acceptedOK);
retBuff.append("QUIT");
out.println("QUIT");
return retBuff;
And now with JavaMail:
StringBuffer retVal = new StringBuffer();
try {
	retVal.append("Sending mail...");
	Properties props = new Properties();
	props.setProperty("mail.transport.protocol", "smtp");
	props.setProperty("mail.host", mailHost.getHostAddress());
	retVal.append(mailHost.getHostAddress());

	Session mailSession = Session.getDefaultInstance(props, null);
	mailSession.setDebug(true);

	Transport transport = mailSession.getTransport();

	MimeMessage message = new MimeMessage(mailSession);
	message.setSubject("HTML  mail with images");
	message.setFrom(new InternetAddress("from@from"));
	message.addRecipient(
		Message.RecipientType.TO,
		new InternetAddress("to@to"));

	//
	// This HTML mail have to 2 part, the BODY and the embedded image
	//
	MimeMultipart multipart = new MimeMultipart("related");

	// first part  (the html)
	BodyPart messageBodyPart = new MimeBodyPart();
	String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
	messageBodyPart.setContent(htmlText, "text/html");

	// add it
	multipart.addBodyPart(messageBodyPart);

	// second part (the image)
	messageBodyPart = new MimeBodyPart();
	File fimg = File.createTempFile("imgmail", ".jpg");
	retVal.append("File size antes: " + fimg.length());
	URL url = new URL("someurl");
	InputStream is = url.openStream();
	byte[] b = new byte[is.available()];
	is.read(b);

	FileOutputStream fos = new FileOutputStream(fimg);
	fos.write(b);
	fos.flush();
	fos.close();
	is.close();

	retVal.append("File size dpues: " + fimg.length());

	DataSource fds = new FileDataSource(fimg);
	messageBodyPart.setDataHandler(new DataHandler(fds));
	messageBodyPart.setHeader("Content-ID", "<image>");

	// add it
	multipart.addBodyPart(messageBodyPart);

	// put everything together
	message.setContent(multipart);

	transport.connect();
	transport.sendMessage(
		message,
		message.getRecipients(Message.RecipientType.TO));
	transport.close();
} catch (MessagingException exc) {
	retVal.append("<br/>" + exc + "<br/>");
	//throw new MessagingException(exc.getMessage());
} catch (IOException exc) {
	retVal.append("<br/>" + exc + "<br/>");
	//throw new IOException(exc.getMessage());
}

return retVal;
Thanks for the help!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 18 2009
Added on Jun 20 2009
1 comment
291 views