It is an example of sending bulk mails with
attachment.
You have to write the uploaded file into a temporary
directory.
See the attachments. I hope the example could be
useful.
-Caroline
--- Daxin Zuo <[EMAIL PROTECTED]> wrote:
>
> Hi,
> Please help. Any sugestion is welcome. In my web
> page, users send email
> with attachment, so upload is related. I can provide
> the file in either a
> byte[] array, or in an inputstream. I try to send
> the attachement with
> JavaMail(I know there is email api in Tomcat-Common.
> But I have no a good
> example).
>
> --- my code in a function in a servlet ---
> DiskFileUpload upload = new DiskFileUpload();
> List items = upload.parseRequest(request);
> Iterator itr = items.iterator();
> item = (FileItem) itr.next();
> .....
> //----- now it is a attachement. ----
> InputStream istrm= item.getInputStream();
> ... ....
> MimeBodyPart messageBodyPart = new MimeBodyPart();
> messageBodyPart.setDisposition(Part.INLINE);
> messageBodyPart.setContent(strBodyText,
> "text/plain");
> MimeMultipart multipart = new MimeMultipart();
> multipart.addBodyPart(messageBodyPart);
> messageBodyPart = new MimeBodyPart(istrm);
>
messageBodyPart.setDisposition(messageBodyPart.ATTACHMENT);
> messageBodyPart.addHeader("Content-Type",strMime);
> //strMime is correct
> messageBodyPart.setFileName(fileName);
> multipart.addBodyPart(messageBodyPart);
> ....
>
>
> NO metter what file it is, the attachement received
> is ATT00211.txt.
> I the file is a text file, the contents are correct.
> if the file is a small zip file, it adds the lines
> as following in attached
> file:
> Content-Type: application/zip; name=idmeta.zip
> Content-Disposition: attachment; filename=idmeta.zip
> Content-Transfer-Encoding: 7bit
>
> What's wrong? Please forward instruction.
>
>
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
>
>
__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mailpackage org.dhsinfo.message;
// import the JavaMail packages
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
// import misc classes that we need
import java.util.Properties;
import java.util.Collection;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
public final class SendBatchMails extends Action
{
private final String PROPFILE = "resources/smtpServer.properties";
private Properties smtpProp = null;
public SendBatchMails()
{
try
{
smtpProp = new Properties();
smtpProp.load ( getClass().getClassLoader().getResourceAsStream(
PROPFILE ) );
}
catch( IOException oEx )
{
System.out.println("Unable to load the Properties " + PROPFILE + oEx );
}
}
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws java.lang.Exception
{
SelectRecipientsForm srf = ( SelectRecipientsForm )form;
// get the message parameters from the HTML page
String from = srf.getSender();
/* if ( from == null ) from = mailOption.defaultEmailFrom; */
String[] to = srf.getSelectedEmailAddresses();
String subject = srf.getMessageTopic();
String text = srf.getMessageBody();
FormFile file = srf.getTheFile();
String fileName = file.getFileName();
Address[] toAddress = new Address[ to.length ];
for ( int i=0; i < to.length; i++ )
{
toAddress[i] = new InternetAddress( to[i] );
}
CreateTemporaryFile ctf = new CreateTemporaryFile();
String filePath = ctf.writeToFile( file, fileName );
//destroy the temporary file created
file.destroy();
// set the SMTP host property value
String smtpServer = smtpProp.getProperty( "smtpServer" );
Properties props = System.getProperties();
props.put( "mail.smtp.host", smtpServer );
Transport trans = null;
PrintWriter out = response.getWriter();
response.setContentType( "text/html" );
try
{
// create a JavaMail session
Session session = Session.getDefaultInstance( props, null );
// To watch the mail commands go by to the mail server
session.setDebug(true);
trans = session.getTransport( "smtp" );
trans.connect();
// create a new MIME message
Message message = new MimeMessage( session );
// set the from address
Address fromAddress = new InternetAddress( from );
message.setFrom( fromAddress );
// set the subject
message.setSubject( subject );
message.setRecipients( Message.RecipientType.TO, toAddress );
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText( text );
Multipart multipart = new MimeMultipart();
multipart.addBodyPart( messageBodyPart );
// Create the message part again
messageBodyPart = new MimeBodyPart();
// Fill the name and content of the attachment
DataSource source = new FileDataSource( filePath );
messageBodyPart.setDataHandler(new DataHandler( source ));
messageBodyPart.setFileName( fileName );
multipart.addBodyPart( messageBodyPart );
// Put parts in message
message.setContent( multipart );
message.saveChanges();
// send the message
trans.sendMessage( message, message.getAllRecipients() );
}
catch ( AddressException e )
{
out.println( "Invalid e-mail address.<br>" + e.getMessage() );
}
catch ( SendFailedException e )
{
out.println( "Send failed.<br>" + e.getMessage() );
}
catch ( MessagingException e )
{
out.println( "Unexpected error.<br>" + e.getMessage() );
}
finally
{
try
{
if ( trans != null ) trans.close();
}
catch (MessagingException ex ) {}
}
// Return success
return ( mapping.findForward( "success" ) );
}
} // End SendBatchMails
package org.dhsinfo.message;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts.upload.FormFile;
class CreateTemporaryFile
{
String tmpdir = System.getProperty( "java.io.tmpdir" );
public String writeToFile( FormFile file, String fileName )
{
String filePath = tmpdir + fileName;
try
{
// retrieve the file data
InputStream stream = file.getInputStream();
// write to the file specified
OutputStream bos = new FileOutputStream( filePath );
int bytesRead = 0;
byte[] buffer = new byte[ 8192 ];
while ( ( bytesRead = stream.read( buffer, 0, 8192 ) ) != -1 )
{
bos.write( buffer, 0, bytesRead );
}
bos.close();
//close the stream
stream.close();
}
catch ( FileNotFoundException fnfe )
{
System.out.println( "The file was not found" + fnfe );
}
catch ( IOException ioe )
{
System.out.println( "Errors occurred during retrieving and writing
file data!" + ioe );
}
return filePath;
}
} // end of the CreateTemporaryFile.java
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]