Hi, I am writing a program where I want the user to be able to e-mail me feedback, so I have downloaded javax.mail and I'm trying to send myself an e-mail to test it out. I have Yahoo Mail Plus. Below is my code, which I tweaked from one of the sample programs from javax.mail. I think the code after line 55 is just error display code.

package javamail;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class mymail 
{
    static String msgText = "This is a message body.\nHere's the second line.";

    public static void main(String[] args) 
    {	
	String to = "bob@yahoo.com";
	String from = "bob@yahoo.com";
	String host = "plus.smtp.mail.yahoo.com";
	boolean debug = true;
        int port = 465;

	// create some properties and get the default Session
	Properties props = new Properties();
	props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", Integer.toString(port));        
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.ssl","true");
        props.put("mail.smtps.auth", "true");        
        
	if (debug) 
            props.put("mail.debug", "true");
        else
            props.put("mail.debug", "false");
                
        Authenticator auth = new Authenticator() 
        {
        };
        
	Session session = Session.getInstance(props, auth);        
	session.setDebug(debug);
	
	try 
        {
	    // create a message
	    Message msg = new MimeMessage(session);
	    msg.setFrom(new InternetAddress(from));
	    InternetAddress address = new InternetAddress(to);
	    msg.setRecipient(Message.RecipientType.TO, address);
	    msg.setSubject("JavaMail APIs Test");
	    msg.setSentDate(new Date());
	    // If the desired charset is known, you can use
	    // setText(text, charset)
	    msg.setText(msgText);
	    
	    Transport.send(msg);
	} 
        catch (MessagingException mex) 
        {
	    System.out.println("\n--Exception handling in msgsendsample.java");

	    mex.printStackTrace();
	    System.out.println();
	    Exception ex = mex;
	    do 
            {
		if (ex instanceof SendFailedException) 
                {
		    SendFailedException sfex = (SendFailedException)ex;
		    Address[] invalid = sfex.getInvalidAddresses();
		    if (invalid != null) 
                    {
			System.out.println("    ** Invalid Addresses");
			if (invalid != null) 
                        {
			    for (int i = 0; i < invalid.length; i++) 
				System.out.println("         " + invalid[i]);
			}
		    }
		    Address[] validUnsent = sfex.getValidUnsentAddresses();
		    if (validUnsent != null) 
                    {
			System.out.println("    ** ValidUnsent Addresses");
			if (validUnsent != null) 
                        {
			    for (int i = 0; i < validUnsent.length; i++) 
				System.out.println("         "+validUnsent[i]);
			}
		    }
		    Address[] validSent = sfex.getValidSentAddresses();
		    if (validSent != null) 
                    {
			System.out.println("    ** ValidSent Addresses");
			if (validSent != null) 
                        {
			    for (int i = 0; i < validSent.length; i++) 
				System.out.println("         "+validSent[i]);
			}
		    }
		}
		System.out.println();
		if (ex instanceof MessagingException)
		    ex = ((MessagingException)ex).getNextException();
		else
		    ex = null;
	    } 
            while (ex != null);
	}
    }
}

When I run this program I get this message:

530 authentication required - for help go to http://help.yahoo.com/help/us/mail/pop/pop-11.html

I thought that line 28 specified that I was connecting using "authorization". I notice that I have not specified a password anywhere. Is that the authentification problem this is referring to? I think I need to put something inside the brackets on line 36 and 37, but I'm not sure what. I also imagine I need to put something else into props also, but what? Thanks.

Most of the example I can find use a small inner class for the Authenticator, such as

private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           String username = SMTP_AUTH_USER;
           String password = SMTP_AUTH_PWD;
           return new PasswordAuthentication(username, password);
        }
    }

The full example is here: http://www.rgagnon.com/javadetails/java-0538.html

Thanks Ezzaral. I've been toying around with this program and the link you gave me, and it appears I have it working now.

package javamail;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class mymail 
{
    String msgText;
    String to;
    String from;
    String host;
    String password;
    boolean debug;
    int port;

    
    public static void main(String[] args) 
    {
        mymail myMail = new mymail();
        myMail.SendAMessage();
    }
    
    
    public mymail()
    {
        msgText = "This is a message body.\nHere's the second line.";
        to = "bob@yahoo.com";
        from = "bob@yahoo.com";
        password = "bobspass";
        host = "plus.smtp.mail.yahoo.com";
        debug = true;
        port = 465;        
    }
    
    
    @SuppressWarnings("static-access")
    public void SendAMessage()
    {
	// create some properties and get the default Session
	Properties props = new Properties();
	props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", Integer.toString(port));        
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.ssl","true");
        props.put("mail.smtp.auth", "true");        
        
	if (debug) 
            props.put("mail.debug", "true");
        else
            props.put("mail.debug", "false");
                
        Authenticator auth = new SMTPAuthenticator();        
	Session session = Session.getInstance(props, auth);        
	session.setDebug(debug);
	
	try 
        {
	    // create a message
	    Message msg = new MimeMessage(session);
	    msg.setFrom(new InternetAddress(from));
	    InternetAddress address = new InternetAddress(to);
	    msg.setRecipient(Message.RecipientType.TO, address);
	    msg.setSubject("JavaMail APIs Test");
	    msg.setSentDate(new Date());
	    // If the desired charset is known, you can use
	    // setText(text, charset)
	    msg.setText(msgText);
            
            Transport.send(msg);
    	} 
        catch (MessagingException mex) 
        {
	    System.out.println("\n--Exception handling in msgsendsample.java");

	    mex.printStackTrace();
	    System.out.println();
	    Exception ex = mex;
	    do 
            {
		if (ex instanceof SendFailedException) 
                {
		    SendFailedException sfex = (SendFailedException)ex;
		    Address[] invalid = sfex.getInvalidAddresses();
		    if (invalid != null) 
                    {
			System.out.println("    ** Invalid Addresses");
			if (invalid != null) 
                        {
			    for (int i = 0; i < invalid.length; i++) 
				System.out.println("         " + invalid[i]);
			}
		    }
		    Address[] validUnsent = sfex.getValidUnsentAddresses();
		    if (validUnsent != null) 
                    {
			System.out.println("    ** ValidUnsent Addresses");
			if (validUnsent != null) 
                        {
			    for (int i = 0; i < validUnsent.length; i++) 
				System.out.println("         "+validUnsent[i]);
			}
		    }
		    Address[] validSent = sfex.getValidSentAddresses();
		    if (validSent != null) 
                    {
			System.out.println("    ** ValidSent Addresses");
			if (validSent != null) 
                        {
			    for (int i = 0; i < validSent.length; i++) 
				System.out.println("         "+validSent[i]);
			}
		    }
		}
		System.out.println();
		if (ex instanceof MessagingException)
		    ex = ((MessagingException)ex).getNextException();
		else
		    ex = null;
	    } 
            while (ex != null);
	}
    }
    
    
    private class SMTPAuthenticator extends javax.mail.Authenticator 
    {
        public PasswordAuthentication getPasswordAuthentication() 
        {
            String uName = from;
            String passwd = password;
            return new PasswordAuthentication(uName, passwd);
        }
    }
}

Changing line 49 from:

props.put("mail.smtps.auth", "true");

to:

props.put("mail.smtp.auth", "true");

seemed to make the difference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.