Ich habe ein Servlet für ein Ticketsystem geschrieben.
Möchte jetzt die Möglichkeit über SMTP eine E-Mail
zu verschicken einbauen. Auch dies habe ich hinbekommen, aber
ich weiß nicht wie man Dateien an die Mail über Java
anhängt.
Sources sind immer was schönes.
Ich kann dir übrigens http://www.jguru.com empfehlen.
Hoffe, es funktioniert, hab etliche Zeilen drumherum weglöschen müssen.
//
// EMAIL ORIGINAL PICTURE TO CUSTOMER
//
String to = "[email protected]";
String from = "irgendeine.mail.adresse@irgendwo";
String host = "der.mail.server";
String filename = originalFilename;
boolean debug = true;
String msgText1 = "Das Photo zum Event!\n" +
"Are you ready for MINI, Now?\n\n"+
"Aufgeweckt? Alles über den neuen MINI"+
" auf http://www.mini.at\n\n"+
"THE MINI COMMUNITY";
String subject = "MINI COMES TO TOWN!";
// create some properties and get the default SessionProperties
props = System.getProperties();
props.put("mail.smtp.host", host);
Session mySession = Session.getDefaultInstance(props, null);
mySession.setDebug(debug);
try {
// create a message
MimeMessage msg = new MimeMessage(mySession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
out.println("general mail message creation... done.
");
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
out.println("first mail message part... done.
");
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
out.println("second mail message part... done.
");
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
out.println("file attachement... done.
");
// 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);
out.println("multipart... done.
");
// set the Date: header
msg.setSentDate(new java.util.Date());
// send the message
Transport.send(msg);
out.println("sending mail to "+paramEMAIL+"... done.
");
} catch (MessagingException mex) {
out.println("Exception du nuss: "+mex.toString());
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
//
// THE HAPPY END
//
out.println("finished.");
%\>
Die Klassen javax.mail.*, javax.activation.* und javax.mail.internet.* bekommst du bei http://java.sun.com