我在尝试使用JavaMail发送电子邮件时遇到错误

时间:2013-08-30 19:48:21

标签: java email

我可以使用文件夹和商店成功访问我的电子邮件文件夹但是当我尝试发送电子邮件时,我收到错误。

我用来尝试发送电子邮件的代码:

private static final String host = "smtp.google.com";
private static final String user = "fakeaddress@gmail.com";
private static final String pass = "fakepassword";

private static Properties props = null;
private static Session session = null;

public static void main(String[] args){

    props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.from", user);

    session = Session.getDefaultInstance(props, null);


    MimeMessage mime = new MimeMessage(session);
    mime.setFrom(new InternetAddress(user));
    mime.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    mime.setSubject("Email from: " + user);
    mime.setText(msg);
    Transport.send(mime);
}

我得到的错误消息:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port:smtp.google.com, 25; timeout -1;
  nested exception is:
        java.net.UnknownHostException: smtp.google.com
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
        at javax.mail.Service.connect(Service.java:345)
        at javax.mail.Service.connect(Service.java:226)
        at javax.mail.Service.connect(Service.java:175)
        at javax.mail.Transport.send0(Transport.java:253)
        at javax.mail.Transport.send(Transport.java:124)


        at com.mangopearapples.pcc.addons.EmailAddon.sendMail(EmailAddon.java:147)
        at com.mangopearapples.pcc.addons.EmailAddon.main(EmailAddon.java:106)


        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.net.UnknownHostException: smtp.google.com
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:585)
        at java.net.Socket.connect(Socket.java:534)
        at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
        at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
        ... 13 more
Error getting email: Messaging error - Couldn't connect to host, port: smtp.google.com, 25; timeout -1

错误消息引用的类的行(EmailAddon.java:147)是:

Transport.send(mime);

如前所述,我可以使用Folder.getUnreadMessageCount()之类的Folder方法成功访问电子邮件,因此我知道主机,电子邮件地址和密码不是问题。

感谢。

0 个答案:

没有答案