自己重新改的一个邮件程序有一个错误无法修改希望高手指点

gpx2046 2007-11-09 12:26:36
代码是
package mail;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
//import java.util.ArrayList;
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,";");
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);

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.send(mimemessage);
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("vv@126.com;cc@126.com",MailGroup.TO);//liulibo@use.com.cn
mail.setFromAddress("ss@sina.com");
mail.setSMTPHost("smtp.sina.com.cn","ss", "123456");
mail.setSubject("测试一下");
mail.setHtmlBody("");
mail.sendBatch(mail);
}
}
...全文
233 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gpx2046 2007-11-11
  • 打赏
  • 举报
回复
高手,希望可以多多指点我的QQ是342994854
cn_vi 2007-11-09
  • 打赏
  • 举报
回复
不好意思. 上面少了几行代码.
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("f.t@163.com;cn_vi@tom.com", MailGroup.TO);// liulibo@use.com.cn
// 要和登陆的邮箱一样
mail.setFromAddress("cn_tes_1@sina.com");
// 用户名, 密码要正确填写. 这是我测试用的邮箱, 可以使用
mail.setSMTPHost("smtp.sina.com", "cn_tes_1", "123456");
mail.setSubject("测试一下");
mail.setHtmlBody("test");
mail.sendBatch(mail);

}
}
cn_vi 2007-11-09
  • 打赏
  • 举报
回复
修改后的程序如下
--------------------------------------------

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("f.t@163.com;cn_vi@tom.com", MailGroup.TO);// liulibo@use.com.cn
// 要和登陆的邮箱一样
mail.setFromAddress("cn_tes_1@sina.com");
// 用户名, 密码要正确填写. 这是我测试用的邮箱, 可以使用
mail.setSMTPHost("smtp.sina.com", "cn_tes_1", "123456");
mail.setSubject("测试一下");
mail.setHtmlBody("test");
mail.sendBatch(mail);

}
}
cn_vi 2007-11-09
  • 打赏
  • 举报
回复
1. setAddress() 方法中
// String[] alAddress = StringHelper.split(strAddress, ";");
// 改成如下 ( 你写反了)
String[] alAddress = StringHelper.split(";", strAddress);
// 或
// String[] alAddress = strAddress.split(";");

就没有 No recipient addresses 异常了.

------------------------------

2. // 用户名, 密码要正确填写
mail.setFromAddress("cn_tes_1@sina.com");
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());

就可以了
gpx2046 2007-11-09
  • 打赏
  • 举报
回复
错误是:
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)


请各位指点,修改运行成功马上发分
zhoche2008 2007-11-09
  • 打赏
  • 举报
回复
好长.没写过.UP
gpx2046 2007-11-09
  • 打赏
  • 举报
回复
谢谢指点,我测试完马上给分

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧