如何使用 GMail、Yahoo 或 Hotmail 通过 Java 应用程序发送电子邮件? [英] How can I send an email by Java application using GMail, Yahoo, or Hotmail?

查看:51
本文介绍了如何使用 GMail、Yahoo 或 Hotmail 通过 Java 应用程序发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 GMail 帐户从我的 Java 应用程序发送电子邮件?我已经用 Java 应用程序配置了我的公司邮件服务器来发送电子邮件,但是当我分发应用程序时这不会削减它.任何使用 Hotmail、Yahoo 或 GMail 的回答都是可以接受的.

Is it possible to send an email from my Java application using a GMail account? I have configured my company mail server with Java app to send email, but that's not going to cut it when I distribute the application. Answers with any of using Hotmail, Yahoo or GMail are acceptable.

推荐答案

首先下载JavaMail API 并确保相关的 jar 文件在您的类路径中.

First download the JavaMail API and make sure the relevant jar files are in your classpath.

这是一个使用 GMail 的完整工作示例.

Here's a full working example using GMail.

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

public class Main {

    private static String USER_NAME = "*****";  // GMail user name (just the part before "@gmail.com")
    private static String PASSWORD = "********"; // GMail password
    private static String RECIPIENT = "lizard.bill@myschool.edu";

    public static void main(String[] args) {
        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Java send mail example";
        String body = "Welcome to JavaMail!";

        sendFromGMail(from, pass, to, subject, body);
    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

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

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
    }
}

自然,您会想要在 catch 块中做更多的事情,而不是像我在上面的示例代码中那样打印堆栈跟踪.(删除 catch 块以查看来自 JavaMail API 的哪些方法调用抛出异常,以便您更好地了解如何正确处理它们.)

Naturally, you'll want to do more in the catch blocks than print the stack trace as I did in the example code above. (Remove the catch blocks to see which method calls from the JavaMail API throw exceptions so you can better see how to properly handle them.)

感谢 @jodonnel 以及所有回答问题的人.我悬赏他,因为他的回答使我获得了完整答案的 95%.

Thanks to @jodonnel and everyone else who answered. I'm giving him a bounty because his answer led me about 95% of the way to a complete answer.

这篇关于如何使用 GMail、Yahoo 或 Hotmail 通过 Java 应用程序发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆