PDA

View Full Version : Gửi mail bằng JSP!



cuongdh
31-01-2007, 13:38
Hi cả nhà!

Các bác giúp E lập trình đoạn script gửi mail bằng Jsp với!

Thanks All!

langbian
01-02-2007, 18:30
Bác dùng thư viện JavaMail de gui mail. Đoạn code trong document của nó :

try {
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
// 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("Mail from : " + subject);

// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
mp.addBodyPart(mbp1);

// attach the file to the message
if(!filename.equals("")) {
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
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) {}

Copy thư viện (file jar) vào WEB-INF/lib (nhớ thêm activation.jar)

truongduygp
09-10-2011, 15:10
ADadsDdADadADasdsdsd