Reputation: 131
Here is my code to send a MultiPartEmail
with attachment in 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("[email protected]");
email.addTo("[email protected]");
email.setMsg("Body Of Message");
email.attach(attachment);
email.send();
} catch (EmailException ee) {
ee.printStackTrace();
}
}
But, this exception occur:
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
Upvotes: 0
Views: 2065
Reputation: 7779
Google e-mail servers refuse to talk to you in plain text. You need to turn on SSL with
email.setTLS(true);
See: Sending email in Java using Apache Commons email libs
Upvotes: 1