wood burning stoves 2.0*
The moose likes Servlets and the fly likes Need help for sending emails using java servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need help for sending emails using java servlets" Watch "Need help for sending emails using java servlets" New topic
Author

Need help for sending emails using java servlets

ruby mohan
Greenhorn

Joined: Aug 02, 2001
Posts: 4
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
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);

message.setContent(multipart);

Transport.send(message);
} catch (Exception e) {
}
}

}

Attacment file
package pm.util;
public class File {
private String name;
private byte[] data;

public File() {
name = "";
data = null;
}

public File(String _name, byte[] _data) {
name = _name;
data = _data;
}

public String getName() {
return name;
}

public byte[] getData() {
return data;
}

public void setName(String _name) {
name = _name;
}

public void setData(byte[] _data) {
data = _data;
}

public String getExtension() {
return name.substring(name.lastIndexOf('.')+1);
}

public String getContentType() {
String ext = getExtension();
if (ext.startsWith("htm")) {
return "text/html";
} else if (ext.startsWith("jp")) {
return "image/jpeg";
} else if (ext.startsWith("gif")) {
return "image/gif";
} else if (ext.startsWith("doc")) {
return "application/msword";
} else {
return "multipart/byteranges";
}
}
}

package pm.util;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.activation.DataSource;
public class FileDataSource implements DataSource {
private File file;

public FileDataSource(File _file) {
this.file = _file;
}

public InputStream getInputStream() throws IOException {
if (file.getData() == null)
throw new IOException("no data");
return new ByteArrayInputStream(file.getData());
}

public OutputStream getOutputStream() throws IOException {
throw new IOException("This class don't support this method");
}

public String getContentType() {
return file.getContentType();
}

public String getName() {
return file.getName();
}
}

[This message has been edited by Dmitriy Pavlyuk (edited August 03, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help for sending emails using java servlets