tags:

views:

337

answers:

3

Hi,

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.

enter code here
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 , where I am doing wrong here ?

A: 

It could be an issue with your ISP blocking port 25 traffic (not unusual!)

From http://help.yahoo.com/l/us/yahoo/smallbusiness/bizmail/pop/pop-32.html:

In an attempt to control unsolicited email (spam), some Internet service providers now block port 25, which means you could experience technical problems when sending email. If you are having trouble sending email, you may need to use port 465 (recommended) or 587 when sending email via Yahoo!'s SMTP server.

Jeremy Smyth
+1  A: 

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

Ratnesh Maurya
Thanks....I got to the bottom of this !
rantravee
please add the solution of your problem to your description - this allows others in your situation to see the answer directly.
Thorbjørn Ravn Andersen
The link in the above post took me to an example that worked !
rantravee
A: 

i using port no 587 for yahoos smtp server but i got the error

trasport........com.sun.mail.smtp.SMTPSendFailedException any solution for this error ......

karthiga