|
Ol� de novo pessoal, Acabo de receber minha pr�pria mensagem (que
postei ontem) sobre imagens inline em e-mails. Ocorre que j� descobri a solu��o
e estou passando a todos neste e-mail (afinal j� se passaram quase 24 hs que
mandei o e-mail). Gostaria tamb�m de sugerir que a lista deixasse de ser
moderada, pois como n�o temos pessoas que ficam o dia todo lendo e-mail (o que �
normal, porque todos precisamos trabalhar), a lista peca por n�o nos dar uma
resposta r�pida do nosso problema (esse � o “goal” dos maillists, certo ?),
mesmo que algu�m tenha a resposta n�o poder� dar porque s� receber� a mensagem
no dia seguinte, e todos sabemos que neste mundo da inform�tica (e
principalmente no mundo web) as informa��es s�o necess�rias sempre “pra ontem”… A Solu��o que funciona sobre Outlook 2000 e
Express e Netscape mail 4.7 : // Esta solucao necessita do JavaMail 1.1.3 e
JDK 1.2.x ou superiores import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.net.*; // Own Import import com.tesla.classes.ByteArrayDataSource; public class InLineEmails { public static void main(String args[]) { InLineEmails that = new InLineEmails(); that.go(); } public void go() { // create some erties and get the default
Session Properties props = System.getProperties();
props.put("mail.smtp.host",
"smtp.tesla.com.br");
Session
ses = Session.getInstance(props,null);
try
{
// create a message
MimeMessage msg = new
MimeMessage(ses);
msg.setFrom(new
InternetAddress("[EMAIL PROTECTED]"));
Address[]
toAddress = InternetAddress.parse("[EMAIL PROTECTED]");
msg.setRecipients(Message.RecipientType.TO,toAddress); msg.setSubject("assunto"); MimeMultipart mp = new
MimeMultipart(); mp.setSubType("related"); MimeBodyPart mbp1= new
MimeBodyPart(); String html =
"<html>"+ "<head><title></title></head>"+ "<body>"+ "<b> see the
following jpg : it is a car!</b><br>"+ "<a
href=a.jsp>hello</a><br>"+ "<IMG SRC="cid:23abc@pc27"
width=80% height=60%><br>"+ "<b> end of
jpg</b>"+
"</body>"+
"</html>";
mbp1.setContent(html,"text/html"); MimeBodyPart mbp2 = new
MimeBodyPart(); FileDataSource fds = new
FileDataSource( "Teste.gif"); mbp2.setFileName(fds.getName()); mbp2.setText("This is a
beautiful car !"); mbp2.setDataHandler(new
DataHandler(fds)); mbp2.setHeader("Content-ID","23abc@pc27"); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg); }catch (Exception ex) { ex.printStackTrace();
} } } []s, Sergio
Stateri Jr Tesla Tecnologia Sao Paulo (SP) -----Mensagem original----- Ol� Pessoal, Como fa�o para enviar imagens in-line (aquelas imagens que podem ser
usadas em e-mails em formato HTML, em tags IMG por exemplo) em e-mails de forma
a funcionar no Outlook e no Netscape Mail ? Fiz um pequeno c�digo que funciona somente
para Outlook, mas no Netscape a imagem vem faltando (com �cone quebrado, como
se n�o tivesse imagem). A seguir apresento o c�digo que estou utilizando: Neste c�digo crio 2 MimeParts, sendo um o c�digo html (text/html) e
outro uma imagem gif (image/gif), coloco os dois em uma MultiPart e envio. No
outlook 2000 vejo a imagem normalmente, como numa p�gina HTML, mas no Netscape
Mail a imagem n�o vem (vem com �cone de imagem quebrada). import
java.util.*; import
java.io.*; import
javax.mail.*; import
javax.mail.internet.*; import
javax.activation.*; import
java.net.*; import
com.own.ByteArrayDataSource; public
class InLineEmails { public static void main(String args[]) { InLineEmails that = new InLineEmails(); that.go(); } public void go() { // create some erties and get the default Session Properties props = System.getProperties(); props.put("mail.smtp.host",
"smtp.tesla.com.br"); Session
ses = Session.getInstance(props,null); try
{ // create a message MimeMessage msg = new
MimeMessage(ses); msg.setFrom(new
InternetAddress("[EMAIL PROTECTED]")); Address[]
toAddress = InternetAddress.parse("[EMAIL PROTECTED]"); msg.setRecipients(Message.RecipientType.TO,toAddress); msg.setSubject("Teste de Assunto"); // Reading the HTML to a byte variable File theHTML = new File("C:\\Down\\texto.html"); FileInputStream fisHTML = new FileInputStream(theHTML); byte[] fileInBytesHTML = new byte[((int)(theHTML.length()))]; fisHTML.read(fileInBytesHTML); fisHTML.close(); MimeBodyPart mbp2 =
new MimeBodyPart(); mbp2.addHeaderLine("Content-Type: text/html;
charset=us-ascii"); mbp2.addHeaderLine("Content-Transfer-Encoding: 7bit"); mbp2.setDataHandler(new DataHandler(new
ByteArrayDataSource(fileInBytesHTML, "text/html"))); // Reading the Image to a byte variable File theFile = new File("C:\\Down\\Teste.gif"); FileInputStream fis = new FileInputStream(theFile); byte[] fileInBytes = new byte[((int)(theFile.length()))]; fis.read(fileInBytes); fis.close(); // create and fill the
image message part MimeBodyPart mbpImage
= new MimeBodyPart(); mbpImage.addHeaderLine("Content-Type:
image/gif"); mbpImage.addHeaderLine("Content-ID:
<[EMAIL PROTECTED]>"); mbpImage.addHeaderLine("Content-Transfer-Encoding:
base64"); mbpImage.addHeaderLine("Content-Disposition:
inline; filename=\"C:\\Down\\Teste.gif\""); mbpImage.setDisposition(Part.INLINE); mbpImage.setDataHandler(new DataHandler(new
ByteArrayDataSource(fileInBytes, "image/gif"))); // create the
Multipart and its parts to it Multipart mp = new
MimeMultipart(); //mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); mp.addBodyPart(mbpImage); // add the Multipart
to the message msg.setContent(mp); // set the Date:
header msg.setSentDate(new
Date()); // send the message Transport.send(msg); }catch (Exception ex) { ex.printStackTrace(); } } } No arquivo texto.html tenho um c�digo HTML
que ser� lido e inclu�do na mensagem: <html> Teste de e-mail <br> <img SRC="cid:[EMAIL PROTECTED]"
height=91 width=253> </html> Desde j� agrade�o, Sergio Stateri Jr Tesla Tecnologia Sao Paulo (SP) |
- [java-list] Imagens In-line no JavaMail 1.1.3 Sergio Stateri Jr
- Sergio Stateri Jr
