【问题标题】:JavaMail - Task ends without errors but no mail is sentJavaMail - 任务结束没有错误,但没有发送邮件
【发布时间】:2015-11-01 09:20:12
【问题描述】:

最近我一直致力于在我的项目的两个不同线程中创建一个自动每日邮件发送器和一个每周邮件。

邮件服务器是 MS Exchange(不记得版本)

当唯一的每日邮件正在运行时,我的邮件发送得很好。 现在我已经为每周邮件添加了另一个线程,我遇到了以下一些问题:

  • 只有一个线程能够发送电子邮件(不能同时发送)
  • 没有一个线程能够发送电子邮件

在我的日志中我没有错误的证据,似乎与 smtp 服务器的连接不是问题,并且邮件已发送,但是当我检查我的邮箱时,根本没有邮件到达。

我会把关于我的电子邮件课程的代码发给你

public class Email {

boolean debug = false ;
String smtpServer = null;
int smtpPort = null;
String smtpSender = null;
String smtpUser = null;
String smtpPassword = null;


public Email(){
        smtpServer = Config.SMTP_SERVER;
        smtpPort = Config.SMTP_PORT;
        smtpSender = Config.SMTP_SENDER;
        smtpUser = Config.SMTP_USER;
        smtpPassword = Config.SMTP_PASSWORD;
}



public void postMailAttach( String recipients[], String subject, String message, String filename ) throws MessagingException {


Properties props = new Properties();
    props.put("mail.smtp.auth", "true"); 

       Session session = Session.getInstance(props, null);


       //SET SERVER FOR MESSAGE
       Message msg = new MimeMessage(session);
       msg.setFrom(new InternetAddress(smtpSender));



       InternetAddress[] toAddress = new InternetAddress[recipients.length];

       for (int i = 0; i < recipients.length; i++){
         toAddress[i] = new InternetAddress(recipients[i]);
       }

       //SET RECIPIENTS FOR MESSAGE
       msg.setRecipients(Message.RecipientType.TO, toAddress);    
       //SET SUBJECT
       msg.setSubject(subject);

       //SET BODY PART OF MESSAGE 
       BodyPart messageBodyPart = new MimeBodyPart();
       messageBodyPart.setText(message);

       Multipart multipart = new MimeMultipart();
       multipart.addBodyPart(messageBodyPart);
       messageBodyPart = new MimeBodyPart();

       //GET FILES TO ATTACH
       DataSource source = new FileDataSource(filename);
       messageBodyPart.setDataHandler(new DataHandler(source));
       //SET FILE NAME
       messageBodyPart.setFileName(filename);

       multipart.addBodyPart(messageBodyPart);
       msg.setContent(multipart);

        //SEND THE EMAIL
        Transport transport = session.getTransport("smtp");    
        transport.connect(smtpServer,smtpPort,smtpUser,smtpPassword);    
        transport.sendMessage(msg,msg.getAllRecipients());    
        transport.close();        

}

public void postMail (String recipients[], String subject, String message) throws MessagingException{

    //Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.auth", "true"); 


// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
//session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(smtpSender);
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);


// Optional : You can also set your custom headers in the Email if you Want
//msg.addHeader("MyHeaderName", "myHeaderValue");

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
    Transport transport = session.getTransport("smtp");    
    transport.connect(smtpServer,smtpPort,smtpUser,smtpPassword);    
    transport.sendMessage(msg,msg.getAllRecipients());    
    transport.close();        


}

感谢您的建议。

【问题讨论】:

  • 这里没有看到线程,但是你说问题出在这个类的多线程使用上。
  • 有 2 个单独的线程。线程只是检查何时发送电子邮件、哪些消息、哪些主题和哪些收件人的容器,仅此而已。发送工作全部在 Email 类上完成
  • 两个线程是共享Email类的同一个对象还是每个线程都创建自己的Email类对象?
  • 对象不共享,它是在 2 个线程中创建的
  • 你通过设置session.setDebug(true);进行调试。这将打印 JavaMail 日志。

标签: java email jakarta-mail exchange-server


【解决方案1】:

初始化用户名、密码等属性后

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
  });

希望这会有所帮助

【讨论】:

  • 我会试试这个并告诉你
  • 写它而不是Session session = Session.getInstance(props,null);并删除传输部分。
  • 我已添加此部分并删除了 transport.connect() 部分,但现在我在异常中收到“未连接”
  • 我编辑了我的答案。将它们全部复制到您的代码中,并确保您输入用户名和密码
  • 绝对不需要 Authenticator,它只会让代码更复杂,没有任何好处。这些属性设置对 Gmail 来说很好,但此人使用的是他们自己的 Exchange 服务器。
猜你喜欢
  • 2012-09-11
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多