简体   繁体   中英

how to send email from using outlook using a java api

I am using the below code to send email from outlook using java. But getting the error.

CODE:

    public static void mail (){
        // TODO Auto-generated method stub
        //String host="POKCPEX07.corp.absc.local";
        String host="POKCPEX07.corp.absc.local";
        final String user="satpal.gupta@accenture.com";  
        String to="satpal.gupta@accenture.com";  

        //Get the session object  
        Properties props = new Properties();  
        props.put("mail.smtp.host",host);  
        props.put("mail.smtp.auth", "false");
        props.put("mail.smtp.port", "587");


        Session session=Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("SGupta@amerisourcebergen.com","******");
            }
          });
        session.setDebug(true);

        try {
            MimeMessage message = new MimeMessage(session);
            message.saveChanges();
            message.setFrom(new InternetAddress(user));  
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
            message.setSubject("Test mail");  
            message.setText("This is test mail.");  

            //send the message
            Transport.send(message);

            System.out.println("message sent successfully...");
        }
        catch (MessagingException e) {e.printStackTrace();}

    }
}

ERROR:

javax.mail.MessagingException: Could not connect to SMTP host: POKCPEX07.corp.absc.local, port: 587;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
    at javax.mail.Service.connect(Service.java:258)
    at javax.mail.Service.connect(Service.java:137)
    at javax.mail.Service.connect(Service.java:86)
    at javax.mail.Transport.send0(Transport.java:150)
    at javax.mail.Transport.send(Transport.java:80)
    at TestEmail.mail(TestEmail.java:50)
    at TestEmail.main(TestEmail.java:16)
package com.sendmail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendAttachmentInEmail {


    private static final String SERVIDOR_SMTP = "smtp.office365.com";
    private static final int PORTA_SERVIDOR_SMTP = 587;
    private static final String CONTA_PADRAO = "xxxx@xxx.com"; //Cofig  Mail Id
    private static final String SENHA_CONTA_PADRAO = "XYZ"; // Password

    private final String from = "xxxx@xxx.com"; 
    private final String to = "xxxx@xxx.com";

    private final String subject = "Teste";
    private final String messageContent = "Teste de Mensagem";

    public void sendEmail() {
        final Session session = Session.getInstance(this.getEmailProperties(), new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(CONTA_PADRAO, SENHA_CONTA_PADRAO);
            }

        });

        try {
            final Message message = new MimeMessage(session);
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setFrom(new InternetAddress(from));
            message.setSubject(subject);
            message.setText(messageContent);
            message.setSentDate(new Date());
            Transport.send(message);
        } catch (final MessagingException ex) {
           System.out.println(" "+ex);
        }
    }

    public Properties getEmailProperties() {
        final Properties config = new Properties();
        config.put("mail.smtp.auth", "true");
        config.put("mail.smtp.starttls.enable", "true");
        config.put("mail.smtp.host", SERVIDOR_SMTP);
        config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
        return config;
    }

    public static void main(final String[] args) {
        new SendAttachmentInEmail().sendEmail();
    }

}

As posted by you in comments above, it looks like your SMTP is not configured and by looking your exception - you are using gmail.

Follow this link to configure your SMTP.

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