public static void sendMail(String[] to, String subject, String messageBody, String[] cc, String[] bcc) throws Exception {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from, fromname));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
msg.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
msg.setSubject(subject);
msg.setContent(messageBody, "text/html");
if(cc[0] != "") {
InternetAddress[] ccAddress = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
ccAddress[i] = new InternetAddress(cc[i]);
}
for (int i = 0; i < ccAddress.length; i++) {
msg.addRecipient(Message.RecipientType.CC, ccAddress[i]);
}
}
if(bcc[0] != "") {
InternetAddress[] bccAddress = new InternetAddress[bcc.length];
for (int i = 0; i < bcc.length; i++) {
bccAddress[i] = new InternetAddress(bcc[i]);
}
for (int i = 0; i < bccAddress.length; i++) {
msg.addRecipient(Message.RecipientType.BCC, bccAddress[i]);
}
}
Transport transport = session.getTransport();
try {
System.out.println("Sending...");
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
System.out.println("Error message: " + ex.getMessage());
} finally {
transport.close();
}
}
public static void sendMail(String[] to, String subject, String messageBody) throws Exception {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from, fromname));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
msg.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
msg.setSubject(subject);
msg.setContent(messageBody, "text/html");
Transport transport = session.getTransport();
try {
System.out.println("Sending...");
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
System.out.println("Error message: " + ex.getMessage());
} finally {
transport.close();
}
}
在此,我们在两种方法中使用相同的代码。 我想将通用代码放在另一种方法中。 我将尝试将通用代码放入另一个方法中,然后尝试调用该方法,但不适用于两个方法。 如果我们尝试在这两种方法中都调用一个通用方法,那么它将仅适用于一个方法。
请给我任何建议。
答案 0 :(得分:1)
您似乎可以使用空数组编写第二种方法来使用第一种方法:
public static void sendMail(String[] to, String subject, String messageBody) throws Exception {
sendMail(to, subject, messageBody, new String[]{""}. new String[]{""});
}
这会在cc和bcc数组中传递一个空字符串,以便您的其他方法将跳过该数组。
请注意,您将在此处保持对NullPointerExceptions的了解:
if(cc[0] != "") { ... }
这是盲目的假设cc数组包含至少一个元素。 如果要使用一个空数组调用该方法,或者为cc或bcc调用null,则程序将引发异常。 在该if语句内部,您已经在使用cc.length,因此,如果cc.length为零,则不会添加CC地址。 该测试的更好实现可能是:
if (cc != null && cc.length > 0) {...}
此外,我不确定为什么您构建ccAddress数组只是为了丢弃它。 这是同一循环,用单个ccAddress对象替换ccAddress数组。
if(cc != null && cc.length > 0) {
for (int i = 0; i < cc.length; i++) {
InternetAddress ccAddress = new InternetAddress(cc[i]);
msg.addRecipient(Message.RecipientType.CC, ccAddress);
}
}
答案 1 :(得分:0)
您可以做的最简单的事情就是实现第二种方法,以便它以某种方式调用第一种,这样就不会添加任何抄送或密件抄送收件人。根据您当前的代码,它可以是:
public static void sendMail(String[] to, String subject, String body) {
sendMail(to, subject, body, new Object[]{""}, new Object[]{""});
}
答案 2 :(得分:0)
您不必制作其他方法。参数计数较低的方法只调用参数计数较高的方法,其值为空,然后在“ real”方法中进行空检查。
public static void sendMail(String[] to, String subject, String messageBody, String[] cc, String[] bcc) throws Exception {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from, fromname));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
msg.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
msg.setSubject(subject);
msg.setContent(messageBody, "text/html");
if(cc != null && cc[0] != "") {
InternetAddress[] ccAddress = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
ccAddress[i] = new InternetAddress(cc[i]);
}
for (int i = 0; i < ccAddress.length; i++) {
msg.addRecipient(Message.RecipientType.CC, ccAddress[i]);
}
}
if(bcc != null && bcc[0] != "") {
InternetAddress[] bccAddress = new InternetAddress[bcc.length];
for (int i = 0; i < bcc.length; i++) {
bccAddress[i] = new InternetAddress(bcc[i]);
}
for (int i = 0; i < bccAddress.length; i++) {
msg.addRecipient(Message.RecipientType.BCC, bccAddress[i]);
}
}
Transport transport = session.getTransport();
try {
System.out.println("Sending...");
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
System.out.println("Error message: " + ex.getMessage());
} finally {
transport.close();
}
}
public static void sendMail(String[] to, String subject, String messageBody) throws Exception {
sendMail(to, subject, messageBody, null, null);
}