Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java中的gmail帐户发送邮件_Java_Email - Fatal编程技术网

使用java中的gmail帐户发送邮件

使用java中的gmail帐户发送邮件,java,email,Java,Email,我曾尝试使用gmail SMTP向任何gmail帐户发送一封简单的电子邮件。获取以下错误 javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:587; 嵌套异常是: java.net.ConnectException:连接超时:连接 我的代码是 package common; import java.util.Date; import java.util.Properties; import javax.mail.Authen

我曾尝试使用gmail SMTP向任何gmail帐户发送一封简单的电子邮件。获取以下错误

javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:587;
嵌套异常是:

java.net.ConnectException:连接超时:连接

我的代码是

package common;

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

import com.sun.mail.smtp.SMTPMessage;

public class SimpleMail{

    /**
           Outgoing Mail (SMTP) Server
           requires TLS or SSL: smtp.gmail.com (use authentication)
           Use Authentication: Yes
           Port for SSL: 465
         */
        public static void main(String[] args) {

             String to="mymailid@gmail.com";
             String subject="New Mail";

             String msg="test test";

            final String user="gmailuser";
            final String pass="gmailpassowd";
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587"); //this is optional
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");



            Session session = Session.getInstance(props,new javax.mail.Authenticator() {

                    @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,pass);
           }
           });

            try {
           MimeMessage message = new MimeMessage(session);
           message.setFrom(new InternetAddress(user));
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
           message.setSubject(subject);
           message.setText(msg);

             Transport.send(message);
                System.out.println("Mail sent..");
       }catch(Exception e)
       {
           System.out.println(e);
       }
    }



}

检查你的端口。来自谷歌的支持:

如果您尝试在端口465(使用SSL)和端口587(使用TLS)上配置SMTP服务器,但仍无法发送邮件,请尝试将SMTP配置为使用端口25(使用SSL)

资料来源:


因此,尝试使用端口25,看看会发生什么。

我已经使用库通过java中的Gmail smtp发送了电子邮件。
有一个简单的好例子:

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

你的互联网连接正常吗?你在使用代理吗?我想你应该试试Gmail通过SSL的例子。此外,您的收件人的电子邮件地址(如果是gmail)应具有此设置,否则邮件无法发送-
Gmail安全设置->帐户权限->访问不太安全的应用程序
java.net.ConnectException:连接超时:连接表示您有一些internet和/或防火墙问题关闭防火墙并关闭tryfirewall已尝试使用SSL端口25,也允许访问不太安全的应用程序。获取以下错误,线程“main”java.lang.RuntimeException中出现异常:javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:25;你能ping smtp.gmail.com吗?ping带有32字节数据的gmail-smtp-msa.l.google.com[74.125.200.108]请求超时。请求超时。请求超时。请求超时。Ping统计数据为74.125.200.108:数据包:发送=4,接收=0,丢失=4(100%丢失),这是您的问题。请与您的网络管理员联系。