简体   繁体   English

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

[英]send mail using gmail account in java

I have tried to send a simple e-mail to any gmail account using Gmail SMTP. 我尝试使用Gmail SMTP将简单的电子邮件发送到任何gmail帐户。 getting the below error. 得到以下错误。

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

My code is 我的代码是

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);
       }
    }



}

Check your ports. 检查您的端口。 From Google's support: 在Google的支持下:

If you tried configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL). 如果您尝试在端口465(带有SSL)和端口587(带有TLS)上配置SMTP服务器,但是仍然无法发送邮件,请尝试将SMTP配置为使用端口25(带有SSL)。

Source: https://support.google.com/mail/answer/78775?hl=en 来源: https//support.google.com/mail/answer/78775?hl = zh_CN

So, try using port 25 and see what happens. 因此,尝试使用端口25,看看会发生什么。

I have sent emails through the Gmail smtp in java using the Apache Commons Email library. 我已经使用Apache Commons Email库通过Java中的Gmail smtp发送了电子邮件。
The documentation has a nice and simple example: 文档有一个很好的简单示例:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM