简体   繁体   English

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

[英]Unable to send mail from Java application

In my java application I need to send mails to different mail addresses. 在我的Java应用程序中,我需要将邮件发送到其他邮件地址。 I am using the next piece of code , but for some reason it doesn't work. 我正在使用下一段代码,但是由于某种原因它无法正常工作。

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 ? 谁能告诉我,我在这里做错了吗?

Yahoo! 雅虎! Mail SMTP server address: smtp.mail.yahoo.com 邮件SMTP服务器地址:smtp.mail.yahoo.com
Yahoo! 雅虎! Mail SMTP user name: Your full Yahoo! 邮件SMTP用户名:您的完整Yahoo! Mail email address (including "@yahoo.com") 邮件电子邮件地址(包括“ @ yahoo.com”)
Yahoo! 雅虎! Mail SMTP password: Your Yahoo! 邮件SMTP密码:您的Yahoo! Mail password 邮件密码
Yahoo! 雅虎! Mail SMTP port: 465 邮件SMTP端口:465
Yahoo! 雅虎! Mail SMTP TLS/SSL required: yes 需要邮件SMTP TLS / SSL:是

Similar settings work with gmail. gmail可以使用类似的设置。 For yahoo you might need yahoo plus account 对于yahoo,您可能需要yahoo plus帐户

I am not sure, but I faced the same problem when sending mail using a gmail id, you are using yahoo. 我不确定,但是使用gmail ID发送邮件时遇到了同样的问题,而您使用的是yahoo。 The problem was gmail uses ssl layer protection, i think same is the case with yahoo so you need to use 问题是gmail使用ssl层保护,我认为Yahoo也是如此,因此您需要使用

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

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 : 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. 为了控制不请自来的电子邮件(垃圾邮件),某些Internet服务提供商现在阻止了端口25,这意味着您在发送电子邮件时可能会遇到技术问题。 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. 如果您在发送电子邮件时遇到问题,则在通过Yahoo!的SMTP服务器发送电子邮件时,可能需要使用端口465(推荐)或587。

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

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