无法通过java programm发送电子邮件

时间:2016-06-17 10:43:06

标签: java smtp jms javamail

尝试在谷歌上找到的选项数量,包括stackoverflow但未成功:

package net.codejava.spring;

import java.util.Properties;

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 SendEmail {
    public static void main(String[] args) {
        String to = "abc@gmail.com";
        String from = "info@xyz.com";

        // Get the session object
        Properties properties = System.getProperties();
        properties.setProperty("mail.debug", "true");
        properties.setProperty("mail.smtp.host", "smtp.xyz.com");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.port", "465");
        properties.setProperty("mail.smtp.socketFactory.port", "465");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("info@xyz.com", "test999");
            }
        });

        // compose the message
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("Ping");
            message.setText("Hello, this is example of sending email  ");

            // Send message
            Transport.send(message);
            System.out.println("message sent successfully....");

        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

我尽我所能,请帮助我。另外,试过这个,但又失败了,

https://confluence.atlassian.com/jirakb/unable-to-send-mail-due-to-could-not-connect-to-smtp-host-297665338.html

0 个答案:

没有答案