gpt4 book ai didi

java - 通过 java MAIL API 从特定的 UNIX 机器发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 07:27:45 26 4
gpt4 key购买 nike

我有下面的java程序,用于通过java Mail API成功发送邮件,现在我的这个程序部署在名为lonabc1123的unix机器上,并且这个unix盒子有一个ip 11.111.11.11,现在我想在这里限制某些事情,那就是只有当程序在这个unix盒子itsef(lonabc1123)上运行时才会发送java邮件,现在任何人都可以发送邮件除了这个 UNIX 盒子我的意思是有人也可以从其他 UNIX 盒子机器发送邮件。

所以请告诉我,我知道邮件是从名为lonabc1123的unix机器发送的,它的IP地址为11.111.11.11,除此之外没有人可以从任何其他机器发送邮件,所以我怎样才能扭转下面的代码片段该邮件只会从一个unix盒子发送,即lonabc1123,其IP为11.111.11.11,我可以进行基于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());
}
}

最佳答案

if (!java.net.InetAddress.getLocalHost().getHostAddress().equals("11.11.11.11")) ... // fail

关于java - 通过 java MAIL API 从特定的 UNIX 机器发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36579253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com