为什么我的电子邮件不会发送?

时间:2017-08-01 17:19:50

标签: java email gmail javamail

我正在编写一个程序,要求我发送电子邮件。代码如下所示以及我遇到的错误。当然,在真实的程序中,出于保密原因,我将使用我刚刚替换它们的实际电子邮件。

package SendEmail;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;
public class SendEmail {

    public static void main(String args[]){
        try{
            String host ="smtp.gmail.com" ;
            String user = "Your Email";
            String pass = "Your Password";
            String to = "Reciever Email";
            String from = "Your Email";
            String subject = "TEST EMAIL";
            String messageText = "This email is a test";
            boolean sessionDebug = false;

            Properties props = System.getProperties();

            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.required", "true");

            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(sessionDebug);
            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject); msg.setSentDate(new Date());
            msg.setText(messageText);

           Transport transport=mailSession.getTransport("smtp");
           transport.connect(host, user, pass);
           transport.sendMessage(msg, msg.getAllRecipients());
           transport.close();
           System.out.println("message send successfully");
        }catch(Exception ex)
        {
            System.out.println(ex);
        }

    }
}

这是错误:

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsU
534-5.7.14 t3OAVI8JE19aKku2h4ZjEX_buFcH_KGTK-duOfH_YcQHPverRtgEyegDwBN4uelBea8Lyc
534-5.7.14 rxgCTVHxUciyb9STZ1Lgr19bd2SLJAyI55htJ1C_HFJo2NYu5DU-HNwQ6UbCShxgmslvXU
534-5.7.14 d_bckUCctyNWVNMf8my29et9jx8tliyClKsUW6zY_2WbLonmHG4aODJUoal7O-Qs0RKvTb
534-5.7.14 opqVsGO_QwUcMl9fQUlGc9SYf6BSU> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 h16sm28086045wrc.89 - gsmtp

0 个答案:

没有答案