当前位置: 首页 > 知识库问答 >
问题:

无法将MimeMultiPart附加到Transport.Send()

申屠喜
2023-03-14

我试图用java发送电子邮件,但我总是收到IOexception,知道我做错了什么吗?我想这是t.send不是transport.send()的问题,但我似乎无法让它工作,我一直得到无法连接错误之后…如有任何帮助,不胜感激:)

public void Send(String recipientEmail) throws AddressException, MessagingException {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    String title = titleBox.getText();
    // Get a Properties object
    Properties props = System.getProperties();
    props.setProperty("mail.smtps.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.setProperty("mail.smtps.auth", "true");


    props.put("mail.smtps.quitwait", "false");

    Session session = Session.getInstance(props, null);
    String ccEmail = "";
    // -- Create a new message --
    final MimeMessage msg = new MimeMessage(session);

    // -- Set the FROM and TO fields --
    msg.setFrom(new InternetAddress(emailUsername + "@gmail.com"));
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));

    if (ccEmail.length() > 0) {
        msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
    }
    String message = messageBox.getText();
    msg.setSubject(title);
    MimeBodyPart messagePart = new MimeBodyPart();
    messagePart.setText(message);
    multipart.addBodyPart(messagePart);
    //msg.setText(message, "utf-8");
    msg.setSentDate(new Date());
    msg.setContent(multipart);
    SMTPTransport t = (SMTPTransport)session.getTransport("smtps");

    t.connect("smtp.gmail.com", emailUsername, emailPassword);
    t.sendMessage(msg, msg.getAllRecipients());
    //Transport.send(msg);
    t.close();
}

我能够正确地ping smtp.gmail.com,我认为连接问题是由于使用transport.send()造成的,在我开始使用MultiParts之前,这也是有效的,因为我以前用它发送过电子邮件

下面是堆栈跟踪

javax.mail.MessagingException: IOException while sending message;
   nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
boundary="----=_Part_0_1039039084.1386965872274"
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
at examples.ContactEditor.Send(ContactEditor.java:445)
at examples.ContactEditor.jButton7ActionPerformed(ContactEditor.java:395)
at examples.ContactEditor.access$400(ContactEditor.java:53)
at examples.ContactEditor$5.actionPerformed(ContactEditor.java:249)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
boundary="----=_Part_0_1039039084.1386965872274"
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:891)
at javax.activation.DataHandler.writeTo(DataHandler.java:317)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
... 40 more

如果我使用静态transport.send(),它会出现以下异常。

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 465;
  nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at examples.ContactEditor.Send(ContactEditor.java:446)
at examples.ContactEditor.jButton7ActionPerformed(ContactEditor.java:396)
at examples.ContactEditor.access$400(ContactEditor.java:53)
at examples.ContactEditor$5.actionPerformed(ContactEditor.java:249)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 46 more

共有1个答案

连晟
2023-03-14

您似乎没有为邮件设置内容类型。

 类似资料:
  • 我有两个不同的JS函数来处理图像上传。一个用于读取文件并(全局)创建FormData对象,另一个用于通过AJAX将其发送到PHP文件。 即使是有效的,我也不能将它附加到FormData和字符串中。事实证明,我只是通过Ajax发送空的XH请求,因为我不会从PHP中获得任何其他内容,然后空数组。 因为第一个JS函数在全局初始化formData变量,所以第二个函数使用它应该没有问题,对吗? 我的Java

  • 我有一个程序,它总是将相同的文件附加到GMAIL(撰写 它所做的是: 在Android 6.0之前,它一直工作良好。现在,我在尝试使用它时收到以下错误: 无法附加空文件 Astro文件共享给我同样的错误(可能是旧版本)。 但是,我安装了ES File Explorer,当我执行相同的例程并选择该文件时,我会收到一个对话框,其中显示: 将文件作为 普通Android方式(适用于彩信、Gmail等)

  • 我有3个维度为(a*2、b*2、c*2)的数据帧,而a、b、c是不同的数字。我想把它们添加到一个csv文件中,但每次都写不出来。我使用了csv的熊猫数据帧输出端 但做不到。 我的代码- 但是,我得到的最终输出是最后一个数据帧,而不是所有其他数据帧。

  • 我在我的项目中使用cucumber版本4。我在我的项目中使用以下依赖项。 使用上述依赖关系,我能够生成“cucumberjvm报告”。 我还用java编写了代码,用于将屏幕截图附加到报告中,我只在场景失败时拍摄屏幕截图。 我不确定是否遗漏了什么,但屏幕截图没有附加到报告中。cucumber生成的默认html报告正在显示屏幕截图。 谁能帮我一下吗。谢谢

  • 我花了无数个小时试图让附件资源API正常工作,但毫无效果。我已经参考了这里的文件:http://docs.getzephyr.apiary.io/#executionresourceapis 但是他们没有多大帮助,泽法的支持在过去的3个月里没有回答我的任何问题。 这是我的卷发电话: 我也尝试过php: 对于这两个例子,我都得到不支持的媒体类型。这没有意义,因为我可以通过Jira附加它。我在这一点上

  • 我无法将 Android Studio 的调试器附加到我的可调试应用程序进程。还有其他人有这个问题吗?基本上,我无法在”对话框中列出我的应用程序。 我正在选择: 然后,以下是Android Studio的对话框的样子: 知道为什么我的应用程序没有显示在< code >选择进程列表中吗? 虽然,在list中看到了我的应用程序: 还可以在列表中看到该应用程序: 我试过这些动作: < li >重新启动<