错误是:
Exception in thread "main" javax.mail.SendFailedException: No recipient addresses
at javax.mail.Transport.send0(Transport.java:128)
at javax.mail.Transport.send(Transport.java:98)
at mail.MailGroup.sendBatch(MailGroup.java:216)
at mail.MailGroup.main(MailGroup.java:248)
请各位指点,修改运行成功马上发分
Exception in thread "main" javax.mail.SendFailedException: No recipient addresses
at javax.mail.Transport.send0(Transport.java:128)
at javax.mail.Transport.send(Transport.java:98)
at mail.MailGroup.sendBatch(MailGroup.java:216)
at mail.MailGroup.main(MailGroup.java:248)
请各位指点,修改运行成功马上发分
// String[] alAddress = StringHelper.split(strAddress, ";");
// 改成如下 ( 你写反了)
String[] alAddress = StringHelper.split(";", strAddress);
// 或
// String[] alAddress = strAddress.split(";");就没有 No recipient addresses 异常了. ------------------------------2. // 用户名, 密码要正确填写
mail.setFromAddress("[email protected]");
mail.setSMTPHost("smtp.sina.com", "cn_tes_1", "123456");------------------------------3. 这时还会报如下异常
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at cn.z.exercise.mail.MailGroup.sendBatch(MailGroup.java:250)
at cn.z.exercise.mail.MailGroup.main(MailGroup.java:279)------------------------------// Transport.send(mimemessage, mimemessage.getAllRecipients());
// 改成下面的
transport.sendMessage(mimemessage, mimemessage.getAllRecipients());
就可以了
--------------------------------------------package cn.z.exercise.mail;import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;import org.hibernate.util.StringHelper;public class MailGroup {
/** 发件方式 - 普通发送 */
final public static int TO = 0; /** 发件方式 - 抄送 */
final public static int CC = 1; /** 发件方式 - 密件抄送 */
final public static int BCC = 2; /** 邮件相关信息 - SMTP 服务器 */
private String mailSMTPHost = null; /** 邮件相关信息 - 邮件用户名 */
private String mailUser = null; /** 邮件相关信息 - 密码 */
private String mailPassword = null; /** 邮件相关信息 - 发件人邮件地址 */
private String mailFromAddress = null; /** 邮件相关信息 - 邮件主题 */
private String mailSubject = ""; /** 邮件相关信息 - 邮件发送地址 */
private Address[] mailTOAddress = null; /** 邮件相关信息 - 邮件抄送地址 */
private Address[] mailCCAddress = null; /** 邮件相关信息 - 邮件密件抄送地址 */
private Address[] mailBCCAddress = null; /** 邮件相关信息 - 邮件正文(复合结构) */
private MimeMultipart mailBody = null; public MailGroup() {
mailBody = new MimeMultipart();
} /**
* 设置 SMTP 服务器
*
* @param strSMTPHost
* 邮件服务器名称或 IP
* @param strUser
* 邮件用户名
* @param strPassword
* 密码
*/
public void setSMTPHost(String strSMTPHost, String strUser,
String strPassword) {
this.mailSMTPHost = strSMTPHost;
this.mailUser = strUser;
this.mailPassword = strPassword;
} /**
* 设置邮件发送地址
*
* @param strFromAddress
* 邮件发送地址
*/
public void setFromAddress(String strFromAddress) {
this.mailFromAddress = strFromAddress;
} /**
* 设置邮件目的地址(支持群发)
*
* @param strAddress
* 邮件目的地址列表, 不同的地址可用;号分隔
* @param iAddressType
* 邮件发送方式 (TO 0, CC 1, BCC 2) 常量已在本类定义
* @throws AddressException
*/
public void setAddress(String strAddress, int iAddressType)
throws AddressException {
switch (iAddressType) {
case MailGroup.TO: {
// String[] alAddress = StringHelper.split(strAddress, ";");
// 改成如下
String[] alAddress = StringHelper.split(";", strAddress);
// 或
// String[] alAddress = strAddress.split(";"); mailTOAddress = new Address[alAddress.length];
for (int i = 0; i < alAddress.length; i++) {
mailTOAddress[i] = new InternetAddress(alAddress[i]);
}
break;
}
case MailGroup.CC: {
String[] alAddress = StringHelper.split(strAddress, ";");
mailCCAddress = new Address[alAddress.length];
for (int i = 0; i < alAddress.length; i++) {
mailCCAddress[i] = new InternetAddress(alAddress[i]);
}
break;
}
case MailGroup.BCC: {
String[] alAddress = StringHelper.split(strAddress, ";");
mailBCCAddress = new Address[alAddress.length];
for (int i = 0; i < alAddress.length; i++) {
mailBCCAddress[i] = new InternetAddress(alAddress[i]);
}
break;
}
}
} /**
* 设置邮件主题
*
* @param strSubject
* 邮件主题
*/
public void setSubject(String strSubject) {
this.mailSubject = strSubject;
} /**
* 设置邮件文本正文
*
* @param strTextBody
* 邮件文本正文
* @throws MessagingException
*/
public void setTextBody(String strTextBody) throws MessagingException {
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setText(strTextBody, "GBK");
mailBody.addBodyPart(mimebodypart);
} /**
* 设置邮件超文本正文
*
* @param strHtmlBody
* 邮件超文本正文
* @throws MessagingException
*/
public void setHtmlBody(String strHtmlBody) throws MessagingException {
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setDataHandler(new DataHandler(strHtmlBody,
"text/html;charset=GBK"));
mailBody.addBodyPart(mimebodypart);
} /**
* 设置邮件正文外部链接 URL, 信体中将包含链接所指向的内容
*
* @param strURLAttachment
* 邮件正文外部链接 URL
* @throws MessagingException
* @throws MalformedURLException
*/
public void setURLAttachment(String strURLAttachment)
throws MessagingException, MalformedURLException {
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setDataHandler(new DataHandler(new URL(strURLAttachment)));
mailBody.addBodyPart(mimebodypart);
} /**
* 设置邮件附件
*
* @param strFileAttachment
* 文件的全路径
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public void setFileAttachment(String strFileAttachment)
throws MessagingException, UnsupportedEncodingException {
File path = new File(strFileAttachment);
if (!path.exists() || path.isDirectory()) {
return;
}
String strFileName = path.getName();
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setDataHandler(new DataHandler(new FileDataSource(
strFileAttachment)));
mimebodypart.setFileName(MimeUtility.encodeText(strFileName));
mailBody.addBodyPart(mimebodypart);
} /**
* 邮件发送(一次发送多个地址, 优点速度快, 但是有非法邮件地址时将中断发送操作)
*
* @throws MessagingException
*/
public void sendBatch(MailGroup mail) throws MessagingException {
Properties properties = new Properties();
properties.put("mail.smtp.host", mail.mailSMTPHost);
properties.put("socksProxyHost", "196.56.17.26");// 设置ie代理
properties.put("socksProxyPort", "8080");// IE的代理端口
properties.put("mail.smtp.auth", "true");// 指定是否需要SMTP验证 // #### 我网速慢
// properties.put("mail.smtp.connectiontimeout", "10000");
// properties.put("mail.smtp.timeout", "10000"); Session session = Session.getInstance(properties, null); // ####打开调试模式
session.setDebug(true); MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setFrom(new InternetAddress(this.mailFromAddress));
if (mailTOAddress != null) {
mimemessage.addRecipients(javax.mail.Message.RecipientType.TO,
this.mailTOAddress);
}
if (mailCCAddress != null) {
mimemessage.addRecipients(javax.mail.Message.RecipientType.CC,
this.mailCCAddress);
}
if (mailBCCAddress != null) {
mimemessage.addRecipients(javax.mail.Message.RecipientType.BCC,
this.mailBCCAddress);
}
mimemessage.setSubject(this.mailSubject);
mimemessage.setContent(this.mailBody);
mimemessage.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect(this.mailSMTPHost, this.mailUser, this.mailPassword);
if (mailTOAddress != null) {
for (int i = 0; i < mailTOAddress.length; i++) {
System.out.println(mailTOAddress[i]);
}
}
if (mailCCAddress != null) {
for (int i = 0; i < mailTOAddress.length; i++) {
System.out.println(mailCCAddress[i]);
}
}
if (mailBCCAddress != null) {
for (int i = 0; i < mailTOAddress.length; i++) {
System.out.println(mailBCCAddress[i]);
}
} } static public void main(String str[]) throws MessagingException {
MailGroup mail = new MailGroup();
mail.setAddress("[email protected];[email protected]", MailGroup.TO);// [email protected]
// 要和登陆的邮箱一样
mail.setFromAddress("[email protected]");
// 用户名, 密码要正确填写. 这是我测试用的邮箱, 可以使用
mail.setSMTPHost("smtp.sina.com", "cn_tes_1", "123456");
mail.setSubject("测试一下");
mail.setHtmlBody("test");
mail.sendBatch(mail); }
}
transport.sendMessage(mimemessage, mimemessage.getAllRecipients());
//Transport.send(mimemessage, mimemessage.getAllRecipients());
System.out.println("已向下列邮箱发送了邮件");
-----------------------------------------------
正确代码如下
import ****(写不下了)
import org.hibernate.util.StringHelper;public class MailGroup {
/** 发件方式 - 普通发送 */
final public static int TO = 0; /** 发件方式 - 抄送 */
final public static int CC = 1; /** 发件方式 - 密件抄送 */
final public static int BCC = 2; /** 邮件相关信息 - SMTP 服务器 */
private String mailSMTPHost = null; /** 邮件相关信息 - 邮件用户名 */
private String mailUser = null; /** 邮件相关信息 - 密码 */
private String mailPassword = null; /** 邮件相关信息 - 发件人邮件地址 */
private String mailFromAddress = null; /** 邮件相关信息 - 邮件主题 */
private String mailSubject = ""; /** 邮件相关信息 - 邮件发送地址 */
private Address[] mailTOAddress = null; /** 邮件相关信息 - 邮件抄送地址 */
private Address[] mailCCAddress = null; /** 邮件相关信息 - 邮件密件抄送地址 */
private Address[] mailBCCAddress = null; /** 邮件相关信息 - 邮件正文(复合结构) */
private MimeMultipart mailBody = null; public MailGroup() {
mailBody = new MimeMultipart();
} /**
* 设置 SMTP 服务器
*
* @param strSMTPHost
* 邮件服务器名称或 IP
* @param strUser
* 邮件用户名
* @param strPassword
* 密码
*/
public void setSMTPHost(String strSMTPHost, String strUser,
String strPassword) {
this.mailSMTPHost = strSMTPHost;
this.mailUser = strUser;
this.mailPassword = strPassword;
} /**
* 设置邮件发送地址
*
* @param strFromAddress
* 邮件发送地址
*/
public void setFromAddress(String strFromAddress) {
this.mailFromAddress = strFromAddress;
} /**
* 设置邮件目的地址(支持群发)
*
* @param strAddress
* 邮件目的地址列表, 不同的地址可用;号分隔
* @param iAddressType
* 邮件发送方式 (TO 0, CC 1, BCC 2) 常量已在本类定义
* @throws AddressException
*/
public void setAddress(String strAddress, int iAddressType)
throws AddressException {
switch (iAddressType) {
case MailGroup.TO: {
// String[] alAddress = StringHelper.split(strAddress, ";");
// 改成如下
String[] alAddress = StringHelper.split(";", strAddress);
// 或
// String[] alAddress = strAddress.split(";"); mailTOAddress = new Address[alAddress.length];
for (int i = 0; i < alAddress.length; i++) {
mailTOAddress[i] = new InternetAddress(alAddress[i]);
}
break;
}
case MailGroup.CC: {
String[] alAddress = StringHelper.split(strAddress, ";");
mailCCAddress = new Address[alAddress.length];
for (int i = 0; i < alAddress.length; i++) {
mailCCAddress[i] = new InternetAddress(alAddress[i]);
}
break;
}
case MailGroup.BCC: {
String[] alAddress = StringHelper.split(strAddress, ";");
mailBCCAddress = new Address[alAddress.length];
for (int i = 0; i < alAddress.length; i++) {
mailBCCAddress[i] = new InternetAddress(alAddress[i]);
}
break;
}
}
} /**
* 设置邮件主题
*
* @param strSubject
* 邮件主题
*/
public void setSubject(String strSubject) {
this.mailSubject = strSubject;
} /**
* 设置邮件文本正文
*
* @param strTextBody
* 邮件文本正文
* @throws MessagingException
*/
public void setTextBody(String strTextBody) throws MessagingException {
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setText(strTextBody, "GBK");
mailBody.addBodyPart(mimebodypart);
} /**
* 设置邮件超文本正文
*
* @param strHtmlBody
* 邮件超文本正文
* @throws MessagingException
*/
public void setHtmlBody(String strHtmlBody) throws MessagingException {
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setDataHandler(new DataHandler(strHtmlBody,
"text/html;charset=GBK"));
mailBody.addBodyPart(mimebodypart);
} /**
* 设置邮件正文外部链接 URL, 信体中将包含链接所指向的内容
*
* @param strURLAttachment
* 邮件正文外部链接 URL
* @throws MessagingException
* @throws MalformedURLException
*/
public void setURLAttachment(String strURLAttachment)
throws MessagingException, MalformedURLException {
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setDataHandler(new DataHandler(new URL(strURLAttachment)));
mailBody.addBodyPart(mimebodypart);
} /**
* 设置邮件附件
*
* @param strFileAttachment
* 文件的全路径
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public void setFileAttachment(String strFileAttachment)
throws MessagingException, UnsupportedEncodingException {
File path = new File(strFileAttachment);
if (!path.exists() || path.isDirectory()) {
return;
}
String strFileName = path.getName();
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setDataHandler(new DataHandler(new FileDataSource(
strFileAttachment)));
mimebodypart.setFileName(MimeUtility.encodeText(strFileName));
mailBody.addBodyPart(mimebodypart);
} /**
* 邮件发送(一次发送多个地址, 优点速度快, 但是有非法邮件地址时将中断发送操作)
*
* @throws MessagingException
*/
public void sendBatch(MailGroup mail) throws MessagingException {
Properties properties = new Properties();
properties.put("mail.smtp.host", mail.mailSMTPHost);
properties.put("socksProxyHost", "196.56.17.26");// 设置ie代理
properties.put("socksProxyPort", "8080");// IE的代理端口
properties.put("mail.smtp.auth", "true");// 指定是否需要SMTP验证 // #### 我网速慢
// properties.put("mail.smtp.connectiontimeout", "10000");
// properties.put("mail.smtp.timeout", "10000"); Session session = Session.getInstance(properties, null); // ####打开调试模式
session.setDebug(true); MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setFrom(new InternetAddress(this.mailFromAddress));
if (mailTOAddress != null) {
mimemessage.addRecipients(javax.mail.Message.RecipientType.TO,
this.mailTOAddress);
}
if (mailCCAddress != null) {
mimemessage.addRecipients(javax.mail.Message.RecipientType.CC,
this.mailCCAddress);
}
if (mailBCCAddress != null) {
mimemessage.addRecipients(javax.mail.Message.RecipientType.BCC,
this.mailBCCAddress);
}
mimemessage.setSubject(this.mailSubject);
mimemessage.setContent(this.mailBody);
mimemessage.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect(this.mailSMTPHost, this.mailUser, this.mailPassword);
transport.sendMessage(mimemessage, mimemessage.getAllRecipients());
//Transport.send(mimemessage, mimemessage.getAllRecipients());
System.out.println("已向下列邮箱发送了邮件");
if (mailTOAddress != null) {
for (int i = 0; i < mailTOAddress.length; i++) {
System.out.println(mailTOAddress[i]);
}
}
if (mailCCAddress != null) {
for (int i = 0; i < mailTOAddress.length; i++) {
System.out.println(mailCCAddress[i]);
}
}
if (mailBCCAddress != null) {
for (int i = 0; i < mailTOAddress.length; i++) {
System.out.println(mailBCCAddress[i]);
}
} } static public void main(String str[]) throws MessagingException {
MailGroup mail = new MailGroup();
mail.setAddress("[email protected];[email protected]", MailGroup.TO);// [email protected]
// 要和登陆的邮箱一样
mail.setFromAddress("[email protected]");
// 用户名, 密码要正确填写. 这是我测试用的邮箱, 可以使用
mail.setSMTPHost("smtp.sina.com", "cn_tes_1", "123456");
mail.setSubject("测试一下");
mail.setHtmlBody("test");
mail.sendBatch(mail); }
}