//简单的文本邮件发送package SMTP;
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.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;public class XuejiaoEmail {
private String hosts;
private String sendforms;
private String passwords;
private String tosmail;
public XuejiaoEmail(){
this.hosts="";
this.sendforms="";
this.passwords="";
this.tosmail="";
}public void send_one_Mail(String host,String sendfrom,String password,String to){
this.hosts=host;
this.sendforms=sendfrom;
this.passwords=password;
this.tosmail=to;
Properties pro=System.getProperties();
pro.put("mail.smtp.host",hosts);
pro.put("mail.smtp.auth", "true");
Session session=Session.getDefaultInstance(pro, new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(sendforms, passwords);
}
});
try {
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(this.sendforms));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.tosmail));
message.setSubject("自己的邮件测试");
message.setText("这只是个简单的文本邮件");
Transport.send(message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
XuejiaoEmail xj=new XuejiaoEmail();
xj.send_one_Mail("mail.qq.com", "[email protected]", "QQ密 码", "[email protected]");
}
}
抱错咯 如下:250 Ok
com.sun.mail.smtp.SMTPSendFailedException: 501 mail from address must be same as authorization userException in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 501 mail from address must be same as authorization user
求高手帮忙!!
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.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;public class XuejiaoEmail {
private String hosts;
private String sendforms;
private String passwords;
private String tosmail;
public XuejiaoEmail(){
this.hosts="";
this.sendforms="";
this.passwords="";
this.tosmail="";
}public void send_one_Mail(String host,String sendfrom,String password,String to){
this.hosts=host;
this.sendforms=sendfrom;
this.passwords=password;
this.tosmail=to;
Properties pro=System.getProperties();
pro.put("mail.smtp.host",hosts);
pro.put("mail.smtp.auth", "true");
Session session=Session.getDefaultInstance(pro, new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(sendforms, passwords);
}
});
try {
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(this.sendforms));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.tosmail));
message.setSubject("自己的邮件测试");
message.setText("这只是个简单的文本邮件");
Transport.send(message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
XuejiaoEmail xj=new XuejiaoEmail();
xj.send_one_Mail("mail.qq.com", "[email protected]", "QQ密 码", "[email protected]");
}
}
抱错咯 如下:250 Ok
com.sun.mail.smtp.SMTPSendFailedException: 501 mail from address must be same as authorization userException in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 501 mail from address must be same as authorization user
求高手帮忙!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货