如何用java发邮件?[重复]


我试图用这个代码发送电子邮件,但每次都显示用户名和密码不接受.但我已经做了一切。我也设置了我的Gmail账户访问第三方应用程序。现在我不明白问题出在哪里。

Exception in thread "main" javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials d8sm6051763pfd.159 - gsmtp
    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:124)
    at com.mirajhossain.TransMail.sendMail(Main.java:37)
    at com.mirajhossain.Main.main(Main.java:52)][1]

这是我的代码。

class TransMail{
        public static void sendMail(String recepient) throws MessagingException {
            Properties properties=new Properties();
            properties.put("mail.from","REDACTED");
            properties.put(" mail.user","REDACTED");
            properties.put(" mail.passr","REDACTED");
            properties.put("mail.smtp.auth","true");
            properties.put("mail.smtp.starttls.enable","true");
            properties.put("mail.smtp.host","smtp.gmail.com");
            properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
            properties.put("mail.smtp.port","587");

            String myAccount="miraj98hossain@gmail.com";
            String pwd="69miraj69hossain69shawon69";






            Session session= Session.getInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(myAccount,pwd);
                }
            });

            Message message= preparemessage(session,myAccount,recepient);
            Transport.send(message);
            System.out.println("yes");
        }

    private static Message preparemessage(Session session, String myAccount,String recepient) throws MessagingException {
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress(myAccount));
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(recepient));
            message.setSubject("Verification");
            message.setText("1254562");
            return message;
    }
}
public class Main{
    public static void main(String[] args) throws MessagingException {
        TransMail.sendMail("miraj09hossain@gmail.com");
    }
}

解决方案:

确保你必须从你的gmail账户设置中打开不太安全的应用程序访问。

public class Main {
    public static void main(String [] args)
    {
        // Sender's email ID needs to be mentioned
        String from = "test@gmail.com";
        String pass ="xxxxxxx";
        // Recipient's email ID needs to be mentioned.
        String to = "test313@gmail.com";

        String host = "smtp.gmail.com";

        // Get system properties
        Properties properties = System.getProperties();
        // Setup mail server
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.user", from);
        properties.put("mail.smtp.password", pass);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);
        int Vcode=(int)(10000+Math.random()*(20000-10000+1));

        try{
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: header field
            message.setSubject("Email Verification");

            // Now set the actual message
            message.setText("Your verification code for our app is"+Vcode+".\n"+"Enter this code for complete your sign up process");

            // Send message
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            System.out.println("Sent message successfully....");
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

给TA打赏
共{{data.count}}人
人已打赏
解决方案

无法从应用程序中使用FaceTime

2022-4-21 22:00:12

解决方案

Dotnet核心SmtpClient - 在SendCompletedEventHandler上插入数据到db中。

2022-4-21 22:00:14

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索