Peguei um exemplo na net sobre JavaMail. Coloquei minhas configurações no Main e setei a variavel needsAuth = true (na classe MailServer), pois meu servidor de email precisa de autenticacão. No entanto, ao rodar o programa dá “Acesso negado”. Falta configurar alguma coisa alem do que ja’ configurei ?
MailManager (Contém a classe main)
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class MailManager {
/** Creates a new instance of MailManager */
public MailManager() {
}
public void send(MailServer server, MailMessage msg) throws Exception {
try {
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", server.getMailServer());
SimpleAuth auth = null;
if (server.isNeedsAuth()) {
// [#] No authorization needed for this mail server
mailProps.put("mail.smtp.auth", "false");
} else {
// [#] Authorization needed for this mail server
auth = new SimpleAuth(server.getUsername(), server.getPassword());
mailProps.put("mail.smtp.auth", "true");
mailProps.put("mail.user", auth.username);
mailProps.put("mail.from", msg.getFrom());
mailProps.put("mail.to", msg.getTo());
}
Session mailSession = Session.getInstance(mailProps, auth);
mailSession.setDebug(false);
Message email = new MimeMessage(mailSession);
email.setRecipients( Message.RecipientType.TO, InternetAddress.parse(msg.getTo()) );
if (!msg.getCc().trim().equals("")) {
email.setRecipients( Message.RecipientType.CC, InternetAddress.parse(msg.getCc()) );
}
if (!msg.getBcc().trim().equals("")) {
email.setRecipients( Message.RecipientType.BCC, InternetAddress.parse(msg.getBcc()) );
}
email.setFrom( new InternetAddress(msg.getFrom()));
email.setSubject(msg.getSubject());
email.setContent( msg.getText(), "text/plain" );
Transport.send( email );
} catch (Exception e) {
System.out.println(" [E] ERROR: "+e);
e.printStackTrace(System.out);
throw e;
}
System.out.println(" [#] Email successfully sent");
return;
}
public void send(MailServer server, String to, String cc, String bcc, String from, String subject, String text) throws Exception {
MailMessage msg = new MailMessage(to,cc,bcc,from,subject,text);
this.send(server, msg);
return;
}
public static void main(String args[]) {
MailServer server = new MailServer("smtp.localohost.com.br","login","senha",true);
MailManager m = new MailManager();
try {
m.send(server,"[email removido]","","","[email removido]","Esse e um email de teste","Corpo do email");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
class SimpleAuth extends Authenticator {
public String username = null;
public String password = null;
public SimpleAuth(String user, String pwd) {
username = user;
password = pwd;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
}
}
MailServer
public class MailServer {
private String mailServer = null;
private String username = null;
private String password = null;
private boolean needsAuth = true;
/** Creates a new instance of MailServer */
public MailServer() {
}
/** Creates a new instance of MailServer */
public MailServer(String mailServer, String username, String password, boolean needsAuth) {
this.mailServer=mailServer;
this.username=username;
this.password=password;
this.needsAuth=needsAuth;
}
/** Getter for property mailServer.
* @return Value of property mailServer.
*
*/
public java.lang.String getMailServer() {
return mailServer;
}
/** Setter for property mailServer.
* @param mailServer New value of property mailServer.
*
*/
public void setMailServer(java.lang.String mailServer) {
this.mailServer = mailServer;
}
/** Getter for property needsAuth.
* @return Value of property needsAuth.
*
*/
public boolean isNeedsAuth() {
return needsAuth;
}
/** Setter for property needsAuth.
* @param needsAuth New value of property needsAuth.
*
*/
public void setNeedsAuth(boolean needsAuth) {
this.needsAuth = needsAuth;
}
/** Getter for property password.
* @return Value of property password.
*
*/
public java.lang.String getPassword() {
return password;
}
/** Setter for property password.
* @param password New value of property password.
*
*/
public void setPassword(java.lang.String password) {
this.password = password;
}
/** Getter for property username.
* @return Value of property username.
*
*/
public java.lang.String getUsername() {
return username;
}
/** Setter for property username.
* @param username New value of property username.
*
*/
public void setUsername(java.lang.String username) {
this.username = username;
}
}
MailMessage
public class MailMessage {
private String to;
private String cc;
private String bcc;
private String from;
private String subject;
private String text;
/** Creates a new instance of MailMessage */
public MailMessage() {
}
/** Creates a new instance of MailMessage */
public MailMessage(String to,String cc,String bcc,String from,String subject,String text) {
this.to=to;
this.cc=cc;
this.bcc=bcc;
this.from=from;
this.subject=subject;
this.text=text;
}
/** Getter for property bcc.
* @return Value of property bcc.
*
*/
public java.lang.String getBcc() {
return bcc;
}
/** Setter for property bcc.
* @param bcc New value of property bcc.
*
*/
public void setBcc(java.lang.String bcc) {
this.bcc = bcc;
}
/** Getter for property cc.
* @return Value of property cc.
*
*/
public java.lang.String getCc() {
return cc;
}
/** Setter for property cc.
* @param cc New value of property cc.
*
*/
public void setCc(java.lang.String cc) {
this.cc = cc;
}
/** Getter for property from.
* @return Value of property from.
*
*/
public java.lang.String getFrom() {
return from;
}
/** Setter for property from.
* @param from New value of property from.
*
*/
public void setFrom(java.lang.String from) {
this.from = from;
}
/** Getter for property subject.
* @return Value of property subject.
*
*/
public java.lang.String getSubject() {
return subject;
}
/** Setter for property subject.
* @param subject New value of property subject.
*
*/
public void setSubject(java.lang.String subject) {
this.subject = subject;
}
/** Getter for property text.
* @return Value of property text.
*
*/
public java.lang.String getText() {
return text;
}
/** Setter for property text.
* @param text New value of property text.
*
*/
public void setText(java.lang.String text) {
this.text = text;
}
/** Getter for property to.
* @return Value of property to.
*
*/
public java.lang.String getTo() {
return to;
}
/** Setter for property to.
* @param to New value of property to.
*
*/
public void setTo(java.lang.String to) {
this.to = to;
}
}