无法从Java应用程序发送邮件

时间:2022-06-25 15:06:06

In my java application I need to send mails to different mail addresses. I am using the next piece of code , but for some reason it doesn't work.

在我的java应用程序中,我需要将邮件发送到不同的邮件地址。我正在使用下一段代码,但由于某种原因它不起作用。

public class main {  
    public static void main(String[] args) {  
        Properties props = new Properties();  
        props.put("mail.smtp.host", "mail.yahoo.com.");  
        props.put("mail.smtp.auth", "true");  
        props.put("mail.debug", "true");  

        Session session = Session.getInstance(props, new MyAuth());  

        try {  
            Message msg = new MimeMessage(session);  
            msg.setFrom(new InternetAddress("giginnho@yahoo.com"));  
            InternetAddress[] address = {new InternetAddress("rantravee@yahoo.com")};  
            msg.setRecipients(Message.RecipientType.TO, address);  
            msg.setSubject("subject ");  
            msg.setSentDate(new Date());  

            msg.setText("Message here ");  

            Transport.send(msg);  
        }  catch (MessagingException e) {}  
    } 

}  

class MyAuth extends Authenticator {  
    protected PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication("my username","my password");  
    }
}

I get the folowing text from debuging it:

我从下面的内容中得到了以下文字:

[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com.au.", port 25, isSSL false

Could anyone inform me , what I am doing wrong here ?

谁能告诉我,我在这里做错了什么?

3 个解决方案

#1


I am not sure, but I faced the same problem when sending mail using a gmail id, you are using yahoo. The problem was gmail uses ssl layer protection, i think same is the case with yahoo so you need to use

我不确定,但是当我使用gmail id发送邮件时,我遇到了同样的问题,你使用的是雅虎。问题是gmail使用ssl层保护,我认为雅虎的情况也是如此,所以你需要使用

mail.smtps.host instead of mail.smtp.host

and same for other properties too.

和其他属性一样。

and isSSL to true.

并且isSSL为true。

I can post complete code snippet, once i reach office and use office's machine. For now you can look at http://www.rgagnon.com/javadetails/java-0570.html

一旦我到办公室并使用办公室的机器,我就可以发布完整的代码片段。现在您可以查看http://www.rgagnon.com/javadetails/java-0570.html

#2


Yahoo! Mail SMTP server address: smtp.mail.yahoo.com
Yahoo! Mail SMTP user name: Your full Yahoo! Mail email address (including "@yahoo.com")
Yahoo! Mail SMTP password: Your Yahoo! Mail password
Yahoo! Mail SMTP port: 465
Yahoo! Mail SMTP TLS/SSL required: yes

雅虎邮件SMTP服务器地址:smtp.mail.yahoo.com Yahoo!邮件SMTP用户名:您的完整Yahoo!邮件地址(包括“@ yahoo.com”)Yahoo!邮件SMTP密码:您的Yahoo!邮件密码Yahoo!邮件SMTP端口:465 Yahoo!邮件SMTP TLS / SSL要求:是的

Similar settings work with gmail. For yahoo you might need yahoo plus account

类似的设置适用于gmail。对于雅虎,你可能需要雅虎加帐户

#3


It could be an issue with your ISP blocking port 25 traffic (not unusual!)

这可能是您的ISP阻止端口25流量的问题(并不罕见!)

From http://help.yahoo.com/l/us/yahoo/smallbusiness/bizmail/pop/pop-32.html:

In an attempt to control unsolicited email (spam), some Internet service providers now block port 25, which means you could experience technical problems when sending email. If you are having trouble sending email, you may need to use port 465 (recommended) or 587 when sending email via Yahoo!'s SMTP server.

为了控制未经请求的电子邮件(垃圾邮件),一些Internet服务提供商现在阻止端口25,这意味着您在发送电子邮件时可能会遇到技术问题。如果您在发送电子邮件时遇到问题,则在通过Yahoo!的SMTP服务器发送电子邮件时,您可能需要使用端口465(推荐)或587。

#1


I am not sure, but I faced the same problem when sending mail using a gmail id, you are using yahoo. The problem was gmail uses ssl layer protection, i think same is the case with yahoo so you need to use

我不确定,但是当我使用gmail id发送邮件时,我遇到了同样的问题,你使用的是雅虎。问题是gmail使用ssl层保护,我认为雅虎的情况也是如此,所以你需要使用

mail.smtps.host instead of mail.smtp.host

and same for other properties too.

和其他属性一样。

and isSSL to true.

并且isSSL为true。

I can post complete code snippet, once i reach office and use office's machine. For now you can look at http://www.rgagnon.com/javadetails/java-0570.html

一旦我到办公室并使用办公室的机器,我就可以发布完整的代码片段。现在您可以查看http://www.rgagnon.com/javadetails/java-0570.html

#2


Yahoo! Mail SMTP server address: smtp.mail.yahoo.com
Yahoo! Mail SMTP user name: Your full Yahoo! Mail email address (including "@yahoo.com")
Yahoo! Mail SMTP password: Your Yahoo! Mail password
Yahoo! Mail SMTP port: 465
Yahoo! Mail SMTP TLS/SSL required: yes

雅虎邮件SMTP服务器地址:smtp.mail.yahoo.com Yahoo!邮件SMTP用户名:您的完整Yahoo!邮件地址(包括“@ yahoo.com”)Yahoo!邮件SMTP密码:您的Yahoo!邮件密码Yahoo!邮件SMTP端口:465 Yahoo!邮件SMTP TLS / SSL要求:是的

Similar settings work with gmail. For yahoo you might need yahoo plus account

类似的设置适用于gmail。对于雅虎,你可能需要雅虎加帐户

#3


It could be an issue with your ISP blocking port 25 traffic (not unusual!)

这可能是您的ISP阻止端口25流量的问题(并不罕见!)

From http://help.yahoo.com/l/us/yahoo/smallbusiness/bizmail/pop/pop-32.html:

In an attempt to control unsolicited email (spam), some Internet service providers now block port 25, which means you could experience technical problems when sending email. If you are having trouble sending email, you may need to use port 465 (recommended) or 587 when sending email via Yahoo!'s SMTP server.

为了控制未经请求的电子邮件(垃圾邮件),一些Internet服务提供商现在阻止端口25,这意味着您在发送电子邮件时可能会遇到技术问题。如果您在发送电子邮件时遇到问题,则在通过Yahoo!的SMTP服务器发送电子邮件时,您可能需要使用端口465(推荐)或587。