当我试图从“smtpout.asia.secureserver.net”发送邮件时,它是从本地主机而不是“secureserver”主机发送的。下面是代码:
Properties props = new Properties();
props.put("mail.smtp.host","smtpout.asia.secureserver.net");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxx@xxxxx.net","xxxxx");
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
}
catt(MessagingExc的e){抛出新的Runtime异常(e);}
调试:
com.sun.mail.smtp.SMTPSendFailedException
550 <narasimhatejav@teja> Sender Rejected - MAILFROM must be a valid domain. Ensure the mailfrom domain: "teja" has a valid MX or A record.
DEBUG SMTP: got response code 550, with response: 550 <narasimhatejav@teja> Sender Rejected - MAILFROM must be a valid domain. Ensure the mailfrom domain: "teja" has a valid MX or A record.
但是如果我将主机更改为“smtp.gmail.com”,它就可以正常工作。
将Javamail Jar更改为v1.5,并添加javax.mail.internet.MimeMessage.set(字符串地址)函数
它是从本地主机发送的,因为您犯了大多数常见的JavaMail错误。
MAILFROM 问题是因为计算机上的主机名或名称服务配置错误。作为解决方法,您可以设置 mail.smtp.localhost
属性。