我的代码如下:
package test;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUser {
Session mailSession=null;
public MailUser(){}
public static void main(String args[]){
MailUser MU=new MailUser();
if((MU.Connection("localhost", "Jmail", "Jmail"))==1){
Message msg=MU.createmessage("xingyuanjian@163.com", "xingyuanjian@sina.com","nihaoa", "fasongchenggong");
if (msg==null){
System.out.println("信息创建失败!");
}
try{
MU.sendmail(msg);
}catch(Exception e){
e.printStackTrace();
}
System.out.print("发送邮件成功!");
}
}
public int Connection(String host,String username,String password){
Properties props=System.getProperties();
SmtpAuth auth=null;
auth=new SmtpAuth();
auth.setUserinfo(username,password);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port","25");
props.put("mail.smtp.protocal","smtp");
props.put("mail.smtp.auth","true");
mailSession=Session.getDefaultInstance(props,auth);
mailSession.setPasswordAuthentication(new URLName(host),auth.getPasswordAuthentication());
System.out.print("连接成功!");
return 1;
}
public Message createmessage(String from,String to,String subject,String text){
Message msg=new MimeMessage(this.mailSession);
try{
msg.setFrom(new InternetAddress(from));
}catch(MessagingException e){
System.out.print("fromerror;"+e);
}
if ((to!=null)&&(!to.equals(""))){
try{
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
}catch(MessagingException e){
System.out.print("toerror"+e);
}
}
if ((subject!=null)&&(!subject.equals(""))){
try{
msg.setSubject(subject);
}catch(MessagingException e){
System.out.print("subjecterror"+e);
}
}
if((text!=null)&&(!text.equals(""))){
try{
msg.setText(text);
}catch(MessagingException e){
System.out.print("texterror"+e);
}
}
System.out.print("信息创建成功!");
return msg;
}
public void sendmail(Message msg){
try {
Transport.send(msg);
} catch (MessagingException e) {
// TODO 自动生成 catch 块
System.out.print("sendmailerror"+e);
}
}
}
运行结果:连接成功!信息创建成功!发送邮件成功!
但是邮箱里收不到邮件!!
哪位大哥帮我看看,代码到底是哪里出错了,,谢谢!!
在线!!!