2

Java アプリケーションから特定の電子メール アドレスに電子メールを送信しようとしています。Java Mail API を使用していますが、残念ながら SMTPSendFailedException エラーが発生しています。私がどこで間違いを犯しているのか、誰でも教えてくれますか?これが私のコードです

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

//import SeconMail.Authenticator;

public class SendMail
{

   public static void main(String [] args)
   {    

      // Recipient's email ID needs to be mentioned.
      String to = "to@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "from@expertflow.com";

      // Assuming you are sending email from localhost
      String host = "smtp.gmail.com";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server

      properties.setProperty("mail.smtp.host", host);


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

      properties.put("mail.smtp.auth", "false");
      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);
      session.setDebug(true);

      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("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}
4

8 に答える 8

1

それでもうまくいかない場合は、次のことを試してください。

properties.setProperty("mail.smtps.ssl.enable", "true");
properties.setProperty("mail.smtps.auth", "true"); 
于 2014-11-27T19:52:14.020 に答える
0
properties.setProperty("mail.smtp.user", "abc");
properties.setProperty("mail.smtp.password", "xyz");
properties.setProperty("mail.smtp.auth", "true");

それはうまくいきました、私の問題を解決してください

于 2021-06-22T03:05:06.903 に答える