<%@ page contentType=“text/html; charset=iso-8859-1” language=“java” import=“java.sql.,java.io . , sun.net.smtp.SmtpClient;” errorPage="" %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd ”>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=“Content-Type” content=“text/html; charset=iso-8859-1”>
</head>
<body>
<% String from="sender@somewhere.com ";
String to="recipient@somewhere.com "; %>
Sending mail from <%=from%> to <%=to%>
<%
try{SmtpClient client = new SmtpClient(“mail.somewhere.com ”);
client.from(from);
client.to(to);
PrintStream msg = client.startMessage();
msg.println("to:"+to);
msg.println("Subject:Sending e-mail from Zamples");
msg.print("\r\n");
msg.println("This email was sent from a JSP built by Zamples.");
msg.println("See [url]http://zamples.com[/url] for more information.");
client.closeServer();
} catch(IOException e) {
out.println("error" + e);
} %>
</body>
</html>
anexos:
http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#InstallJAF
import java.io.IOException;
import java.io.PrintStream;
import sun.net.smtp.SmtpClient;
public class Email {
public void sendEmail() {
String from = "dcamargo@sicredi.com.br";
String to = "dcamargo@sicredi.com.br";
try {
SmtpClient client = new SmtpClient("10.1.0.38");
client.from(from);
client.to(to);
PrintStream msg = client.startMessage();
msg.println("to:" + to);
msg.println("Subject:Sending e-mail from Zamples");
msg.print("\r\n");
msg.println("This email was sent from a JSP built by Zamples.");
msg.println("See [url]http://zamples.com[/url] for more information.");
client.closeServer();
} catch (IOException e) {
System.out.println("error" + e);
}
}
}
exemplo:
public class ExemploEmail
{
public static void main(String[] args) {
Email MyEmail = new Email();
MyEmail.setOrigem("correa@hotmail.com ");
MyEmail.setDestino(“correa@bol.com.br”);
MyEmail.setAssunto(“teste de e-mail”);
MyEmail.setMensagem(“Testando …\r\n Testando … \r\n Testando …”);
MyEmail.setSmtp(“smtp.bol.com.br”);
MyEmail.sendEmail();
System.exit(0);
}
}
A classe e-mail ficou assim :
import java.io.IOException;
import java.io.PrintStream;
import sun.net.smtp.SmtpClient;
public class Email {
private String origem = null;
private String destino = null;
private String smtp = null;
private String assunto = null;
private String mensagem = null;
public void sendEmail() {
try {
SmtpClient client = new SmtpClient(smtp);
client.from(getOrigem());
client.to(getDestino());
PrintStream msg = client.startMessage();
msg.println(“to:” + getOrigem());
msg.println(“Subject:”+getAssunto());
msg.print("\r\n");
msg.println(getMensagem());
client.closeServer();
} catch (IOException e) {
System.out.println(“error” + e);
}
}
public String getOrigem(){
return origem;
}
public void setOrigem(String valor) {
origem = valor;
}
public String getDestino(){
return destino;
}
public void setDestino(String valor) {
destino = valor;
}
public String getAssunto(){
return assunto;
}
public void setAssunto(String valor) {
assunto = valor;
}
public String getMensagem(){
return mensagem;
}
public void setMensagem(String valor) {
mensagem = valor;
}
public String getSmtp(){
return smtp;
}
public void setSmtp(String valor) {
smtp = valor;
}
}