0

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

邮件服务器是 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();        


}

感谢您提出任何建议。

4

1 回答 1

0

After initializating properties like username, password etc

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);
    }
  });

hope this would help

于 2013-04-29T11:20:56.463 回答