简体   繁体   中英

Restricting the email to be send from unix box machine itself through java Mail

I have the below java program which is used to send a mail successfully through java Mail API , now my this program is deployed on unix machine named lonabc1123 and this unix box has an ip 11.111.11.11 , now i want to restrict here certain things , that is the java mail would only be send if the program is running on this unix box itsef(lonabc1123) , rite now any one can send the mail apart from this unix box i mean some one can send an mail from other unix box machine also.

so please advise i know the mail is to be send from unix box machine named lonabc1123 which has IP 11.111.11.11 and apart from this nobody could send an mail from any other machine so how can i twist the below piece of code so that mail would be only send from one unix box that is lonabc1123 which has ip 11.111.11.11 , can i have an ip based check where i will check the ip before sending the mail please advise

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());
    }
}

You are trying to change the port to 1965, but you are using the wrong property: properties.put("mailSmtpPort", mailSmtpPort);

The correct property is: properties.put("mail.smtp.port", mailSmtpPort);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM