尝试从Web应用程序发送邮件时遇到此异常:
com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Missing
or literal domains not allowed
我使用的代码如下代码。
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.verizon.net");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
我还使用身份验证方法使用用户名和密码对用户进行身份验证。
只有在我进行身份验证时才能获得成功消息。当我进入名为transport.sen(message)
的行时,我遇到了异常。
this is my full code..
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// message.addRecipient(Message.RecipientType.CC, new InternetAddress(
// cc));
// message.addRecipient(Message.RecipientType.BCC,
// new InternetAddress(bcc));
message.setSubject("TEST...!!!!!!!");
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart
.setText("Dear Sir, Mail Testing");
multipart.addBodyPart(messageBodyPart);
messageBodyPart.setText("Hao test");
message.setText("Kader here");
message.setContent(multipart);
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Transport transport = session.getTransport();
transport.connect();
Transport.send(message);
transport.close();
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
答案 0 :(得分:1)
有些邮件服务器可能会在客户端日志记录时自动将域名附加到用户名,但某些服务器不会,因此验证失败。
答案 1 :(得分:0)
Try this code :-
MailUtil.java
------------
package com.test;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* Please test with your GMAIL
* @author Jamsheer T
* +91-9846716175
*
*/
public class MailUtil {
private String SMTP_HOST = "smtp.gmail.com";
private String FROM_ADDRESS = "jamsheer568@gmail.com"; //Give Your gmail here
private String PASSWORD = "***********"; //Give Your password here
private String FROM_NAME = "Jamsheer T";
public boolean sendMail(String[] recipients, String[] bccRecipients, String subject,
String message) {
try {
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable","true");
Session session = Session.getInstance(props, new SocialAuth());
Message msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME);
msg.setFrom(from);
InternetAddress[] toAddresses = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
toAddresses[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, toAddresses);
InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length];
for (int j = 0; j < bccRecipients.length; j++) {
bccAddresses[j] = new InternetAddress(bccRecipients[j]);
}
msg.setRecipients(Message.RecipientType.BCC, bccAddresses);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
return true;
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (MessagingException ex) {
ex.printStackTrace();
Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
class SocialAuth extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM_ADDRESS, PASSWORD);
}
}
}
Main.java
----------------
package com.test;
public class Main {
public static void main(String[] args) {
String[] recipients = new String[]{"example1@gmail.com"}; //To
String[] bccRecipients = new String[]{"example2@gmail.com"}; //Bcc
String subject = "Test Mail"; //Subject
String messageBody = "Hi how r u?????"; //Body
System.out.print("Result"+new MailUtil().sendMail(recipients, bccRecipients,
subject, messageBody));
}
}