【发布时间】: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