0

Javaクラスからメールを送信しようとしていますが、MessangingExceptionが継続的に発生します。認証の問題に関係していると思いますが、問題がわかりません。

以下は、例外のスタックトレースです。

Sep 19, 2012 7:27:30 PM me.uni.sushilkumar.geodine.util.Mailer send
SEVERE: null
javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first.     ko8sm1883776pbc.40

    at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at me.uni.sushilkumar.geodine.util.Mailer.send(Mailer.java:61)
at me.uni.sushilkumar.geodine.util.Mailer.main(Mailer.java:73)

次のコードを使用してメッセージを送信しています

public class Mailer {
private Session session;
private final String username;
private final String password;
public Mailer(String username,String password)
{
    this.username=username;
    this.password=password;
    init(username,password);
}

public final void init(String username,String password)
{
    Properties props=new Properties();
    props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
    session=Session.getInstance(props, new Authenticator()
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(Mailer.this.username,Mailer.this. password);
        }
    });

}

public boolean send(String recipient,String subject,String body) 
{
    boolean status=false;
    try {

        Message message=new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
        message.setSubject(subject);
        message.setText(body);
        Transport.send(message);
        status=true;

    } catch (MessagingException ex) {
        Logger.getLogger(Mailer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return status;
}

public static void main(String[] args)
{
    Mailer mailer=new Mailer("example@example.com","password");
    boolean status=mailer.send("0120sushil@gmail.com", "Demo Mail", "This is a demo message");
}

}

誰かがこのコードの何が問題になっているのか教えてもらえますか?

4

2 に答える 2