JavaMail 1.5.4无法发送邮件?

时间:2015-06-25 23:05:43

标签: processing javamail

大家好,我一直试图让这个工作,但它没有,我已经尝试检查所有库是否是最新的,我更新它们,我确保我没有错别字,我错过了什么?我知道它在哪里说电子邮件我有" MYEMAIL"和密码类似。这是我的代码:

// Daniel Shiffman               
// http://www.shiffman.net       

// Simple E-mail Checking        
// This code requires the Java mail library
// smtp.jar, pop3.jar, mailapi.jar, imap.jar, activation.jar
// Download:// http://java.sun.com/products/javamail/

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

void setup() {
  size(200,200);

  // Function to check mail


  // Function to send mail
  sendMail();

  noLoop();
}

public class Auth extends Authenticator {

  public Auth() {
    super();
  }

  public PasswordAuthentication getPasswordAuthentication() {
    String username, password;
    username = "MYEMAIL";
    password = "MYPASSWORD";
    System.out.println("authenticating. . ");
    return new PasswordAuthentication(username, password);
  }
}


// A function to send mail
void sendMail() {
  // Create a session
  String host="smtp.gmail.com";
  Properties props=new Properties();

  // SMTP Session
  props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", "25");
  props.put("mail.smtp.auth", "true");
  // We need TTLS, which gmail requires
  props.put("mail.smtp.starttls.enable","true");

  // Create a session
  Session session = Session.getDefaultInstance(props, new Auth());

  try
  {



    // Make a new message
    MimeMessage message = new MimeMessage(session);

    // Who is this message from
    message.setFrom(new InternetAddress("MYEMAIL", "Name"));

    // Who is this message to (we could do fancier things like make a list or add CC's)
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("MYEMAIL", false));

    // Subject and body
    message.setSubject("Hello World!");
    message.setText("It's great to be here. . .");

    // We can do more here, set the date, the headers, etc.
    Transport.send(message);
    println("Mail sent!");
  }
  catch(Exception e)
  {
    e.printStackTrace();
  }

}

当我尝试运行代码时,我收到一条错误消息,指出我的连接已超时:

引起:java.net.ConnectException:连接超时:java.net.DualStackPlainSocket上的连接超时java.net.DualStackPlainSocketImpl.socketConnect(未知来源)中的连接java.AbstractPlainSocketImpl.doConnect(未知来源) java.net.SocksSocketImpl.connect(未知来源)java.net.PlainSocketImpl.connect(未知来源)java.net.AbstractPlainSocketImpl.connect(未知来源)java.net.AbstractPlainSocketImpl.connectToAddress(未知来源)在com.sun.mail上com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312)的java.net.Socket.connect(未知来源)的java.net.Socket.connect(未知来源)。 com.Sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019)上的util.SocketFetcher.getSocket(SocketFetcher.java:236)... 12更多

顺便说一句,这将在处理论坛上交叉发布,当我在那里提问时,我会添加链接。

0 个答案:

没有答案