import java.io.IOException;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmailHtmlWithImg {
static String hostname = "smtp.gmail.com";
//Email id from which you want to send mail
static String username = "username@gmail.com";
//Password for that id
static String password = "password@gmail.com";
public static void main(String[] args) {
Properties props = new Properties();
//smtp.gmail.com is server of Gmail replace it if you want some other server
props.put("mail.smtp.host", hostname);
//465 is SSL port no for Gmail
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
//465 is SSL port no for Gmail
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
try {
MimeMultipart content = new MimeMultipart("related");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@gmail.com"));
message.setSubject("Mail Subject");
message.setText("mail message");
String cid = getContentId();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("<html><head>"
+ "<title>Esempio di Testo HTML Mail</title>"
+ "</head>\n"
+ "<body><div><b>Ciao</b></div>"
+ "<div>Invio del testo HTML con l'aggiunta di una immagine <i>Forte Vero!</i> </div>\n"
+ "<div>Qui sotto troverete una immagine:<br> <img src=\"cid:"
+ cid
+ "\" /></div>\n" + "<div><br>Qui sotto il testo dopo l'immagine!</div></body></html>",
"US-ASCII", "html");
content.addBodyPart(textPart);
// Image part
MimeBodyPart imagePart = new MimeBodyPart();
try {
imagePart.attachFile("img/PrivacyFix.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);
message.setContent(content);
Transport.send(message);
System.out.println("Fine/Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
static int seq = 0;
/**
* Sequence goes from 0 to 100K, then starts up at 0 again. This is large enough,
* and saves
* @return
*/
public static synchronized int getSeq() {
return (seq++) % 100000;
}
/**
* One possible way to generate very-likely-unique content IDs.
* @return A content id that uses the hostname, the current time, and a sequence number
* to avoid collision.
*/
public static String getContentId() {
int c = getSeq();
return c + "." + System.currentTimeMillis() + "@" + hostname;
}
}