我如何在核心 java 中使用 google 发送邮件

how can i send mail using google in core java

这是我在核心中发送电子邮件的代码java.but它无法工作请提出一些解决方案。

final String username = "username@gmail.com";
    final String password = "password";

    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 session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("username@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("username@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
            + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

我在尝试使用 java 邮件时收到以下异常;

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:227)
    at javax.mail.Session.<init>(Session.java:212)
    at javax.mail.Session.getInstance(Session.java:248)
    at SendMailTLS.main(SendMailTLS.java:26)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    enter code here`at java.net.URLClassLoader.run(URLClassLoader.java:355)
    enter code here`at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 4 more

http://commons.apache.org/proper/commons-email/

尝试 "Apache Commons Email" 库

com.sun.mail.util.MailLogger 在 JavaMail API 中,Java SE 中没有。

尝试添加此 Maven 依赖项或将此 jar 获取到类路径

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.2</version>
</dependency>

有关 javamail 的更多详细信息 here