Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29934

Tried to send an email but Tranport.send() doesn't run

$
0
0

I tried sending an email with attached pdf-file using javax.mail maven dependency. The following code runs, but it doesnt finish Transport.send(message) line. There is no error, but program doesn't finist. I found out that it could take some time, but I waited over a minute and still nothing. I've looked at similar questions but the solutions have not worked.

public class EmailService {    private String host = "";    private int port = 0;    private String username = "";    private String password = "";    public EmailService(String host, int port, String username, String password) {        this.host = host;        this.port = port;        this.username = username;        this.password = password;        sendMail();    }    private void sendMail() {        Properties prop = new Properties();        prop.put("mail.smtp.auth", true);        prop.put("mail.smtp.starttls.enable", "true");        prop.put("mail.smtp.host", host);        prop.put("mail.smtp.port", port);        prop.put("mail.smtp.ssl.trust", host);        Session session = Session.getInstance(prop, new Authenticator() {            @Override            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication(username, password);            }        });        try {            Message message = new MimeMessage(session);            message.setFrom(new InternetAddress("email@gmail.com"));            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("email@gmail.com"));            message.setSubject("Mail Subject");            String msg = "This is my first email using JavaMailer";            MimeBodyPart mimeBodyPart = new MimeBodyPart();            mimeBodyPart.setContent(msg, "Email sent with java");            MimeBodyPart attachmentBodyPart = new MimeBodyPart();            attachmentBodyPart.attachFile(new File("file.pdf"));            Multipart multipart = new MimeMultipart();            multipart.addBodyPart(mimeBodyPart);            multipart.addBodyPart(attachmentBodyPart);            message.setContent(multipart);            Transport.send(message);            System.out.println("Message sent");        } catch (Exception e) {            e.printStackTrace();        }    }    public static void main(String ... args) {        new EmailService("smtp.gmail.com", 25, "email@gmail.com", "password");    }}

Viewing all articles
Browse latest Browse all 29934

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>