SendMail

Propaganda
/*
* SendMail.java
*
* Created on 17 de Novembro de 2003, 11:28
*/
package sistema;
/* ******************** SendMail.java ******************** */
/*
* SendMail.java
*
* Created on November 15, 2003, 4:46 PM
*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* @author: HEngholmJr
*
* @javamail: http://java.sun.com/products/javamail/
*/
public class SendMail {
// embedded image
private final String strImage = "C:\\Cara_Vermelha.gif";
// private final String strImage = "C:\\Msn\\alien01.gif";
private String strHost;
private String strFrom;
private String strTo;
private String strCC;
private String strSubject;
private String strAttachment;
private String mensagemErro;
public void enviarEmail(String emailDest, String nomeDest,
String emailRemet, String nomeRemet, String assunto, String corpo)
throws Exception {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.webrhnet.com.br");
props.put("mail.smtp.auth", "true");
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "Clau123pam");
}};
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailRemet, nomeRemet));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(emailDest, nomeDest));
message.setSubject(assunto);
message.setContent(corpo, "text/plain");
Transport.send(message);
}
/** Creates a new instance of SendMail */
public SendMail(String strHost, String strFrom, String strTo, String
strCC, String strSubject, String strAttachment)
{
this.strHost = strHost;
this.strFrom = strFrom;
this.strTo = strTo;
this.strCC = strCC;
this.strSubject = strSubject;
this.strAttachment = strAttachment;
mensagemErro = "";
}
public SendMail(){
strHost = "186.220.234.171";
strFrom = "[email protected]";
strTo = "[email protected]";
strCC = "[email protected]";
strSubject = "Hello from JavaMail";
strAttachment = "imagens\\Cara_Vermelha.gif";
}
public void text() throws Exception
{
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", strHost);
//Autenticacao
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "Clau123pam");
}};
// Get session
Session session = Session.getInstance(props, auth);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(strFrom));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(strTo));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(strCC));
message.setSubject(strSubject);
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setText("Text Mail by JavaMail");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(strAttachment);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setFileName(strAttachment);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send( message );
}
public void html()
throws Exception
{
try{
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", strHost);
//Autenticacao
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "Clau123pam");
}};
// Get session
Session session = Session.getInstance(props, auth);
// Define message
MimeMessage message = new MimeMessage(session);
message.setSubject(strSubject);
message.setFrom(new InternetAddress(strFrom));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(strTo));
message.addRecipient(Message.RecipientType.CC, new
InternetAddress(strCC));
// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
// Set the HTML content
String htmlText = "<h1>Texto de e-mail de teste de envio</h1>" + "<hr>";
// use a cid URL. The content-id will need to be specified for the image later.
//htmlText = htmlText + "<img src=\"cid:idimage\">" + "<hr>";
htmlText = htmlText + "<h1>Voce foi selecionado pelo RH para participar do processo de
avaliao</h1>";
htmlText = htmlText + "<table class=tab1 border=1 width='100%' bordercolor='#990000'
bordercolordark=white cellpadding=3 cellspacing=0 rules=rows frame=below >";
htmlText = htmlText + "<tr><td class=ttltab2 width='55%'><font color='#0000FF' size='6'
face='Verdana, Arial, Helvetica, sans-serif'>Nome</td><td class=ttltab2 width='45%'><font
color='#0000FF' size='6' face='Verdana, Arial, Helvetica, sans-serif'>Opcoes</td></tr>";
htmlText = htmlText + "<tr><td width='55%'>Claudia Schimalesky</td><td class=ttltab2
width='45%'>Iniciar</td></tr></table>";
// Set the content of the body part
messageBodyPart.setContent(htmlText, "text/html");
// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
// Add body part to multipart
multipart.addBodyPart(messageBodyPart);
// Create part for the image
messageBodyPart = new MimeBodyPart();
// Fetch the image to associate to part
DataSource dsImage = new FileDataSource(strImage);
messageBodyPart.setDataHandler(new DataHandler(dsImage));
// Add a header to connect to the HTML
messageBodyPart.setHeader("Content-ID","<idimage>");
// Add part to multi-part
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
message.setContent(multipart);
// Send message
Transport.send(message);
}
catch(Exception ex){
mensagemErro = ex.toString();
}
}
public String getMensagemErro(){
return mensagemErro;
}
}
/* ******************** ************* ******************** */
Download