public static void main(String [] args){

  String to="[email protected]";//change accordingly
  final String user="[email protected]";//change accordingly
  final String password="xxxxxx";//change accordingly

  Properties pro = System.getProperties();
  pro.setProperty("mail.smtp.host", "mail.javatpoint.com");
  pro.put("mail.smtp.auth", "true");

  Session session = Session.getDefaultInstance(pro,  new javax.mail.Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(user,password);
           }
          });

  try{
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(user));
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
      message.setSubject("certificate");

      BodyPart msg = new MimeBodyPart();
      msg.setText("bodyPartMsg");

      MimeBodyPart messageBodyPart2 = new MimeBodyPart();
      System.out.println("attach");
        String filename = "C:\\Users\\Rudresh Mehta\\Desktop\\flow for certiManipulator";//change accordingly
      System.out.println("attach complete");
        DataSource source = new FileDataSource(filename);
        messageBodyPart2.setDataHandler(new DataHandler(source));
        messageBodyPart2.setFileName(filename);

        Multipart multipart = new javax.mail.internet.MimeMultipart();

        multipart.addBodyPart(msg);
        multipart.addBodyPart(messageBodyPart2);


        //6) set the multiplart object to the message object
        message.setContent(multipart );
        //7) send message


*我的问题是,为什么它到达这里时,比提供运输时要早一些,然后又从它中移出并且不发送邮件,并且如标题中所述,当打印e时,错误显示在catch中。
Transport.send(message);
System.out.println(“已发送邮件...”);

  }catch(Exception e)
  {
      System.out.print(e);
      System.out.println("in catch");
  }
  System.out.println("complete");

最佳答案

如果未正确选择本地主机名,请将mail.smtp.localhost属性设置为您的主机名。

08-17 11:51