Dúvida: Inserir Imagem Anexada em Email HTML

1 resposta
S

Alguém pode me tirar esta dúvida, estou precisando urgente, obrigado!!
http://www.guj.com.br/posts/list/203546.java

Em resumo, quero fazer com que uma imagem que anexei apareça no corpo da mensagem escrita em HTML (Email)

1 Resposta

C

Tenho este exemplo que anexa imagem no e-mail, não é em HTML mas acho que se vc alterar vai conseguir alterar, fiz usando pra mandar usando e-mail do Terra:

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;  
import javax.mail.internet.*;  

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;  
public class Email {  

	public void sendSimpleMail ()throws AddressException, MessagingException {  

		Properties mailProps = new Properties();  
		//definição do mailserver  

		mailProps.put("mail.host", "smtp.terra.com.br");
		//mailProps.put("mail.host", "smtps.bol.com.br");
		mailProps.put ("mail.smtp.auth", "true"); 
		Session mailSession = Session.getDefaultInstance (mailProps, new Authenticator() {  
			protected PasswordAuthentication getPasswordAuthentication() {  
				return new PasswordAuthentication("nome_de_usuario", "sua_senha");  
			}  
		});  
		mailSession.setDebug (true);

		MimeMessage msg = new MimeMessage(mailSession);  
		msg.setFrom(new InternetAddress("endereço_email_destinatario"));  
		//InternetAddress[] address = { new InternetAddress("endereço_email_remetente) };  
		InternetAddress[] address = { new InternetAddress("endereço_email_remetente") };
		msg.setRecipients(Message.RecipientType.TO, address);  
		msg.setSubject("Titulo_do_email");  

		// create and fill the first message part  
		MimeBodyPart bp1 = new MimeBodyPart();  
		bp1.setText("Conteudo_do_email");  
		// create the second message part  
		MimeBodyPart bp2 = new MimeBodyPart();  

		// attach the file to the message  
		//m_xqLog.logInformation("[ContentType]: "+prt.getContentType());  
		//FileDataSource fds = new FileDataSource(prt.getDataHandler()));  
		//DataHandler sh = new DataHandler((DataSource) prt.getDataHandler());  
		DataSource ds = new FileDataSource("nome_da_pasta/nome_da_imagem.jpg");  
		bp2.setDataHandler( new DataHandler(ds));  
		//bp2.setContent(prt.getContent().toString().getBytes(), prt.getContentType());  
		//bp2.setDataHandler(new DataHandler(new ByteArrayDataSource(prt.getContent().toString(),"application/pdf")));  
		bp2.setFileName("nome da sua imagem.jpg");  
		// create the Multipart and add its parts to it  
		Multipart mp = new MimeMultipart();  
		mp.addBodyPart(bp1);  
		mp.addBodyPart(bp2);  

		// add the Multipart to the message  
		msg.setContent(mp);  

		// set the Date: header  
		msg.setSentDate(new Date());  

		// send the message 
		int i;
		
			Transport.send(msg);	
			System.out.println("Email enviado com sucesso!"); 	
		  
	}  

	public static void main (String args[]) throws AddressException, MessagingException  
	{  
		Email rodar = new Email();
		rodar.sendSimpleMail();
	}

}
Criado 11 de abril de 2010
Ultima resposta 11 de abr. de 2010
Respostas 1
Participantes 2