简体   繁体   English

无法使用javamail向hotmail发送电子邮件

[英]Trouble sending email to hotmail using javamail

Question: Has anyone ever successfully sent an email to a hotmail account through JavaMail from an SMTP server? 问题:有没有人通过JavaMail从SMTP服务器成功发送电子邮件到hotmail帐户? If so could you put up the code that worked? 如果是这样,你能提出有效的代码吗?

I can send emails to gmail and yahoo accounts using my JavaMail code but I can not send any emails to hotmail accounts. 我可以使用我的JavaMail代码向gmail和yahoo帐户发送电子邮件,但我无法向hotmail帐户发送任何电子邮件。 If I use my phone or another email client and use the same SMTP server as my JavaMail code then I can indeed send emails to hotmail accounts. 如果我使用我的手机或其他电子邮件客户端并使用与我的JavaMail代码相同的SMTP服务器,那么我确实可以向hotmail帐户发送电子邮件。 This leads me to believe JavaMail leaves out a flag that hotmail seems to think is important. 这让我相信JavaMail会遗漏一个hotmail似乎认为很重要的标志。 Using the Apache Commons JavaMail implementation produces the same results. 使用Apache Commons JavaMail实现会产生相同的结果。

        try{
            Email email = new SimpleEmail();
            email.setSmtpPort(Integer.parseInt(port));
            email.setAuthenticator(new DefaultAuthenticator(from,  MyUtilities.getSystemPWD(from)));
            email.setDebug(true);
            email.setHostName(host);
            email.setFrom(from);
            email.setSubject(subject);
            email.setMsg("test");                
            email.addTo(to);                
            email.setStartTLSRequired(true);
            email.send();    
        } catch(Exception ex){
              MyLogger.log("MyUtilities.sendEmail: Messaging error",ex);
              Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", ex);                
        }

Answer: There is an accepted answer below but the underlying cause of the problem is that Hotmail requires extra authentication headers (SPF & DKIM) to prove the domain name of your from address is associated with the SMTP server. 答:下面有一个接受的答案,但问题的根本原因是Hotmail需要额外的身份验证标头(SPF和DKIM)来证明您的起始地址的域名与SMTP服务器相关联。 Using an intermediary SMTP server, like sendgrid, can solve the problem as they will do it for you automatically..at a cost. 使用中间SMTP服务器,如sendgrid,可以解决问题,因为他们会自动为您完成这项工作。

You can also attempt to add the needed SPF and DKIM headers yourself. 您也可以尝试自己添加所需的SPF和DKIM标头。

You can try using sendgrid. 您可以尝试使用sendgrid。 I just tested it out and if you use legitimate email addresse for the from, it seems to work. 我刚刚测试了它,如果你使用合法的电子邮件地址,它似乎工作。

    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
    import java.util.Properties;

    public class SimpleMail {

        private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
        private static final String SMTP_AUTH_USER = "sendgrid-username";
        private static final String SMTP_AUTH_PWD  = "sendgrid-password";

        public static void main(String[] args) throws Exception{
           new SimpleMail().test();
        }

        public void test() throws Exception{
            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtp.port", 587);
            props.put("mail.smtp.auth", "true");

            Authenticator auth = new SMTPAuthenticator();
            Session mailSession = Session.getDefaultInstance(props, auth);
            // uncomment for debugging infos to stdout
            // mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();

            MimeMessage message = new MimeMessage(mailSession);

            Multipart multipart = new MimeMultipart("alternative");

            BodyPart part1 = new MimeBodyPart();
            part1.setText("Checking to see what box this mail goes in ?");

            BodyPart part2 = new MimeBodyPart();
            part2.setContent("Checking to see what box this mail goes in ?", "text/html");

            multipart.addBodyPart(part1);
            multipart.addBodyPart(part2);

            message.setContent(multipart);
            message.setFrom(new InternetAddress("actual@emailaddress-goeshere.com"));
            message.setSubject("Can you see this mail ?");
            message.addRecipient(Message.RecipientType.TO,
                 new InternetAddress("person@tosendto.com"));

            transport.connect();
            transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
            transport.close();
        }

        private class SMTPAuthenticator extends javax.mail.Authenticator {
            public PasswordAuthentication getPasswordAuthentication() {
               String username = SMTP_AUTH_USER;
               String password = SMTP_AUTH_PWD;
               return new PasswordAuthentication(username, password);
            }
        }
    }

If you have code that works for sending to any other internet email address, it should work for sending to Hotmail too. 如果您的代码适用于发送到任何其他Internet电子邮件地址,则它也适用于发送到Hotmail。

If you don't have code that works in general, see the JavaMail sample code and the JavaMail FAQ . 如果您没有一般工作的代码,请参阅JavaMail示例代码JavaMail FAQ

If you're trying to use Hotmail as your SMTP server, see the JavaMail FAQ . 如果您尝试使用Hotmail作为SMTP服务器,请参阅JavaMail FAQ

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

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