Sending mail with Javamail over ssl...

C

carsten801

Hi,

are there any tutorials for sending a mail with Javamail (1.4 Release)?

I've tried it on my own with SMTPSSLTranport with no success... It's
driving me nuts...


Thanks in andvance and best regards.

Carsten Marx
 
T

Thomas Weidenfeller

carsten801 said:
are there any tutorials for sending a mail with Javamail (1.4 Release)?

Check your JavaMail installation directory. The docs subdirectory should
contain the design spec (in PostScript and pdf), which explains how the
components interact. The demo subdirectory contains 10+ example
applications.

/Thomas
 
C

carsten801

Thomas said:
Check your JavaMail installation directory. The docs subdirectory should
contain the design spec (in PostScript and pdf), which explains how the
components interact. The demo subdirectory contains 10+ example
applications.

/Thomas

I've checked the installation directory. So i've written a method like
this:


<code>
private void sendMail() {
MainFrame.get().setCursor(Preferences.WAIT_CURSOR);
String adr = address.getText();
try {
// create properties
Properties props = System.getProperties();
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.port", "25");
props.put("mail.smtps.host", Preferences.MAIL_HOST);
// create session
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
// create content
MimeMultipart content = new MimeMultipart("alternative");
// Text Mail
MimeBodyPart plain = new MimeBodyPart();
plain.setContent(plainText, "text/plain");
plain.setHeader("MIME-Version", "1.0");
plain.setHeader("Content-Type", "text/plain;
charset=\"iso-8859-1\"");
content.addBodyPart(plain);
// HTML Mail
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlText, "text/html");
html.setHeader("MIME-Version", "1.0");
html.setHeader("Content-Type", "text/html;
charset=\"iso-8859-1\"");
content.addBodyPart(html);
// create message
MimeMessage msg = new MimeMessage(session);
msg.setContent(content);
msg.setHeader("MIME-Version", "1.0");
msg.setHeader("Content-Type", content.getContentType());
msg.setHeader("X-Mailer", "Java-Mailer");
msg.setSentDate(new Date());
// set sender and recipient
msg.setFrom(new InternetAddress(Preferences.MAIL_FROM,
Preferences.MAIL_SENDER));
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(adr));
msg.setSubject(Texts.MAIL_SUBJECT);
// save changes
msg.saveChanges();
// transport the message
SMTPSSLTransport transport =
(SMTPSSLTransport)session.getTransport("smtps");
// get password
String pwNew = DataManager.code(Preferences.MAIL_PASSWORD);
// connect to server
// send the message
transport.connect(Preferences.MAIL_HOST, Preferences.MAIL_LOGIN,
pwNew);
transport.sendMessage(msg, msg.getAllRecipients());
// close the connection
transport.close();
} catch (Exception e) {
e.getStackTrace();
ErrorManager.mailError(MainFrame.get());
return;
} finally {
MainFrame.get().setCursor(Preferences.DEFAULT_CURSOR);
dispose();
}
ErrorManager.mailSent(MainFrame.get());
}
</code>


But this isn't working (Yes the port 25 is ok for ssl connection, with
Mail.app this account is working).

Here the debug stack trace:
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun
Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "xxx-my.mail.host-xxx", port 25,
isSSL true
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException:
Unrecognized SSL message, plaintext connection?


Bug? Or am i blind?

Best regards

Carsten
 
G

Greg R. Broderick

Here the debug stack trace:
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTranspor
t,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "xxx-my.mail.host-xxx", port
25, isSSL true
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException:
Unrecognized SSL message, plaintext connection?

Try using the SMTPS port (465) instead of the SMTP port (25).

Cheers
GRB
 

dfx

Joined
Feb 3, 2009
Messages
1
Reaction score
0
mail over SSL

<my properties>
props.setProperty("mail.transport.protocol", "smtps");
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.port", "465");
props.put("mail.smtps.host", this.strMailHost);

<Strack Trace Error>

DEBUG: setDebug: JavaMail version 1.4.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "exch.pdea.gov.ph", port 465, isSSL true
Got Exception: javax.mail.MessagingException: Could not connect to SMTP host: exch.pdea.gov.ph, port: 465;
nested exception is:
java.net.SocketException: Network is unreachable: connect
Got Exception: javax.mail.MessagingException: Could not connect to SMTP host: exch.pdea.gov.ph, port: 465;
nested exception is:
java.net.SocketException: Network is unreachable: connect
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top