简体   繁体   中英

how to send email through outlook from java

i have using the following code for sending mail within a domain.

public void sendMail(String mailServer, String from, String to,
            String subject, String messageBody, String[] attachments)
            throws MessagingException, AddressException {
        // Setup mail server
        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailServer);

        // Get a mail session
        Session session = Session.getDefaultInstance(props, null);

        // Define a new mail message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject);

        // Create a message part to represent the body text
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(messageBody);

        // use a MimeMultipart as we need to handle the file attachments
        Multipart multipart = new MimeMultipart();

        // add the message body to the mime message
        multipart.addBodyPart(messageBodyPart);

        // add any file attachments to the message
        addAtachments(attachments, multipart);

        // Put all message parts in the message
        message.setContent(multipart);

        // Send the message
        Transport.send(message);
        System.err.println("Message Send");

    }

    protected void addAtachments(String[] attachments, Multipart multipart)
            throws MessagingException, AddressException {
        for (int i = 0; i < attachments.length ; i++) {
            String filename = attachments[i];
            MimeBodyPart attachmentBodyPart = new MimeBodyPart();

            // use a JAF FileDataSource as it does MIME type detection
            DataSource source = new FileDataSource(filename);
            attachmentBodyPart.setDataHandler(new DataHandler(source));

            // assume that the filename you want to send is the same as the
            // actual file name - could alter this to remove the file path
            attachmentBodyPart.setFileName(filename);

            // add the attachment
            multipart.addBodyPart(attachmentBodyPart);
        }
    }

but if with the same code i try to send an email outside the domain, say i am sending email from mjsharma@domain.com to mhsharma@gmail,com then it fails and gives me the following error. 550 5.7.1 Rcpt command failed: Mail denied due to site's policy

Am i missing something in the above code. Please help me

I suspect that's not a problem in your code, but in your mail server ( sendmail ?) configuration. I would talk to whoever administers your mail infrastructure.

Assuming your machine has Outlook installed... have you tried using moyosoft's java outlook connector? it's pretty simple to use and passes through network restrictions because it connects to your Outlook application and then sends the mail, so any restriction on smtp ports or servers/proxy policies will be ignored if your Outlook client is working fine.

if you are doing it with command line server-side then i guess this answer is useless for you.

Source: i had similar problem (not same error code, more like an intranet restriction) and using this library solved my problem because of the posted above.

It's likely that your local mail server requires you to authenticate before sending mail out into the world. This is a common policy to prevent spammers from relaying their mail through it.

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