java发送hotmail邮件,使用javamail将电子邮件发送到hotmail时遇到问题

Question: Has anyone ever successfully sent an email to a hotmail account through JavaMail from an SMTP server? 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. 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. This leads me to believe JavaMail leaves out a flag that hotmail seems to think is important. Using the Apache Commons JavaMail implementation produces the same results.

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. Using an intermediary SMTP server, like sendgrid, can solve the problem as they will do it for you automatically..at a cost.

You can also attempt to add the needed SPF and DKIM headers yourself.

解决方案

You can try using 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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值