通过Java发送电子邮件 - javax.mail.MessagingException的:无法连接到SMTP主机:本地主机,端口:587; [英] Sending emails through Java - javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587;

查看:4778
本文介绍了通过Java发送电子邮件 - javax.mail.MessagingException的:无法连接到SMTP主机:本地主机,端口:587;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在对涉及一个.jar文件的创建项目。以下是可以达到的,我应该导出/读朋友的注册表路径,一旦这样做是我应该得到的结果通过电子邮件回来。整个概念是在创建jar文件,一旦它被点击我通过我的电子邮件得到的结果,因为我实际上是通过电子邮件发送的。

(我希望这是有道理的)

因此​​,首先,我结合以下code实际读取注册表并显示键(我从受欢迎的岗位上进行读/写注册表堆栈溢出了它),这样在阅读过程工作正常,现在我的问题是与电子邮件code,

(我不太清楚原车主到此code是谁,但完全归功于它他)
我试图让这个电子邮件code的工作,这样我就可以继续送我的jar文件作为附件工作,当用户点击它的方式链接到我的code的基本概念jar文件,注册表结果将通过电子邮件发送给我。

 进口java.util.Properties;
进口javax.mail.Message;
进口javax.mail.MessagingException的;
进口javax.mail.PasswordAut​​hentication;
进口javax.mail.Session的;
进口javax.mail.Transport;
进口javax.mail.internet.AddressException;
进口javax.mail.internet.InternetAddress;
进口的javax.mail.internet.MimeMessage;公共类邮件code {  公共静态无效的主要(字串[] args)抛出异常{
     最后弦乐smtp_host =smtp.gmail.com;
    最后弦乐smtp_username =user@gmail.com;
    最后弦乐smtp_password参数=密码;
    最后弦乐smtp_connection =TLS; //使用TLS或SSL连接    最后弦乐toEmail =tomail@hotmail.com;
    最后弦乐fromEmail =** @ gmail.com    属性道具=新特性();
    props.put(mail.smtp.auth,真);   如果(smtp_connection.equals(TLS)){
            props.put(mail.smtp.starttls.enable,真);
            props.put(mail.smtp.port,587);
    }其他{
            props.put(mail.smtp.socketFactory.port,465);
            props.put(mail.smtp.socketFactory.class,javax.net.ssl​​.SSLSocketFactory);
            props.put(mail.smtp.port,465);
    }    会话的会话= Session.getInstance(道具,
      新javax.mail.Authenticator(){
            @覆盖
            受保护的PasswordAut​​hentication的getPasswordAut​​hentication(){
                    返回新的PasswordAut​​hentication(smtp_username,smtp_password参数);
            }
      });    尝试{
        消息味精=新的MimeMessage(会话);
        msg.setFrom(新网际地址(fromEmailNoReply));
        msg.addRecipient(Message.RecipientType.TO,
                         新的网际地址(toEmail,收件人先生));
        msg.setSubject(欢迎来到的JavaMail API);
        msg.setText(JavaMail的API测试 - 通过远程SMTP服务器发送电子邮件的例子);
        Transport.send(MSG);
        的System.out.println(电子邮件发送成功......);
    }赶上(是AddressException E){
        抛出新的RuntimeException(E);
    }赶上(MessagingException E){
        抛出新的RuntimeException(E);
    }
   }
 }

这是我收到的错误消息:

 异常螺纹主了java.lang.RuntimeException:javax.mail.MessagingException的:无法连接到SMTP主机:本地主机,端口:587;
嵌套的异常是:
java.net.SocketException异常:权限被拒绝:连接


解决方案

以下code可以帮助你解决你的问题,其工作........

 进口的java.util。*;
导入javax.mail *。
进口javax.mail.internet *。公共类电子邮件{私人静态字符串USER_NAME =用户名; // GMail的用户名(之前的部分@ gmail.com)
私有静态字符串密码=密码; // Gmail密码私人静态字符串收件人=xxxxx@gmail.com;公共静态无效的主要(字串[] args){
    从= USER_NAME字符串;
    字符串传递= PASSWORD;
    的String []为= {}收件人; //收件人电子邮件地址列表
    字符串主题=Java的发送邮件的例子
    绳体=嗨......;    sendFromGMail(距离,传球,收件人,主题,正文);
}私有静态无效sendFromGMail(从字符串,字符串传递,字符串[]到,弦乐主题,绳体){
    属性道具= System.getProperties();
  字符串主机=smtp.gmail.com;    props.put(mail.smtp.starttls.enable,真);    props.put(mail.smtp.ssl.trust,主机);
    props.put(mail.smtp.user,从);
    props.put(mail.smtp.password,通过);
    props.put(mail.smtp.port,587);
    props.put(mail.smtp.auth,真);
    会话的会话=作为Session.getDefaultInstance(道具);
    的MimeMessage消息=新的MimeMessage(会话);    尝试{
        message.setFrom(新网际地址(从));
        网际地址[] =的toAddress新的网际地址[to.length]        //为了得到地址数组
        的for(int i = 0; I< to.length;我++){
            的toAddress [I] =新的网际地址(在[I]);
        }        的for(int i = 0; I< toAddress.length;我++){
            message.addRecipient(Message.RecipientType.TO,的toAddress [I]);
        }        message.setSubject(一级学科);
        message.setText(体);
        交通运输= session.getTransport(SMTP);
        transport.connect(主机,从,通);
        transport.sendMessage(消息,message.getAllRecipients());
        transport.close();    }
    赶上(AE是AddressException){
        ae.printStackTrace();
    }
    赶上(MessagingException我){
        me.printStackTrace();
    }
    }
   }

I'm currently working on a project that involves the creation of a .jar file. The following is to be achieved, I'm supposed to export/read the path of a friend's registry, once this is done I'm supposed to get the result back via email. The whole concept is in creating the jar file and once it's clicked I get the results through my email since I actually sent it through email.

(I hope this makes sense)

So first, I've combined the following code to actually read the registry and display the keys ( i got it from the popular post on stack overflow for read/write registries) so the reading process is working fine, now my problem is with the email code,

(I'm not quite sure who the original owner to this code is but full credit goes it him) I'm trying to get the basic concept of this email code to work so that I can continue working on sending my jar file as an attachment and link it to my code in a way when a user clicks on the jar file, the registry results will be emailed back to me.

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailCode {

  public static void main(String[] args) throws Exception {


     final String smtp_host = "smtp.gmail.com";
    final String smtp_username = "user@gmail.com";
    final String smtp_password = "password";
    final String smtp_connection = "TLS";  // Use 'TLS' or 'SSL' connection

    final String toEmail="tomail@hotmail.com";
    final String fromEmail="**@gmail.com";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");

   if (smtp_connection.equals("TLS")) {
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.port", "587");
    } else{
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.port", "465");
    }

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(smtp_username, smtp_password);
            }
      });

    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromEmail, "NoReply"));
        msg.addRecipient(Message.RecipientType.TO,
                         new InternetAddress(toEmail, "Mr. Recipient"));
        msg.setSubject("Welcome To JavaMail API");
        msg.setText("JavaMail API Test - Sending email example through remote smtp server");
        Transport.send(msg);
        System.out.println("Email sent successfully...");
    } catch (AddressException e) {
        throw new RuntimeException(e);
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
   }
 }

This is the error message I'm getting:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587;
nested exception is:
java.net.SocketException: Permission denied: connect  

解决方案

The following code may help you to solve your problem, its working........

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Email {

private static String USER_NAME = "username";  // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "password"; // GMail password

private static String RECIPIENT = "xxxxx@gmail.com";

public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT }; // list of recipient email addresses
    String subject = "Java send mail example";
    String body = "hi ....,!";

    sendFromGMail(from, pass, to, subject, body);
}

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
  String host = "smtp.gmail.com";

    props.put("mail.smtp.starttls.enable", "true");

    props.put("mail.smtp.ssl.trust", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");


    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {


        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }



        message.setSubject(subject);
        message.setText(body);


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


        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
    }
   } 

这篇关于通过Java发送电子邮件 - javax.mail.MessagingException的:无法连接到SMTP主机:本地主机,端口:587;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆