请前辈帮忙看一下,553 You are not authorized to send mail, authentication is required

java_shi 2010-10-04 02:51:19
package com.witbridge.util;

import java.awt.List;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.dom4j.DocumentException;


/**
*
* @author ShiDiwen

*
*/
public class MailSendtest {

String host="smtp.163.com";
String userName="shiguangcan_g@163.com";//可替换为你自己的邮箱用户名
String password="123456";//不好意思密码不能给你们看
String from = "84859195@qq.com"; //你要发送的邮箱名称

String to;//发给谁
String subject;//邮件议题
String body;//内容

String fileName;//附件路径(注意绝对路径和相对路径的选择)

// 用于保存发送附件的文件名列表
List arrFileName = new List();

public MailSendtest(String to, String subject, String body)
throws MalformedURLException, DocumentException {
//初始化发件人、收件人、主题等

this.to = to;
this.subject =subject;
this.body = body;
}

// 用于收集附件名
public void attachFile(String fileName) {
this.arrFileName.add(fileName);
}

// 开始发送
public boolean startSend() {

//创建Properties对象
Properties props = System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", this.host);
//props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true"); //通过验证
//得到默认的对话对象
Session session = Session.getDefaultInstance(props, null);
try {
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(this.from));



if (this.to.trim().length() > 0) {
String[] arr = this.to.split(",");
//int ReceiverCount=1;
int receiverCount = arr.length;
if (receiverCount > 0) {
InternetAddress[] address = new InternetAddress[receiverCount];
for (int i = 0; i < receiverCount; i++) {
address[i] = new InternetAddress(arr[i]);
}
msg.addRecipients(Message.RecipientType.TO, address);
} else {
System.out
.println("HI,你好,我是史光灿");
System.exit(0);
}
} else {
System.out.println("你是谁?");
System.exit(0);
}
msg.setSubject(subject);
//后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
//获取附件
int FileCount = this.arrFileName.getItemCount();
if (FileCount > 0) {
for (int i = 0; i < FileCount; i++) {
MimeBodyPart mbp = new MimeBodyPart();
//选择出附件名
fileName = arrFileName.getItem(i).toString();
//得到数据源
FileDataSource fds = new FileDataSource(fileName);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(this.body);
mp.addBodyPart(mbp);
//移走集合中的所有元素
arrFileName.removeAll();
//Multipart加入到信件
msg.setContent(mp);
} else {
//设置邮件正文
msg.setText(this.body);
}
//设置信件头的发送日期
msg.setSentDate(new Date());
//msg.saveChanges();
//发送信件
Transport transport = session.getTransport("smtp");
transport.connect(this.host, this.userName, this.password);
transport.sendMessage(msg , msg
.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
return false;
}
return true;
}

public static void main(String[] args) {

try {
MailSendtest sendmail = new MailSendtest("84859195@qq.com",
"邮件主题", "邮件内容");
sendmail.attachFile("");//附件地址
sendmail.attachFile("");//附件2地址;根据需要可继续添加....................
sendmail.startSend();
System.out.println("OK");
} catch (Exception e) {
e.printStackTrace();
}

}
}


553 You are not authorized to send mail, authentication is required
...全文
1192 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
飞跃颠峰 2011-06-22
没通过身份验证啊
邮箱密码不要输错了
回复
johnawm 2011-06-22
String host="smtp.163.com";
String userName="shiguangcan_g@163.com";//可替换为你自己的邮箱用户名
String password="123456";//不好意思密码不能给你们看
String from = "84859195@qq.com"; //你要发送的邮箱名称

host与from需要一致,即from的内容改成163的邮箱
回复
thegodofwar 2010-10-04
根据报的错,应该是你的权限不够
回复
java_shi 2010-10-04
刚写JAVA 程序,请前辈多多指点,我表示忠心感谢!!
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-04 02:51
社区公告
暂无公告