In my previous post, I’ve given you the code which sends email using Microsoft’s EWS web java API. The example given below does the same task using java mail API. This should work for EWS , outlook 365 and gmail.
package org.grassfield;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "pandian@myoutlook365.com";
final String password = "mypassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.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("pandian@myoutlook365.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("mygirlfriend@likeverymuch.com"));
message.setSubject("Test TLS Subject");
message.setText("Body of the email");
Transport.send(message);
System.out.println("Sent");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

how to send mail with attachements with office365 using javamail api
Pls use java multiparts
I have tried something like this..
Can you check this… if it’s wrong can you guide me or give the code snippet for sending mail with attachment
final String username = “mymailid@oops.com”;
final String password = “zEsAce9E”;
Properties props = new Properties();
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.starttls.enable”, “true”);
props.put(“mail.smtp.host”, “smtp.office365.com”);
props.put(“mail.smtp.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(“mymailid@oops.com”));
Transport transport =session.getTransport();
MimeBodyPart messageBodyPart = new MimeBodyPart();
//messageBodyPart.setContent(“With Attachments”, “text/html”);
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// String[] attachFiles = {“D:/Version.java”, “D:/PoCMacro.java”};
String[] attachments = {“cdediff.txt”, “uidiff.txt”, “pltdiff.txt”};
String[] attachFiles = attachments;
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(new File(filePath));
} catch (IOException ex) {
ex.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail’s content
message.setContent(multipart);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(“tomail@oops.com”));
message.setRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(“bcdmail@oops.com”));
message.setSubject(“iOS Mail”);
//message.setText(“with out attchment code”);
message.setContent(“with html body”, “text/html”);
transport.connect();
Transport.send(message);
transport.close();
System.out.println(“Sent”);
Thanks in advance…..