[英]Restricting the email to be send from unix box machine itself through java Mail
我有下面的java程序,該程序用於通過java Mail API成功發送郵件,現在我的程序已部署在名為lonabc1123
unix機器上,並且此unix框具有ip 11.111.11.11
,現在我想在此限制某些內容,那就是Java郵件僅在程序在此unix框itsef(lonabc1123)上運行時才發送,現在任何人都可以從此unix框之外發送郵件,我的意思是有人可以從其他unix框機發送郵件。
所以請告知我我知道這封郵件是從名為lonabc1123的IP地址為11.111.11.11的unix盒式計算機發送的,除此之外,沒有人可以從任何其他計算機發送郵件,所以我該如何扭曲下面的代碼,以便郵件可以僅從一個具有IP 11.111.11.11的lonabc1123的Unix框發送,我可以進行基於IP的檢查嗎,在發送郵件之前我將檢查IP,請告知
static String smtpHost = "11.162.90.80";
static String mailSmtpPort = "1965";
static String mailTo[] = {"ena@abs.com" };
static String mailCc[] = {"ena@abs.com" };
static String bccAddress[] = null;
public static void main(String [] args) throws Exception, IOException, Exception{
postEmail(mailTo, mailCc, "k",
"testSubject", "Body123", smtpHost , mailSmtpPort);
}
public static void postEmail(String mailTo[], String mailCc[], String from, String subject, String text, String smtpHost, String mailSmtpPort ) throws Exception, DocumentException, IOException {
try {
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mail.store.protocol", "imaps");
properties.put("mailSmtpPort", mailSmtpPort);
//obtaining the session
Session emailSession = Session.getDefaultInstance(properties);
//Enable for debuging
emailSession.setDebug(true);
Message emailMessage = new MimeMessage(emailSession);
if(mailTo!= null){
InternetAddress[] addressTo = new InternetAddress[mailTo.length];
for (int i = 0; i < mailTo.length; i++) {
addressTo[i] = new InternetAddress(mailTo[i]);
}
emailMessage.setRecipients(RecipientType.TO, addressTo);
}
InternetAddress[] addresscc = new InternetAddress[mailCc.length];
for (int i = 0; i < mailCc.length; i++) {
addresscc[i] = new InternetAddress(mailCc[i]);
}
emailMessage.setRecipients(RecipientType.CC, addresscc);
emailMessage.setFrom(new InternetAddress(from));
emailMessage.setSubject(subject);
emailMessage.setContent(text, "text/html");
//Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(text, "text/html");
messageBodyPart.setText(text);
// Create a multipart message
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// attachment part
MimeBodyPart attachPart = new MimeBodyPart();
String filename = "c:\\swap.xls";
multipart.addBodyPart(attachPart);
// Send the complete message parts
emailMessage.setContent(multipart);
Transport.send(emailMessage);
} catch (AddressException e) {
e.printStackTrace();
} catch(MessagingException messagingException) {
messagingException.printStackTrace();
throw new Exception(messagingException.getMessage());
}
}
您試圖將端口更改為1965,但是使用的屬性錯誤: properties.put("mailSmtpPort", mailSmtpPort);
正確的屬性是: properties.put("mail.smtp.port", mailSmtpPort);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.