当前位置: 技术问答>java相关
关于java mail
来源: 互联网 发布时间:2015-09-25
本文导语: 请问我的代码错在那里 import javax.mail.*; import javax.activation.*; import javax.mail.internet.*; import java.util.*; public class temp { public void send() { try { SmtpAuth sa = new SmtpAuth(); ...
请问我的代码错在那里
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
public class temp {
public void send() {
try {
SmtpAuth sa = new SmtpAuth();
sa.getuserinfo(user, password);
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp");
props.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(props, sa);
MimeMessage msg = new MimeMessage(session);
msg.setText("11");
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress("sgr"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("shiguanren@hsic.com.cn", false));
msg.setSubject("test");
Transport.send(msg);
}
catch(Exception ex) {
}
}
private class SmtpAuth extends Authenticator{
private String user,password;
public void getuserinfo(String getuser,String getpassword){
user=getuser;
password=getpassword;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
}
}
}
出错信息:No provider for Address type: rfc822
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
public class temp {
public void send() {
try {
SmtpAuth sa = new SmtpAuth();
sa.getuserinfo(user, password);
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp");
props.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(props, sa);
MimeMessage msg = new MimeMessage(session);
msg.setText("11");
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress("sgr"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("shiguanren@hsic.com.cn", false));
msg.setSubject("test");
Transport.send(msg);
}
catch(Exception ex) {
}
}
private class SmtpAuth extends Authenticator{
private String user,password;
public void getuserinfo(String getuser,String getpassword){
user=getuser;
password=getpassword;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
}
}
}
出错信息:No provider for Address type: rfc822
|
代码应该没问题
是 msg.setFrom(new InternetAddress("sgr"));
这句出错吗?
是 msg.setFrom(new InternetAddress("sgr"));
这句出错吗?
|
楼上的说的对,
new InternetAddress("sgr"));中的参数sgr应该是一个有效的邮件地址。你的是非法的,你该成如下:
new InternetAddress("sgr@sina.com"));
就可以了。
new InternetAddress("sgr"));中的参数sgr应该是一个有效的邮件地址。你的是非法的,你该成如下:
new InternetAddress("sgr@sina.com"));
就可以了。