javax.mail:无法向 SMTP 主机发送命令

我是 Java Mail 的新手。当我执行下面的代码时,我遇到了一个异常


javax.mail.MessagingException:无法向 SMTP 主机发送命令


public void sendMessage(EmailMessage emailMessage) throws MessagingException {


    Properties props = new Properties();

    props.put("mail.smtp.auth", "true");

    props.put("mail.smtp.starttls.enable", "true");

    props.put("mail.smtp.host", "smtp.gmail.com");

    props.put("mail.smtp.port", "587");


    System.out.println(emailMessage.getFromAddress());

    Optional<EmailId> emailIdOptional = emailIdRepository.findByEmailId(emailMessage.getFromAddress());

    System.out.println(emailIdOptional.get().getEmailId());


    Session session = Session.getInstance(props, new javax.mail.Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {

            return new PasswordAuthentication(emailIdOptional.get().getEmailId(), emailIdOptional.get().getPassword());

        }

    });

    javax.mail.Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(emailMessage.getFromAddress(), false));


    msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailMessage.getToAddress()));

    msg.setSubject(emailMessage.getSubject());

    msg.setContent(emailMessage.getMessage(), "text/html");

    msg.setSentDate(new Date());


    MimeBodyPart messageBodyPart = new MimeBodyPart();

    messageBodyPart.setContent(emailMessage.getMessage(), "text/html");


    Transport.send(msg);

}

任何人都请帮我解决这个问题。


湖上湖
浏览 237回答 2
2回答

杨魅力

String host = "smtp.xyz.com";final String username = "xyz@business.com";final String password = "your password";Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");// go to account setting -> smtp setting and get server name and portprops.put("mail.smtp.host", "your server name");props.put("mail.smtp.port", "your server port");props.put("mail.debug", "true");try {&nbsp; &nbsp; Session session = Session.getInstance(prop,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new javax.mail.Authenticator() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected javax.mail.PasswordAuthentication getPasswordAuthentication() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new javax.mail.PasswordAuthentication(username, password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; Message msg = new MimeMessage(session);&nbsp; &nbsp; msg.setFrom(new InternetAddress("xyz@business.com"));&nbsp; &nbsp; msg.setRecipients(Message.RecipientType.TO,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InternetAddress.parse("abc@business.com"));&nbsp; &nbsp; msg.setSubject("Test E-Mail through Java");&nbsp; &nbsp; Transport.send(msg,username,password);&nbsp; &nbsp; System.out.println("Sent message successfully...");}catch(Exception e){}你可以试试这个可能会解决你的问题!!!!

牛魔王的故事

此代码对我有用:-public static void SendMessage(final String femail, final String fpass, final String email, final String subject, final String message){//Creating propertiesProperties props = new Properties();//Configuring properties for gmail//If you are not using gmail you may need to change the values&nbsp;props.put("mail.smtp.host", "smtp.gmail.com");&nbsp;props.put("mail.smtp.port", "587");&nbsp;props.put("mail.smtp.auth", "true");&nbsp;props.put("mail.smtp.starttls.enable", "true");&nbsp;props.put("mail.smtp.ssl.trust", "smtp.gmail.com");&nbsp; &nbsp; &nbsp;//Creating a new sessionSession session = Session.getDefaultInstance(props,&nbsp; &nbsp; &nbsp; &nbsp; new javax.mail.Authenticator() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Authenticating the password&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected PasswordAuthentication getPasswordAuthentication() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new PasswordAuthentication(femail, fpass);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });try {&nbsp; &nbsp; //Creating MimeMessage object&nbsp; &nbsp; MimeMessage mm = new MimeMessage(session);&nbsp; &nbsp; //Setting sender address&nbsp; &nbsp; mm.setFrom(new InternetAddress(femail));&nbsp; &nbsp; //Adding receiver&nbsp; &nbsp; mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));&nbsp; &nbsp; //Adding subject&nbsp; &nbsp; mm.setSubject(subject);&nbsp; &nbsp; //Adding message&nbsp; &nbsp; mm.setText(message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; //Sending email&nbsp; &nbsp; Transport.send(mm);} catch (MessagingException e) {&nbsp; &nbsp; e.printStackTrace();}}我已经编辑了代码并使用 starttls 方法进行了设置。希望会有所帮助!!!!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java