无法通过Java发送电子邮件 [英] Unable to send e-mail through Java

查看:156
本文介绍了无法通过Java发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过提供相同的问题后,我写了下面的代码。但是我收到以下异常:



javax.mail.MessagingException:无法连接到SMTP主机:smtp .gmail.com,端口:587;
嵌套异常是:java.net.ConnectException:连接超时:connect

  public static void main(String [ ] args){

String to =xxx@gmail.com//有效的Gmail地址。
来自=yyy@gmail.com的字符串; //有效的Gmail地址

String host =smtp.gmail.com;
String password =****; //从

int port = 587中使用的gmaill acc的密码;


属性properties = System.getProperties();
properties.put(mail.smtp.starttls.enable,true);
properties.setProperty(mail.smtp.host,host);
properties.setProperty(mail.smtp.user,from);
properties.setProperty(mail.smtp.password,password);
properties.setProperty(mail.smtp.port,587);
properties.setProperty(mail.smtp.auth,true);
Session session = Session.getDefaultInstance(properties,null);

尝试{

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

message.setSubject(Test Mail);

message.setText(这只是生成的测试邮件);

Transport transport = session.getTransport(smtp);
transport.connect(host,from,password);
InternetAddress []地址=新InternetAddress [1];
地址[0] =新InternetAddress(至);
transport.sendMessage(消息,地址);


System.out.println(Message Sent Successfully);
} catch(MessagingException excp){
System.out.println(excp);
}

}

有人可以说错我是这样做。在我的gmail帐户中是否有需要设置为使用gmail smtp服务器的设置?

解决方案

请尝试以下代码。你需要下载 javax.mail package(一个jar文件),我假设你已经有了那个jar文件,因为你已经试过了这段代码,因此,提供下载该jar文件的链接。正确设置类路径并导入必要的软件包。请注意防火墙和您选择的主机端口。

  import java.util.logging.Level; 
import java.util.logging.Logger;
import javax.mail。*;
import javax.mail.internet。*;
import java.util。*;

final class MailClient
{
private class SMTPAuthenticator extends Authenticator
{
private PasswordAuthentication authentication;

public SMTPAuthenticator(String login,String password)
{
authentication = new PasswordAuthentication(login,password);
}

@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return authentication;


$ b $ public void mail()
{
try
{
String from =xyz.com ;
String to =abc.com;
String subject =您的主题。;
String message =消息文本;
String登录=xyz.com;
字符串密码=密码;

属性props = new Properties();
props.setProperty(mail.host,smtp.gmail.com);
props.setProperty(mail.smtp.port,587);
props.setProperty(mail.smtp.auth,true);
props.setProperty(mail.smtp.starttls.enable,true);

Authenticator auth = new SMTPAuthenticator(login,password);

Session session = Session.getInstance(props,auth);

MimeMessage msg = new MimeMessage(session);

尝试
{
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,
InternetAddress(to));
Transport.send(msg);
}
catch(MessagingException ex)
{
Logger.getLogger(MailClient.class.getName())。
log(Level.SEVERE,null,ex);





final public class Main
{
public static void main(String ... args)
{
新的MailClient()。mail();
}
}


After going through post provided for the same problem, I have written the following code. But I am getting the following exception :

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

public static void main(String[] args) {

    String to = "xxx@gmail.com" // valid gmail address.     
    String from = "yyy@gmail.com"; // valid gmail address

    String host = "smtp.gmail.com";
    String password = "****"; // password of the gmaill acc used in from

    int port = 587;


    Properties properties = System.getProperties();
    properties.put("mail.smtp.starttls.enable", "true");
    properties.setProperty("mail.smtp.host",host );
    properties.setProperty("mail.smtp.user", from);
    properties.setProperty("mail.smtp.password", password);
    properties.setProperty("mail.smtp.port", "587");
    properties.setProperty("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(properties,null);

    try {

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        message.setSubject("Test Mail");

        message.setText("This is just a test mail generated");

       Transport transport = session.getTransport("smtp");
       transport.connect(host,from,password);
       InternetAddress[] addresses = new InternetAddress[1];
       addresses[0] = new InternetAddress(to);
       transport.sendMessage(message,addresses);


        System.out.println("Message Sent Successfully");
    }catch(MessagingException excp){
        System.out.println(excp);
    }

}

Can somebody tell mistake that I am doing. Is there any setting in my gmail account which needs to be set to use the gmail smtp server?

解决方案

Try the following code. You will need to download javax.mail package (A jar file), I assumed that you would have already that jar file because you have tried this code and consequently, I don't provide the link to download that jar file. Set the class path properly and import the necessary packages. Take care with the firewall and the host port you selected.

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

final class MailClient
{
    private class SMTPAuthenticator extends Authenticator
    {
        private PasswordAuthentication authentication;

        public SMTPAuthenticator(String login, String password)
        {
             authentication = new PasswordAuthentication(login, password);
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
             return authentication;
        }
    }

    public void mail()
    {
        try
        {
            String from = "xyz.com";
            String to = "abc.com";
            String subject = "Your Subject.";
            String message = "Message Text.";
            String login = "xyz.com";
            String password = "password";

            Properties props = new Properties();
            props.setProperty("mail.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.port", "587");
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.starttls.enable", "true");

            Authenticator auth = new SMTPAuthenticator(login, password);

            Session session = Session.getInstance(props, auth);

            MimeMessage msg = new MimeMessage(session);

           try
           {
                msg.setText(message);
                msg.setSubject(subject);
                msg.setFrom(new InternetAddress(from));
                msg.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));
                Transport.send(msg);
           }
           catch (MessagingException ex)
           {
                Logger.getLogger(MailClient.class.getName()).
                log(Level.SEVERE, null, ex);
           }
        }
    }
}

final public class Main
{
    public static void main(String...args)
    {
        new MailClient().mail();
    }
}

这篇关于无法通过Java发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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