最近我一直致力于自动每日邮件发件人以及我项目中两个不同主题内的每周邮件。
邮件服务器是MS Exchange(不记得版本)
当唯一的每日邮件运行时,我的邮件发送得很好。 现在我已经为每周邮件添加了另一个帖子,我有一些问题:
在我的日志中我没有错误的证据,似乎与smtp服务器的连接不是问题,并且邮件已经发送,但是当我检查我的邮箱时,根本没有邮件到达。< / p>
我会发给你关于我的电子邮件课程的代码
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();
}
感谢您对任何建议的建议。
答案 0 :(得分:0)
初始化用户名,密码等属性后
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);
}
});
希望这会有所帮助