我正在使用服务监视器,它会检查http请求的http-Status的其他值。一切正常,但我正在为我的代码寻找更聪明的方法。 我正在使用的代码发送带附件的邮件。但是,如果我不需要在每个if语句中添加附件块,那就太好了 我已经搜索了一个可能的解决方案,但据我所见,我无法在方法中创建方法。 那么......有没有办法避免额外的代码行?附件块在时间上是相同的......
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
// define the class
class SendMailTLS {
// define a method which recive your status code
// instead of define main method
public void sendMail(
int httpStatus, ) {
final String username = "sender@domain.com";
final String password = "password";
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);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@domain.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recipients@domain.com"));
message.setSubject("Status alert - Service");
BodyPart messageBodyPart = new MimeBodyPart();
if (httpStatus == 200) {
messageBodyPart.setText("OK! The http Status is: " + httpStatus);
//////////// AttachmentBlock START//////////
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
String attachmentPath = "c:\\doms\\log.txt";
messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getFile().getName());
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
//////////// AttachmentBlock END//////////
Transport.send(message);
}
if (httpStatus != 200) {
messageBodyPart.setText("ERROR! The http Status is: " + httpStatus);
//////////// AttachmentBlock START//////////
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
String attachmentPath = "c:\\doms\\log.txt";
messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getFile().getName());
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
//////////// AttachmentBlock END//////////
Transport.send(message);
}
//Transport.send(message)
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
// create new instance of your class
def mailSender = new SendMailTLS();
// send the mail passing the parameters
mailSender.sendMail(httpStatus);
答案 0 :(得分:1)
您可以在类中创建一个private
方法来执行您需要的代码块:
class SendMailTLS {
// method that contains the attachment block
private void sendAttachment(BodyPart messageBodyPart) {
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
String attachmentPath = "c:\\doms\\log.txt";
messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getFile().getName());
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
}
public void sendMail(
int httpStatus, )
// ... your code
if (httpStatus == 200) {
messageBodyPart.setText("OK! The http Status is: " + httpStatus);
// execute the private method here and anywhere else you want the same code to run
sendAttachment(messageBodyPart);
}
// ... rest of the code
}
另外,如上所述,您的http代码无效。您将代码200作为两个不同if语句中的条件,一个用于良好请求,另一个用于坏请求。我假设您的第二个代码是400个范围之一(客户端错误代码)。