【问题标题】:Send Email with apache commons使用 apache commons 发送电子邮件
【发布时间】:2014-03-15 06:40:37
【问题描述】:

这是我用 Java 发送带有附件的 MultiPartEmail 的代码:

public static void main(String[] args) {

    // Create The Attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath("check.png");
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("My file on my system");
    attachment.setName("Check icon");

    // Create The Email
    MultiPartEmail email = new MultiPartEmail();
    try {
        email.setHostName("smtp.googlemail.com");
        email.setAuthentication("MyUsername", "Mypassword");
        email.setFrom("Myemail@gmail.com");
        email.addTo("myfriend@gmail.com");
        email.setMsg("Body Of Message");
        email.attach(attachment);
        email.send();

    } catch (EmailException ee) {
        ee.printStackTrace();
    }
}

但是,发生了这个异常:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
at org.apache.commons.mail.Email.send(Email.java:1437)
at networking.EmailWithAttachment.main(EmailWithAttachment.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. a4sm18234039eep.12 - gsmtp

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2133)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1630)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1132)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 7 more

【问题讨论】:

  • 530 5.7.0 Must issue a STARTTLS command first.

标签: java apache email


【解决方案1】:

Google 电子邮件服务器拒绝以纯文本形式与您交谈。您需要打开 SSL

email.setTLS(true);

见:Sending email in Java using Apache Commons email libs

【讨论】:

  • 它适用于 email.setTLS(true); ,但 IDE 警告此方法 Deprecated
猜你喜欢
  • 2010-10-25
  • 1970-01-01
  • 2018-09-22
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 2016-10-02
  • 1970-01-01
相关资源
最近更新 更多