java.net.ConnectException:连接被拒绝:使用Java发送邮件时进行连接

时间:2019-03-21 08:54:07

标签: java email connection

我正在尝试使用Java和HTML建立网站。在那个网站上,我试图编写一个Java代码发送电子邮件。我没有任何编译错误,但是当我尝试编写它时,它给了我以下错误:

java.net.ConnectException:连接被拒绝:connect

完整的堆栈跟踪如下:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at Mail.sendEmail.main(sendEmail.java:50)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    ... 4 more
BUILD SUCCESSFUL (total time: 7 seconds)

我的Java代码如下:

    import java.util.Properties; 
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    public class sendEmail {

        Properties emailProperties;
        Session mailSession;
        MimeMessage emailMessage;

        public static void main(String args[]) throws AddressException,
                MessagingException {

            sendEmail javaEmail = new sendEmail();

            javaEmail.setMailServerProperties();
            javaEmail.createEmailMessage();
            javaEmail.sendEmail();
        }

        public void setMailServerProperties() {

            String emailPort = "587";//gmail's smtp port

            emailProperties = System.getProperties();
            emailProperties.put("mail.smtp.port", emailPort);
            emailProperties.put("mail.smtp.auth", "true");
            emailProperties.put("mail.smtp.starttls.enable", "true");

        }

        public void createEmailMessage() throws AddressException,
                MessagingException {
            String[] toEmails = { "shalakasawant37@gmail.com" };
            String emailSubject = "Java Email";
            String emailBody = "This is an email sent by JavaMail api.";

            mailSession = Session.getDefaultInstance(emailProperties, null);
            emailMessage = new MimeMessage(mailSession);

            for (int i = 0; i < toEmails.length; i++) {
                emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
            }

            emailMessage.setSubject(emailSubject);
            emailMessage.setContent(emailBody, "text/html");//for a html email
            //emailMessage.setText(emailBody);// for a text email

        }

        public void sendEmail() throws AddressException, MessagingException {

            String emailHost = "smtp.gmail.com";
            String fromUser = "shalakasawant38";//just the id alone without @gmail.com
            String fromUserEmailPassword = "Shalak@31";

            Transport transport = mailSession.getTransport("smtp");

            transport.connect(emailHost, fromUser, fromUserEmailPassword);
            transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
            transport.close();
            System.out.println("Email sent successfully.");
        }

    }

如果您有任何帮助,我会很高兴。我必须在3月30日提交。

0 个答案:

没有答案