我使用JavaMail API在data insertion in database
时通知管理员这很好用。管理员收到数据输入通知。
但是,我还需要通过同时向他发送确认通知客户,但它不起作用。客户未收到任何确认。我还想在主题中粘贴确认号码。它可能与港口冲突有关吗?
EmailAdmin.sendEmail(to, subject, body);
EmailCustomer.sendEmail(EM, subjectsub, bodybod, CN);
EmailAdmin.java
package session;
import java.util.Date;
import java.util.Properties;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Stateless
@LocalBean
public class EmailAdmin {
private final int port = 465;
private final String host = "smtp.server.com";
private final String from = "admin@domain.com";
private final boolean auth = true;
private final String username = "admin@domain.com";
private final String password = "password";
private final Protocol protocol = Protocol.SMTPS;
private final boolean debug = true;
public void sendEmail(String to, String subject, String body) {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
switch (protocol) {
case SMTPS:
props.put("mail.smtp.ssl.enable", true);
break;
case TLS:
props.put("mail.smtp.starttls.enable", true);
break;
}
Authenticator authenticator = null;
if (auth) {
props.put("mail.smtp.auth", true);
authenticator = new Authenticator() {
private final PasswordAuthentication pa = new PasswordAuthentication(username, password);
@Override
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
Session session = Session.getInstance(props, authenticator);
session.setDebug(debug);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(body);
Transport.send(message);
} catch (MessagingException ex) {
}
}
}
EmailCustomer.java
package session;
import java.util.Date;
import java.util.Properties;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Stateless
@LocalBean
public class EmailCustomer {
private final int port = 465;
private final String host = "smtp.server.com";
private final String from = "admin@domain.com";
private final boolean auth = true;
private final String username = "admin@domain.com";
private final String password = "password";
private final Protocol protocol = Protocol.SMTPS;
private final boolean debug = true;
public void sendEmail(String EM, String subjectsub, String bodybod, String CN) {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
switch (protocol) {
case SMTPS:
props.put("mail.smtp.ssl.enable", true);
break;
case TLS:
props.put("mail.smtp.starttls.enable", true);
break;
}
Authenticator authenticator = null;
if (auth) {
props.put("mail.smtp.auth", true);
authenticator = new Authenticator() {
private final PasswordAuthentication pa = new PasswordAuthentication(username, password);
@Override
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
Session session = Session.getInstance(props, authenticator);
session.setDebug(debug);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(EM)};
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject, CN);
message.setSentDate(new Date());
message.setText(body);
Transport.send(message);
} catch (MessagingException ex) {
}
}
}