Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

use Linux sendmail from Java

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could just use the JavaMail API (http://java.sun.com/products/javamail/index.html) which will use sendmail on your behalf.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
}
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checkout this tutorial
http://www-106.ibm.com/developerworks/edu/j-dw-javamail-i.html
[ October 14, 2003: Message edited by: Pradeep Bhat ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic