简体   繁体   中英

Java Mail: won't sent an email

I have a code that worked fine earlier on, but now it won't sent emails. Is there something wrong in my code?

There is an error that says that the program couldn't connect with the SMTP server of Google.

Here's the code:

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;


public class Mailing
{

    public Mailing()
    {


    }
    public void getMail(String warning,String subject)
    {
        final String username = "wim81.vangeyt@gmail.com";
        final String password = "Minidisc";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.startssl.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");

        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("wim81.vangeyt@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("wimvangeyt@outlook.com"));
            message.setSubject(subject);
            message.setText(warning);

            Transport.send(message);

            System.out.println("Done");

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

    }
    public static void main(String[] args)
    {
        Mailing mailing = new Mailing();
        mailing.getMail("test", "test mail");
    }

}

May be enable the settings in gmail account which you have given in the code from where the email can be sent. probably this would help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM