如何在java程序中发送电子邮件,向收件人发送控制台显示的内容?

如何在java程序中发送电子邮件,向收件人发送控制台显示的内容?,java,eclipse,email,selenium-webdriver,Java,Eclipse,Email,Selenium Webdriver,我正在制作一个SeleniumWebDriver,它运行后会在控制台中生成一个表,显示哪些应用程序失败或通过了我的测试。我希望能够在运行此表后将其发送给其他人。这是我的程序表部分的代码。它确实成功地制作了表格,我只是以前从未用java程序发送过电子邮件。提前谢谢 String to = "abcd@gmail.com"; String from = "efg@gmail.com"; String host = "localhost";

我正在制作一个SeleniumWebDriver,它运行后会在控制台中生成一个表,显示哪些应用程序失败或通过了我的测试。我希望能够在运行此表后将其发送给其他人。这是我的程序表部分的代码。它确实成功地制作了表格,我只是以前从未用java程序发送过电子邮件。提前谢谢

        String to = "abcd@gmail.com";
        String from = "efg@gmail.com";
        String host = "localhost";
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        Session session = Session.getDefaultInstance(properties);
        try{
            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress(from));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            message.setSubject("TEST RESULTS");

            //Something needs to go here I think
        }
    }

我建议在stringbuffer中收集输出并使用log4j。配置log4j smtp追加器并记录结果。Log4j将负责邮寄您的结果。
看这里:

这里有一个使用gmail发送电子邮件的课程,它可能适合你。你需要给它你的gmail用户名和密码

import com.sun.mail.smtp.SMTPTransport;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GoogleMail {
    private GoogleMail() {
    }

    /**
     * Send email using GMail SMTP server.
     *
     * @param username GMail username
     * @param password GMail password
     * @param recipientEmail TO recipient
     * @param title title of the message
     * @param message message to be sent
     * @throws AddressException if the email address parse failed
     * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
     */
    public static void send(final String username, final String password, String recipientEmail, String title, String message) throws AddressException, MessagingException {
        GoogleMail.Send(username, password, recipientEmail, "", title, message);
    }

    /**
     * Send email using GMail SMTP server.
     *
     * @param username GMail username
     * @param password GMail password
     * @param recipientEmail TO recipient
     * @param ccEmail CC recipient. Can be empty if there is no CC recipient
     * @param title title of the message
     * @param message message to be sent
     * @throws AddressException if the email address parse failed
     * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
     */
    public static void send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtps.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtps.auth", "true");

        /*
        If set to false, the QUIT command is sent and the connection is immediately closed. If set 
        to true (the default), causes the transport to wait for the response to the QUIT command.

        ref :   http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
                http://forum.java.sun.com/thread.jspa?threadID=5205249
                smtpsend.java - demo program from javamail
        */
        props.put("mail.smtps.quitwait", "false");

        Session session = Session.getInstance(props, null);

        // -- Create a new message --
        final MimeMessage msg = new MimeMessage(session);

        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(username + "@gmail.com"));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));

        if (ccEmail.length() > 0) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
        }

        msg.setSubject(title);
        msg.setText(message, "utf-8");
        msg.setSentDate(new Date());

        SMTPTransport t = (SMTPTransport)session.getTransport("smtps");

        t.connect("smtp.gmail.com", username, password);
        t.sendMessage(msg, msg.getAllRecipients());      
        t.close();
    }
}

我忘了提到我确实安装了JavaMail和Java激活框架,并且在classpath
String to=“abcd$gmail.com”中这不应该是
字符串to=”abcd@gmail.com";
@Chris是的,谢谢你看到我的打字错误,这不是主要的问题,但当我把其余的都搞定的时候,我会有问题的