当前位置: 技术问答>java相关
我的代码中已经假如smtp认证,为什么在tomcat上还提示客户端没有验证?使用smtp.sohu.com或smtp.sina.com.cn都不行代码如下:
来源: 互联网 发布时间:2015-05-18
本文导语: 我的代码中已经假如smtp认证,为什么在tomcat上还提示客户端没有验证?代码如下: 使用smtp.sohu.com或smtp.sina.com.cn都不行 import javax.mail.*; import javax.mail.internet.*; import java.io.*; import java.util.*; import javax.activation.*; public cl...
我的代码中已经假如smtp认证,为什么在tomcat上还提示客户端没有验证?代码如下:
使用smtp.sohu.com或smtp.sina.com.cn都不行
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
public class SmtpAuthenticator extends javax.mail.Authenticator
{
String username = "xxxxx";
String password = "xxxxx";
public SmtpAuthenticator()
{
super();
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password);
}
}
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
public class SendTextEmail
{
String host = "smtp.sohu.com";
String username = "xxxxx";
String password = "xxxxx";
String mail_head_name = "this is head of this mail name";
String mail_head_value = "this is head of this mail value";
String mail_from = "xxxxxx@sohu.com";
String mail_subject = "你在班级网站上注册的用户名和密码!";
String mail_body = null;
public void sendMessage(String mail_to,String cname,String userid,String password)
{
try
{
Properties props = System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.smtp,auth","true");
SmtpAuthenticator auth = new SmtpAuthenticator();
Session session = Session.getInstance(props,auth);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setSentDate(new Date());
message.setHeader(mail_head_name,mail_head_value);
Address fromAddress = new InternetAddress(mail_from,"班级网站管理员");
message.setFrom(fromAddress);
Address toAddress = new InternetAddress(mail_to);
message.setRecipient(Message.RecipientType.TO,toAddress);
message.setSubject(mail_subject);
mail_body = cname + ":rn"+" 你好!你在班级网站上已经成功注册!rn"+" 用户名:"+userid+"rn"+" 密 码:"+password;
message.setText(mail_body);
Transport transport = session.getTransport("smtp");
transport.connect(host,username,password);
transport.send(message);
transport.close();
}
catch(Exception ex)
{
System.err.println("send Email faled:"+ex);
}
}
}
使用smtp.sohu.com或smtp.sina.com.cn都不行
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
public class SmtpAuthenticator extends javax.mail.Authenticator
{
String username = "xxxxx";
String password = "xxxxx";
public SmtpAuthenticator()
{
super();
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password);
}
}
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
public class SendTextEmail
{
String host = "smtp.sohu.com";
String username = "xxxxx";
String password = "xxxxx";
String mail_head_name = "this is head of this mail name";
String mail_head_value = "this is head of this mail value";
String mail_from = "xxxxxx@sohu.com";
String mail_subject = "你在班级网站上注册的用户名和密码!";
String mail_body = null;
public void sendMessage(String mail_to,String cname,String userid,String password)
{
try
{
Properties props = System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.smtp,auth","true");
SmtpAuthenticator auth = new SmtpAuthenticator();
Session session = Session.getInstance(props,auth);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setSentDate(new Date());
message.setHeader(mail_head_name,mail_head_value);
Address fromAddress = new InternetAddress(mail_from,"班级网站管理员");
message.setFrom(fromAddress);
Address toAddress = new InternetAddress(mail_to);
message.setRecipient(Message.RecipientType.TO,toAddress);
message.setSubject(mail_subject);
mail_body = cname + ":rn"+" 你好!你在班级网站上已经成功注册!rn"+" 用户名:"+userid+"rn"+" 密 码:"+password;
message.setText(mail_body);
Transport transport = session.getTransport("smtp");
transport.connect(host,username,password);
transport.send(message);
transport.close();
}
catch(Exception ex)
{
System.err.println("send Email faled:"+ex);
}
}
}
|
debug 信息贴上来!