wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes use Linux sendmail from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "use Linux sendmail from Java" Watch "use Linux sendmail from Java" New topic
Author

use Linux sendmail from Java

Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
Hi everybody. I'm brand new to Java and I would like to send an email using Linux Sendmail, however; I cannot find any references that tell how to use Sendmail at all, much less how to access it from Java. Does anyone know of any?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You could just use the JavaMail API (http://java.sun.com/products/javamail/index.html) which will use sendmail on your behalf.


[Jess in Action][AskingGoodQuestions]
Gustavo Adolpho Bonesso
Ranch Hand

Joined: May 11, 2002
Posts: 103
One alternative way is to deal directly with the Socket on the Mail Server... The class listed above do it... The comments are in Portuguese, unfortunatelly (my born language...). I Hope it helps...
import java.io.*;
import java.net.*;
import java.util.*;
/**
* Classe para modelagem de e-mail
*
* @author Gustavo Adolpho Bonesso - 29/Ago/2003
*/
public class SendMail {
private boolean online = false;
private PrintWriter out = null;
private BufferedReader in = null;
private Socket socket = null;
private Vector vRecipient = new Vector(1);
private String from = null;
private String subject = null;
private String bgColor = null;
private StringBuffer message = null;
private boolean isHtml = false;
/**
* Construtor
* @param host IP ou Host do servidor SMTP
* @param port porta de comunica��o para o servidor SMTP - valor padr�o: 25
*/
public SendMail(String host, int port) {
try {
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
online = true;
} catch(Exception exception) {
System.err.println("Problem: " + exception.toString() );
}
this.from = "";
this.subject = "";
this.isHtml = false;
this.bgColor = "white";
this.message = new StringBuffer();
}
/**
* Construtor padr�o
*/
public SendMail(){
this.from = "";
this.subject = "";
this.isHtml = false;
this.bgColor = "white";
this.message = new StringBuffer();
}
/**
* Metodo que estabelece a conex�o com o servidor de e-mail
*/
public void connect(String host, int port) {
try {
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
online = true;
} catch(Exception exception) {
System.err.println("Problem: " + exception.toString() );
}
}
/**
* M�todo que fecha o socket
*/
public void close() {
try {
socket.close();
} catch(IOException e) {
System.out.println(e.getMessage());
}
out.close();
try {
in.close();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
/**
* M�todo para envio de mensagens em "stream" (uma linha por vez)
* @param strOUT Mensagem a enviar
* @param getResponse diz se o m�todo deve ou n�o devolver a resposta do servidor
*/
public void send(String strOUT, boolean getResponse) throws IOException {
if(strOUT!=null) {
out.println(strOUT);
out.flush();
}
if(getResponse) {
String strIN;
if((strIN = in.readLine()) != null)
System.out.println(strIN);
}
}
/**
* M�todo Sobrecarregado - M�todo para envio de mensagens em "stream" (uma linha por vez)
* @param strOUT Mensagem a enviar
*/
public void send(String strOUT) throws IOException {
this.send(strOUT, true);
}
/**
* M�todo para setar o destinat�rio da mensagem
* @param recipient destinat�rio da mensagem
*/
public void to(String recipient){
vRecipient.addElement(recipient);
}
/**
* M�todo para setar o remetente da mensagem
* @param from remetente da mensagem
*/
public void from (String from){
this.from = from;
}
/**
* M�todo para setar o assunto da mensagem
* @param subject assunto da mensagem
*/
public void subject (String subject){
this.subject = subject;
}
/**
* M�todo para incluir uma linha ao corpo da mensagem
* @param msg mensagem a incluir
*/
public void message (String msg){
if (isHtml){
this.message.append(msg + "<br>");
} else {
this.message.append(msg + "\n");
}
}
public void message (String msg, Font font){
this.message.append(font.getHtml(msg));
}
/**
* M�todo que indica que o e-mail ser� formatado em HTML
*/
public void setHtml(){
this.isHtml = true;
}
public void setBgColor(String color){
this.bgColor = color;
}
/**
* M�todo para iniciar o processo de envio da mensagem
*/
public void go(){
try {
java.util.Date now = new java.util.Date();
String hostName = InetAddress.getLocalHost().getHostName();
this.send(null);
this.send("HELO " + hostName);
this.send("MAIL FROM: " + from);
for (int i = 0; i < vRecipient.size(); i++){
this.send("RCPT TO: " + vRecipient.elementAt(i));
}
this.send("DATA");
this.send("Date: " + now.toString(), false);
this.send("From: " + this.from, false);
this.send("Subject: " + this.subject, false);
for (int i = 0; i < vRecipient.size(); i++){
this.send("To: " + vRecipient.elementAt(i), false);
}
if (this.isHtml){
this.send("Content-type: text/html", false);
}
this.send("", false);
if (this.isHtml){
this.send("<html><body bgcolor=\"" + this.bgColor + "\">", false);
}
this.send(this.message.toString(), false);
if (this.isHtml){
this.send("</body></html>", false);
}
this.send("\n.\n");
this.send("QUIT");
this.close();
} catch (IOException e){
System.out.println(e.getMessage());
}
}
}


Gustavo Adolpho Bonesso<br />SCJP1.4
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Here is a wrapper for Java mail to send a simple e-mail:

You'll probably need the following imports to get this to work:

Look at the JavaMail API to see how to do attachments, etc.


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Checkout this tutorial
http://www-106.ibm.com/developerworks/edu/j-dw-javamail-i.html
[ October 14, 2003: Message edited by: Pradeep Bhat ]

Groovy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use Linux sendmail from Java