Java Email Sender

时间:2016-01-08 20:06:28

标签: java email javamail

我想发送电子邮件,但我不知道我在这里做错了什么。你们有人可以帮我吗?

EmailSender:

public class EmailSender {

    public static boolean sendMail(String from, String password, String message, String to[]) {
        String host = "smtp.gmail.com";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.port", 587);
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage mineMessage = new MimeMessage(session);
        try {
            mineMessage.setFrom(new InternetAddress(from));
            InternetAddress[] toAddresses = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {
                toAddresses[i] = new InternetAddress(to[i]);
            }
            for (int i = 0; i < to.length; i++) {
                mineMessage.setRecipients(Message.RecipientType.TO, toAddresses);
            }

            mineMessage.setSubject("mail using javam");
            mineMessage.setText(message);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, password);
            transport.sendMessage(mineMessage, mineMessage.getAllRecipients());
            transport.close();
            return true;

        } catch (MessagingException ex) {
            Logger.getLogger(EmailSender.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}

这里是主要的:

public class Main {

    public static void main(String[] args) {
        String[] to = {"kaleck.vd@gmail.com"};
        if (EmailSender.sendMail("something@yahoo.com", "******", "message to recive", args)){
            System.out.println("The mail has sent");
        }
        else{
            System.out.println("Don't work");
        }
    }
}

这是我的错误信息:

  

2016年1月8日下午10:01:49 recruitment.system.utils.EmailSender sendMail   SEVERE:null javax.mail.AuthenticationFailedException:534-5.7.14    请通过您的网站登录   浏览器和534-5.7.14然后再试一次。 534-5.7.14在534了解更多   5.7.14 https://support.google.com/mail/answer/78754 w73sm621753wmw.21 - gsmtp

我需要从地址(任何类型的地址)发送一封邮件,不仅仅是Gmail或Yahoo,还有另一个地址,可以是任何东西。 我尝试使用那种方法,但我不知道我在哪里做错了,因为它不起作用......而且不仅仅是这样,我试图发送邮件的所有方法都失败了..请告诉我我失败的地方,以及如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您需要输入Gmail设置并启用IMAP访问

看到这个链接: https://support.google.com/mail/answer/78775?hl=en