Can not send mail from the Java application

advertisements

In my java application I need to send mails to different mail addresses. I am using the next piece of code , but for some reason it doesn't work.

public class main {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "mail.yahoo.com.");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");  

        Session session = Session.getInstance(props, new MyAuth());  

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("[email protected]"));
            InternetAddress[] address = {new InternetAddress("[email protected]")};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("subject ");
            msg.setSentDate(new Date());  

            msg.setText("Message here ");  

            Transport.send(msg);
        }  catch (MessagingException e) {}
    } 

}  

class MyAuth extends Authenticator {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("my username","my password");
    }
}

I get the folowing text from debuging it:

[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com.au.", port 25, isSSL false

Could anyone inform me , what I am doing wrong here ?


I am not sure, but I faced the same problem when sending mail using a gmail id, you are using yahoo. The problem was gmail uses ssl layer protection, i think same is the case with yahoo so you need to use

mail.smtps.host instead of mail.smtp.host

and same for other properties too.

and isSSL to true.

I can post complete code snippet, once i reach office and use office's machine. For now you can look at http://www.rgagnon.com/javadetails/java-0570.html