Hello Friends, I want to send an html page without containing any form elements as email to the users with different id's retrieved from the database in servlets using local host without using any web server and also with using Java Web Server 2.0 using smtp or java mail package. Would anyone help me in this matter. Thanks for your response in advance, by, ruby.
Dmitriy Pavlyuk
Ranch Hand
Joined: Mar 25, 2001
Posts: 33
posted
0
This is my class which sends email with or w/o attachment. If i send woth attachment? i use class File. package pm.util; import java.util.Collection; import java.util.ArrayList; import java.sql.SQLException; import java.util.Iterator; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class MailSender{
public static final String MAILHOST = "123box.co.uk"; public static final String FROM = "dmitriy@123box.co.uk"; public static final Session SESSION; static { java.util.Properties props = System.getProperties(); props.put("mail.smtp.host", MAILHOST); SESSION = Session.getInstance(props, null); }
public static void send(String to, String subject, String text, String memoType) { try { MimeMessage message = new MimeMessage(SESSION); message.setFrom(new InternetAddress(FROM)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setText(text); message.setHeader("Content-Type", memoType + "; charset=\"something\""); Transport.send(message); } catch (Exception e) { } }
public static void send(String to, String subject, String text, pm.util.File file) { try { MimeMessage message = new MimeMessage(SESSION); message.setFrom(new InternetAddress(FROM)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); Multipart multipart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(text); multipart.addBodyPart(messageBodyPart); MimeBodyPart fileBodyPart = new MimeBodyPart(); fileBodyPart.setFileName(file.getName()); fileBodyPart.setHeader("Content-Type",file.getContentType()); DataSource ds = new pm.util.FileDataSource(file); fileBodyPart.setDataHandler(new DataHandler(ds)); multipart.addBodyPart(fileBodyPart);