Please modify the code to this :
String to = "mail@example.com";
// Sender's email ID needs to be mentioned
String from = "sender@example.com";
final String username = "user";//change accordingly
final String password = "pwd";//change accordingly
// Assuming you are sending email through outlook mail
String host = "outlook.office365.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(msgSubject);
message.setSentDate(new java.util.Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<p>Hi Team,</p> <p>Please find the Report for the day </p> <p> </p> <img src=\"cid:image\"> <p> Regards, </p> <p> Team</p>";
messageBodyPart.setContent(htmlText, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "<image>");
//add this to avoid unwanted attachment.
imagePart.setDisposition(MimeBodyPart.INLINE);
imagePart.attachFile(new File("C:\\abc.png"));
multipart.addBodyPart(imagePart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException | IOException ex) {
throw new RuntimeException(e);
}
Your code works, apart from setting up the connection with the SMTP server. You need a running mail (SMTP) server to send you email for you.
Here is your modified code. I commented out the parts that are not needed and changed the Session creation so it takes an Authenticator. Now just find out the SMPT_HOSTNAME, USERNAME and PASSWORD you want to use (your Internet provider usually provides them).
I always do it like this (using a remote SMTP server I know) because running a local mailserver is not that trivial under Windows (it's apparently quite easy under Linux).
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
//import javax.activation.*;
public class SendEmail {
private static String SMPT_HOSTNAME = "";
private static String USERNAME = "";
private static String PASSWORD = "";
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending email from localhost
// String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", SMPT_HOSTNAME);
// Get the default Session object.
// Session session = Session.getDefaultInstance(properties);
// create a session with an Authenticator
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}