java发邮件出错了,帮忙看下~~~~

bmj 2007-01-03 02:15:17
按书上说的写了个邮件发送的小类,可老是发不出去,大家帮忙看看下.一起两个类,MailSedn.java是发送的,s.java是主类,代码如下:
//MailSend.java:

import java.io.*;
import java.text.*;
import java.net.*;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;

public class MailSend {
String subject = null,text = null,mailhost = null;
public MailSend(){
mailhost = "localhost";
text = getUserText();
subject = "Welcome~";
}
public String sednMsg(String from,String to){
boolean debug = false;
String err = null;
try{
Properties props = System.getProperties();
if(mailhost!=null)
props.put("mail.smtp.host", mailhost);
Session session = Session.getDefaultInstance(props,null);
if(debug)
session.setDebug(true);
Message msg = new MimeMessage(session);

//from = null;
if(from != null){
msg.setFrom(new InternetAddress(from));
System.out.print("from is not null~");
}
else{
msg.setFrom();
System.out.print("from is null~");
}
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,false));

msg.setSubject(subject);
msg.setText(text);

msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Mail has been send successfully!");

}
catch (Exception e){
e.printStackTrace();
err = e.toString();
err = "Mail send has an error!";
}
return err;
}

public String getUserText(){
String userText = "";
String nowDate = DateFormat.getDateInstance().format(new Date());
userText = "hello~";
return userText;
}

}


//S.java


public class S {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String mail = new String("suhuajun2001@163.com");
MailSend ms = new MailSend();
ms.sednMsg("shj0717@gmail.com", "suhuajun2001@163.com");
//ms.sednMsg("", mail);

}

}


错误提示如下:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for suhuajun2001@163.com

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1196)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:584)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at server.MailSend.sednMsg(MailSend.java:47)
at server.S.main(S.java:12)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for suhuajun2001@163.com

大家看看是什么原因.还有,这个要用到的jar文件(mail.java,activation.java)我都包进去了~~~


...全文
639 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bmj 2007-01-04
  • 打赏
  • 举报
回复
问题解决了,十分感谢~~~~~
Nuage 2007-01-03
  • 打赏
  • 举报
回复
/*
* 创建日期 2006-11-28
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.day1124;

/**
* @author dengjiang
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMail {

public SendMail() {
}

public void send() {
try {
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com"); //smtp主机名。
props.put("mail.smtp.user", "test@163.com"); //发送方邮件地址。
props.put("mail.smtp.password", "111111"); //邮件密码。
PopupAuthenticator popA = new PopupAuthenticator();//邮件安全认证。
PasswordAuthentication pop = popA.performCheck("test", "11111"); //填写用户名及密码
sendMailSession = Session.getInstance(props, popA);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("test@163.com"));
newMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress("luozonghang@163.com")); //接收方邮件地址
String subject="邮件主题subject";
String tmp=new String(subject.getBytes("GBK"));
newMessage.setSubject(tmp);
newMessage.setSentDate(new Date());
String mailContent;
mailContent = "你好hellow";
mailContent += "\t邮件正文content\n\n";
String content1="hrrhrhrhrhhhhhhhhhhhhhhhhhhhhhhhhh";

mailContent += new Date().toLocaleString();
newMessage.setText(new String(content1.getBytes("big5"))); //邮件正文
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
} catch (MessagingException ex) {
ex.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

public static void main(String[] args) {
SendMail sml = new SendMail();
sml.send();
}

public class PopupAuthenticator extends Authenticator {
String username = null;

String password = null;

public PopupAuthenticator() {
}

public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
}


有邮件地址的地方改成你的邮箱即可。
Nuage 2007-01-03
  • 打赏
  • 举报
回复
应该要设置你的密码才能发啊,mailhost 也不对吧,

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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