【问题标题】:how to send email in java? [duplicate]如何在java中发送电子邮件? [复制]
【发布时间】:2023-04-10 13:35:02
【问题描述】:

我正在尝试使用此代码发送电子邮件,但每次都显示用户名和密码不被接受。 但我已经做了一切。我还设置了我的 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");
    }
}

【问题讨论】:

  • 一般来说,您不希望将您的实际电子邮件/密码放在问题中。
  • 过去的问题可能有助于快速回答这个问题stackoverflow.com/search?q=java+mail+gmail+smtp
  • 我已经看过他们的代码并尝试过,但仍然显示 AuthenticationFailedException 用户名和密码不被接受。
  • 对不起,代码现在运行良好。是我的电子邮件被阻止了。我在设置中打开了不太安全的应用程序。它运行良好。

标签: java gmail jakarta-mail


【解决方案1】:

确保您必须从您的 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();
        }
    }
}

【讨论】:

    【解决方案2】:

    我正在使用 JavaMail 库。看看它的样子:

    配置:

            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    
            mailSender.setHost(host); // for example smtp.gmail.com
            mailSender.setPort(port); // 465
            mailSender.setUsername(username); // your email
            mailSender.setPassword(password); // your password
    
            Properties properties = mailSender.getJavaMailProperties();
    
            properties.setProperty("mail.transport.protocol", protocol); // smtps
            properties.setProperty("mail.debug", debug); // as you wish
            properties.setProperty("mail.smtp.auth", auth); // true 
            properties.setProperty("mail.smtp.starttls.enable", enable); // true 
    
        }
    

    发送消息:

    private void send(String subject, String message) {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
    
            mailMessage.setFrom(emailFrom);
            mailMessage.setTo(emailTo); // receiver
            mailMessage.setSubject(subject);
            mailMessage.setText(message);
    
            mailSender.send(mailMessage);
    
        }
    

    试试这个。

    【讨论】:

    • 我试过你的代码,但它显示异常[线程“主”org.springframework.mail.MailSendException中的异常:邮件服务器连接失败;嵌套异常是 javax.mail.NoSuchProviderException: No provider for protocol。失败消息:javax.mail.NoSuchProviderException:没有协议提供者;消息异常详细信息 (1) 为:失败消息 1:javax.mail.NoSuchProviderException: No provider for protocol]
    • 因为你低不明白怎么办?问题不在于代码,而在于您的理解@Miraj Hossain Shawon
    猜你喜欢
    • 2012-04-17
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2018-03-05
    相关资源
    最近更新 更多