I am curious is there any way to send emails with attachments directly from Java code. The email belongs to the company mylogin
@companyxxx.com` I.e. manipulate Outlook using Java? Or maybe I do not need to manipulate Outlook, and it is enough to have login and password and other staff...
Asked
Active
Viewed 3,058 times
1

Бывший Мусор
- 169
- 1
- 5
- 12
-
If you're not fussed about using Outlook, you can use JavaMail, for [example](http://stackoverflow.com/questions/16117365/sending-mail-attachment-using-java) and [example](http://stackoverflow.com/questions/3177616/how-to-attach-multiple-files-to-an-email-using-javamail) and [example](http://stackoverflow.com/questions/8973026/java-mail-attachments-inline-images) – MadProgrammer Oct 11 '13 at 06:12
-
Thank you ... The only problem is that this is a kind of confidential email system ... so i have no idea how to reach to it other than using Outlook ... These examples mainly address regular gmail etc - - - public mailboxes – Бывший Мусор Oct 11 '13 at 06:15
-
Mail still needs to be sent to a smtp or imap server for distribution, so even if it was an internal mail server, the process would be the same...not sure about exchange though... – MadProgrammer Oct 11 '13 at 06:18
-
Exchange can be setup to accept smtp – Scary Wombat Oct 11 '13 at 06:19
1 Answers
2
Yes you can do that using javamail, you can download jar from here . For outlook, you just have to set properties as follows:
Properties props = new Properties();
props.put("mail.smtp.user", username);
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.socketFactory.port", "587");
Or if you want full example, use the method below:
public int sendMailWithAttachment(String to, String subject, String body, String filepath, String sendFileName) {
final String username = "YOUR EMAIL";
final String password = "YOUR PWD";
Properties props = new Properties();
props.put("mail.smtp.user", username);
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.socketFactory.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<html><body>HELLO</body></html>", "text/html");
DataSource source = new FileDataSource(filepath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(sendFileName);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
Transport.send(message);
return 1;
} catch (Exception e) {
return 0;
}
}

Jhanvi
- 5,069
- 8
- 32
- 41