How do I use the Yahoo SMTP server to send mail with the JavaMail API?

John Zukowski

From your Yahoo mail account, you need to enable POP Access and Forwarding. Select Options in the left gutter, then POP Access & Forwarding under Mail Management. You need to have Web and POP Access enabled. This is not a free service. You then MUST use your Yahoo address as the FROM address when sending the message. The basic mail sending program with authentication is sufficient.

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class MailExample {
  public static void main (String args[]) throws Exception {
    String smtpHost = "";
    String popHost = "";
    String from = args[0]; // with
    String to = args[1];
    String username = args[2];
    String password = args[3];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("", smtpHost);

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Pop Authenticate yourself
    Store store = session.getStore("pop3");
    store.connect(popHost, username, password);

    // Define message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
      new InternetAddress(to));
    message.setSubject("Hello JavaMail");
    message.setText("Welcome to Yahoo's JavaMail");

    // Send message
Note: Previously, POP authentication was required. That is no longer the case.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact