Fala pessoal. Não estou conseguindo enviar e-mails de minhas conta do google ou yahoo para eu mesmo. No meu caso estou tentando usar javamail e estive pesquisando pacas. Geralmente alguns dizem que conseguem e mostram o código e posteriormente outros pegam este código e também não estão conseguindo como eu.
Segue abaixo o código exemplo que estou testando:
/*
* Created on 14/09/2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author gaucho
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class NewTestGmail {
private static final String SMTP_HOST_NAME = "gmail-smtp.l.google.com";
private static final String SMTP_AUTH_USER = "[email removido]";
private static final String SMTP_AUTH_PWD = "minhasenha";
private static final String emailMsgTxt = "Please visit my project at ";
private static final String emailSubjectTxt = "Order Confirmation Subject";
private static final String emailFromAddress = "[email removido]";
// Add List of Email address to who email needs to be sent to
private static final String[] emailList = { "[email removido]" };
public static void main(String args[]) throws Exception {
NewTestGmail smtpMailSender = new NewTestGmail();
smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt,
emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
public void postMail(String recipients[], String subject, String message,
String from) throws MessagingException {
boolean debug = false;
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//Set the host smtp address
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
/**
* SimpleAuthenticator is used to do simple authentication when the SMTP
* server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
Este código peguei deste link. Também tentei outros e-mails como walla. O curioso é que somente consegui um e-mail da bol. Alguém já passou por isso. Ah sim, segue abaixo o erro que tanto me implica.
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first 38sm10234361agdat javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at tce.util.emailexcecao.NewTestGmail.postMail(NewTestGmail.java:84)
at tce.util.emailexcecao.NewTestGmail.main(NewTestGmail.java:45)
