PessoALL !!!
Existe uma demo no pacote javamail chamado sendfile.java, que envia arquivo
atachado por e-mail. Este programa roda em console.
Preciso fazer um servlet que envie uma arquivo atachado por e-mail.
O que eu fiz ent�o:
aproveitando o sendfile.java, que roda em console eu adaptei para um
servlet. Mas est� compilando normal, mas n�o est� enviado nada.
Abaixo o sendfile.java este roda em console tudo OK.
/*
* @(#)sendfile.java 1.8 00/05/24
*/
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class sendfile {
public static void main(String[] args) {
if (args.length != 5) {
System.out.println("usage: java sendfile <to> <from> <smtp> <file>
true|false");
System.exit(1);
}
String to = args[0];
String from = args[1];
String host = args[2];
String filename = args[3];
boolean debug = Boolean.valueOf(args[4]).booleanValue();
String msgText1 = "Sending a file.\n";
String subject = "Sending a file";
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
}
este arquivo se chama sendfile e � uma exemplo de biblioteca javamail.
Eu adaptei este programa acima em forma de servlet e ficou desta maneira:
import java.util.*;
import java.io.*;
import java.io.PrintStream;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class sendfile extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
boolean b_sucesso = true;
// declara o dispositivo de sa�da.
PrintWriter output;
// define o tipo de sa�da.
resp.setContentType( "text/html" );
// define no dispositivo de sa�da, o tipo de sa�da.
output = resp.getWriter();
// enumera os par�metros.
Enumeration s_Parametros = req.getParameterNames();
// Get Parameter
String to = req.getParameter("fld_to");
String from = req.getParameter("fld_from");
String host = req.getParameter("fld_smtphost");
String filename = req.getParameter("fld_filename");
// boolean debug =
Boolean.valueOf(req.getParameter("fld_filename_yn")).booleanValue();
boolean debug = true;
String msgText1 = "Sending a file.\n";
String subject = "Sending a file";
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props,
null);
session.setDebug(debug);
try {
// criando arquivo de log
PrintStream saidaPadraoEErro = new PrintStream( new
FileOutputStream("usr/local/web/vesper/servlet/sendfile.log", true), true );
System.setErr(saidaPadraoEErro);
System.setOut(saidaPadraoEErro);
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
}
/*
catch (FileNotFoundException fnfExc) {
System.out.println("InscricaoSaServlet01 -N�o
conseguiu criar arquivo de log");
System.exit(0);
} // catch
*/
catch (MessagingException mex) {
b_sucesso = false;
file://mex.printStackTrace();
System.out.println(mex);
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
//ex.printStackTrace();
System.out.println(ex);
} // fecha catch (MessagingException mex)
} //fecha Try
sendResponse( b_sucesso, output );
} // public void doPost
public void sendResponse( boolean p_b_sucesso,
PrintWriter p_output )
{
p_output.println( "<html>\n" );
_output.println( "<head><title>Vesper</title></head>\n" );
if (p_b_sucesso == true)
p_output.println( "Formulario enviado com sucesso
!!!\n" );
else
p_output.println( "Formulario com ERRO !!!\n" );
p_output.println( "</body>\n" );
p_output.println( "</html>" );
} // sendResponse
} // public class sendfile extends HttpServlet
Tamb�m est� logo abaixo o html que d� o post p/ o servlet:
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="http://www.host.com.br/servlet/t/sendfile">
<table>
<tr>
<td colspan="3" align="left"><input type="text" name="fld_smtphost"
size="72" value="smtp.bol..com.br">
</tr>
<tr>
<td colspan="3" align="left"><input type="text" name="fld_from" size="72"
value="[EMAIL PROTECTED]">
</tr>
<tr>
<td colspan="3" align="left"><input type="text" name="fld_to" size="72"
value="[EMAIL PROTECTED]"">
</tr>
<tr>
<td colspan="3" align="left"><input type="text" name="fld_filename"
size="72" value="c:\teste.txt">
</tr>
<tr>
<td colspan="3" align="left"><input type="text" name="fld_filename_yn"
size="72" value="true">
</tr>
<tr>
<td colspan="4" align="center"><input type="submit" value="Enviar"
name="bt_submit">
</tr>
</table>
</form>
<br>
</body>
</html>
Quem puder me ajudar, eu agrade�o.
Muito obrigado.
[] 's
Kleber Rodrigo de Carvalho
Software Engineer
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
------------------------------ LISTA SOUJAVA ----------------------------
http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
d�vidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------