java邮件API异常
尝试从web应用程序发送邮件时出现此异常:java邮件API异常,java,email,Java,Email,尝试从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", "
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");
我还使用authenticate方法使用用户名和密码对用户进行身份验证
我只有在进行身份验证时才收到成功消息。当我转到名为transport.sen(message)
的行时,出现异常
有些邮件服务器可能会在客户端登录时自动将域名附加到用户名,但有些服务器不会,因此身份验证失败。有些邮件服务器可能会在客户端登录时自动将域名附加到用户名,但有些服务器不会,因此,身份验证失败。
请尝试以下代码:-
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));
}
}
MailUtil.java
------------
包com.test;
导入java.io.UnsupportedEncodingException;
导入java.util.Properties;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.mail.Authenticator;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
/**
*请用你的GMAIL进行测试
*@author jamt
* +91-9846716175
*
*/
公共类MailUtil{
私有字符串SMTP_HOST=“SMTP.gmail.com”;
来自_地址的私有字符串=”jamsheer568@gmail.com“;//把你的gmail给我
私有字符串密码=“**********”;//在此处输入密码
来自_NAME=“jamt”的私有字符串;
公共布尔sendMail(字符串[]收件人,字符串[]b收件人,字符串主题,
字符串消息){
试试{
Properties props=新属性();
props.put(“mail.smtp.host”,smtp\u host);
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.debug”、“false”);
put(“mail.smtp.ssl.enable”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
Session=Session.getInstance(props,new SocialAuth());
Message msg=新的mimessage(会话);
InternetAddress from=新的InternetAddress(from\u地址,from\u名称);
msg.setFrom(from);
InternetAddress[]ToAddresss=新的InternetAddress[recipients.length];
对于(int i=0;i
尝试以下代码:-
MailUtil.java
------------
包com.test;
导入java.io.UnsupportedEncodingException;
导入java.util.Properties;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.mail.Authenticator;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
/**
*请用你的GMAIL进行测试
*@author jamt
* +91-9846716175
*
*/
公共类MailUtil{
私有字符串SMTP_HOST=“SMTP.gmail.com”;
来自_地址的私有字符串=”jamsheer568@gmail.com“;//把你的gmail给我
私有字符串密码=“**********”;//在此处输入密码
来自_NAME=“jamt”的私有字符串;
公共布尔sendMail(字符串[]收件人,字符串[]b收件人,字符串主题,
字符串消息){
试试{
Properties props=新属性();
props.put(“mail.smtp.host”,smtp\u host);
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.debug”、“false”);
put(“mail.smtp.ssl.enable”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
Session=Session.getInstance(props,new SocialAuth());
Message msg=新的mimessage(会话);
InternetAddress from=新的InternetAddress(from\u地址,from\u名称);
msg.setFrom(from);
InternetAddress[]ToAddresss=新的InternetAddress[recipients.length];
对于(int i=0;iTry 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));
}
}