Java 使用apache commons发送电子邮件

Java 使用apache commons发送电子邮件,java,apache,email,Java,Apache,Email,以下是我用Java发送带有附件的多端口邮件的代码: public static void main(String[] args) { // Create The Attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath("check.png"); attachment.setDisposition(EmailAttachment.ATTACHMENT);

以下是我用Java发送带有附件的
多端口邮件的代码:

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

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

email.setTLS(true);

请参阅:

530 5.7.0必须首先发出STARTTLS命令。
它与
email.setts一起工作(true),但IDE警告此方法已弃用。