Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.612
  • Views Posts: 542.418
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

JAVA - Inviare Email in formato HTML con l'aggiunta di Immagini e Connessione SSL (Codice)

JAVA - Inviare Email in formato HTML con l'aggiunta di Immagini e Connessione SSL (Codice)


  

Oggi voglio mettervi ha disposizione una funzione che vi permette di inviare una Email in Java nel formato HTML aggiungendo le immagini nell'email. 

Esempio volete inviare una email con la fine i vostri dati e il vostro logo della società o quello che volete. Ora usando la funzione che vi metto a disposizione lo potete fare.

Ho deciso di inviare l'email utilizzando Google ma lo potete modificare come volete a vostro piacimento.

Ecco cosa potete ottenere in Java con un progetto di Esempio :

(ATTENZIONE!!! Prima di procedere dovete scaricare le ultime Jar di JavaMail dal seguente sito internet dell'Oracol : http://www.oracle.com/technetwork/java/index-138643.html, vi allego anche un'altro link dove poterlo scaricare : Download API Java Mail )

CODICE JAVA : (Completo)

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;

 }

}


 
Potete scaricare il file d'esempio :

Password :  "ImaginSystem" 

Link File Download : Download SendEmailHtmlWithImg Java (Eclipse)

Attenzione!!! Se ci sono problemi dell'eseguire il file, prima controllate se avete importato bene le librerie che ho allegato dentro al file nella cartella "lib" .


By ImaginSystems & Queen Gin  

Categoria: JAVA
giovedì, 11 set 2014 Ore. 08.04

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003