我在发送电子邮件时遇到了一些麻烦。
主要代码
public static void main(String[] args) throws EmailException, IOException {
ConfiguracaoEmail emailConfig = new ConfiguracaoEmail(new Filial("matriz", true));
emailConfig.setServidor("smtp.gmail.com");
emailConfig.setRemetente("emailFrom@gmail.com");
emailConfig.setTitulo("Teste");
emailConfig.setCodificacao("utf-8");
emailConfig.setAutenticacao("emailFrom@gmail.com");
emailConfig.setSenha("senhaEmail");
emailConfig.setPortaSMTP(465);
emailConfig.setTLS(true);
List<String> emails = new ArrayList<String>();
emails.add("emailTo@gmail.com");
try {
SendMail mail = new SendMail("Teste", " - envio email", emails, emailConfig);
mail.start();
} catch (EmailException e) {
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
}
}
ConfiguracaoEmail类只是辅助传递信息。
类SendEmail
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class SendMail extends Thread {
private HtmlEmail email;
public SendMail(String subject, String message, List<String> mailTo, ConfiguracaoEmail config) throws EmailException, IOException {
this.email = emailConfig(config);
email.setSubject(subject);
addTo(mailTo);
}
private void addTo(List<String> mailTo) throws EmailException {
for (String mail : mailTo) {
email.addTo(mail);
}
}
public HtmlEmail getEmail() {
return email;
}
public void setEmail(HtmlEmail email) {
this.email = email;
}
private HtmlEmail emailConfig(ConfiguracaoEmail cfg) throws EmailException {
HtmlEmail email = new HtmlEmail();
email.setDebug(cfg.getDebug());
email.setTLS(cfg.getTLS());
email.setSSL(true);
email.setHostName(cfg.getServidor());
email.setFrom(cfg.getRemetente(), cfg.getTitulo());
email.setCharset(cfg.getCodificacao());
email.setAuthentication(cfg.getAutenticacao(), cfg.getSenha());
email.setSmtpPort(cfg.getPortaSMTP());
email.setSSL(false);
return email;
}
@Override
public void run() {
try {
email.send();
} catch (EmailException e) {
throw new RuntimeException(e);
}
}
}
有人知道可能会发生什么?它没有显示任何错误。电子邮件不会发送。 (观点:代码不仅仅是它。我只是选择电子邮件的部分)
答案 0 :(得分:0)
问题出在emailConfig
方法中,具体如下:
email.setSSL(false);
删除此行解决了我的问题。
答案 1 :(得分:0)
以下代码可以解决您的问题,它对我有用
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Email {
private static String USER_NAME = "xxxxx"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "xxxxx"; // GMail password
private static String RECIPIENT = "xxxxx@gmail.com";
public static void main(String[] args) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = { RECIPIENT }; // list of recipient email addresses
String subject = "Java send mail example";
String body = "Welcome to JavaMail!";
sendFromGMail(from, pass, to, subject, body);
}
private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");//587
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
}
如果它不起作用,请检查您的jar文件