Eclipse Java,以编程方式从Gmail SMTP发送电子邮件

时间:2015-07-13 19:45:53

标签: java eclipse email gmail javamail

我正在尝试使用Java在Eclipse中以编程方式发送电子邮件。我使用最新的JDK 1.8.0_45正确设置了项目。我的意思是我已加入mailapi.jarsmtp.jar

我已经阅读了关于该主题的大量其他帖子,我知道我的Java构建路径必须包含mail.jar。但是,当我搜索文件夹时,找不到mail.jar; mailapi.jar是最接近的,似乎解决了我没有它的包含错误。

这是我的代码:

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

public class emailer {

    private static String host = "smtp.gmail.com";
    private static String user = "**********";
    private static String pass = "***********";

    public static void sendEmail(String fromAddr, String toAddr, String subject, String body)
    {
        Properties prop = System.getProperties();
        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.host", host);
        prop.put("mail.smtp.user", user);
        prop.put("mail.smtp.password", pass);
        prop.put("mail.smtp.port", "465");
        prop.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(prop);
        MimeMessage message = new MimeMessage(session);

        try
        {
            message.setFrom(new InternetAddress(fromAddr));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, user, pass);
            transport.send(message, message.getAllRecipients()); 
            //The above line has a warning message
            //"The static method send(Message, Address[]) from the type Transport should be accessed in a static way"
            transport.close();
            System.out.println("done");
        }
        catch (AddressException e) {e.printStackTrace();}
        catch (MessagingException e) {e.printStackTrace();}
    }
}

代码编译时没有错误,并且在运行时,继续无限期地运行,从不破坏或停止。所以不幸的是,我没有显示堆栈跟踪......非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

将传出端口更改为587而不是465.这可以消除无限超时(因为Transport没有超时值)。此外,使用静态Transport.send()将大大简化您的代码(我在使用非静态方法时也遇到了一些错误)。就像这样。

public class Emailer  //Class names should always be capitalized
{

private static String host = "smtp.gmail.com";
private static String user = "**********";
private static String pass = "***********";

public static void sendEmail(String fromAddr, String toAddr, String subject, String body)
{
    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.getInstance(props, null);

    MimeMessage message = new MimeMessage(session);

    try
    {
        message.setFrom(new InternetAddress(fromAddr));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
        message.setSubject(subject);
        message.setText(body);
        Transport.send(message, user, pass);
    }
    catch (AddressException e) {e.printStackTrace();}
    catch (MessagingException e) {e.printStackTrace();}
}

答案 1 :(得分:0)

您可能想要包含javax.mail.jar

这是您可以从

下载的主页

https://java.net/projects/javamail/pages/Home