当前位置: 技术问答>java相关
一个smtp的问题,请高手看看
来源: 互联网 发布时间:2017-04-02
本文导语: import javax.activation.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; public class TestSmtp{ private String smtp="smtp.163.com"; private String subject="Try"; private String to="hopeai@sina.com";//目的信箱 private String user="zhiweiqiu"...
import javax.activation.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public class TestSmtp{
private String smtp="smtp.163.com";
private String subject="Try";
private String to="hopeai@sina.com";//目的信箱
private String user="zhiweiqiu";//我的163信箱用户名
private String pass="010504";//我的163信箱密码
private String body="This is the mail body";
public boolean sendMail(){
//发送一个邮件
try {
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
Sessionsess=Session.getDefaultInstance (props,null);
sess.setDebug(true);
MimeMessage msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(smtp));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
System.out.println("Date set");
Transport transport = sess.getTransport("smtp");
System.out.println("get transport");
transport.connect(smtp,user,pass);
System.out.println("connect build");
transport.send(msg);
transport.close();
return true;
}
catch (Exception e) {
return false;
}
}
public static void main(String []args){
TestSmtp sender=new TestSmtp();
boolean flag=sender.sendMail();
if(flag)
System.out.println("send successfully");
else
System.out.println("send fault");
}
}
上面这个程序可以执行到transport.connect(smtp,user,pass);
然后便不能抛出异常了,不知道什么问题,请高手指点,谢谢
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public class TestSmtp{
private String smtp="smtp.163.com";
private String subject="Try";
private String to="hopeai@sina.com";//目的信箱
private String user="zhiweiqiu";//我的163信箱用户名
private String pass="010504";//我的163信箱密码
private String body="This is the mail body";
public boolean sendMail(){
//发送一个邮件
try {
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
Sessionsess=Session.getDefaultInstance (props,null);
sess.setDebug(true);
MimeMessage msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(smtp));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
System.out.println("Date set");
Transport transport = sess.getTransport("smtp");
System.out.println("get transport");
transport.connect(smtp,user,pass);
System.out.println("connect build");
transport.send(msg);
transport.close();
return true;
}
catch (Exception e) {
return false;
}
}
public static void main(String []args){
TestSmtp sender=new TestSmtp();
boolean flag=sender.sendMail();
if(flag)
System.out.println("send successfully");
else
System.out.println("send fault");
}
}
上面这个程序可以执行到transport.connect(smtp,user,pass);
然后便不能抛出异常了,不知道什么问题,请高手指点,谢谢
|
import javax.activation.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class TestSmtp{
private String smtp="smtp.163.com";
private String subject="Try";
private String to="xxxxxx";//目的信箱
private String user="xxxxxxx";//我的163信箱用户名
private String pass="xxxxxxxxxx";//我的163信箱密码
private String body="This is the mail body";
private class SmtpAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication
(user, pass);
}
}
public boolean sendMail(){
//发送一个邮件
try {
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtp);
Session sess = Session.getDefaultInstance(props, new SmtpAuthenticator());
// Session sess=Session.getDefaultInstance (props,null);
sess.setDebug(true);
MimeMessage msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(smtp));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
System.out.println("Date set");
Transport transport = sess.getTransport("smtp");
System.out.println("get transport");
transport.connect(smtp,user,pass);
System.out.println("connect build");
transport.send(msg);
transport.close();
return true;
}
catch (Exception e) {
return false;
}
}
public static void main(String []args){
TestSmtp sender=new TestSmtp();
boolean flag=sender.sendMail();
if(flag)
System.out.println("send successfully");
else
System.out.println("send fault");
}
}
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class TestSmtp{
private String smtp="smtp.163.com";
private String subject="Try";
private String to="xxxxxx";//目的信箱
private String user="xxxxxxx";//我的163信箱用户名
private String pass="xxxxxxxxxx";//我的163信箱密码
private String body="This is the mail body";
private class SmtpAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication
(user, pass);
}
}
public boolean sendMail(){
//发送一个邮件
try {
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtp);
Session sess = Session.getDefaultInstance(props, new SmtpAuthenticator());
// Session sess=Session.getDefaultInstance (props,null);
sess.setDebug(true);
MimeMessage msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(smtp));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
System.out.println("Date set");
Transport transport = sess.getTransport("smtp");
System.out.println("get transport");
transport.connect(smtp,user,pass);
System.out.println("connect build");
transport.send(msg);
transport.close();
return true;
}
catch (Exception e) {
return false;
}
}
public static void main(String []args){
TestSmtp sender=new TestSmtp();
boolean flag=sender.sendMail();
if(flag)
System.out.println("send successfully");
else
System.out.println("send fault");
}
}
|
smtp.163.com发送的时候是需要认证的,如此而已