package util;
import javax.mail.*;
import javax.activation.*;
import java.util.*;
import javax.mail.internet.*;
public class Mail { /**
* 实现自动发邮件功能
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String send="[email protected]";
String get="[email protected]";
String pass="qiujintao520luod";
String host="smtp.qq.com";
Properties props=System.getProperties(); //获取系统Properties
props.put("mail.smtp.com",host);//设置smtp服务器
props.put("mail.smtp.auth","true");
SMTPAuthenticator smtpAuthenticator = new SMTPAuthenticator(send,pass);
Session se=Session.getDefaultInstance(props,smtpAuthenticator);
se.setDebug(true);//查看运行时的信息
MimeMessage mmg=new MimeMessage(se); //邮件会话新建一个消息对象
try {
InternetAddress from=new InternetAddress(send); //发件人
mmg.setFrom(from);
InternetAddress to=new InternetAddress(get,false); //收件人
mmg.setRecipient(Message.RecipientType.TO, to);
mmg.setSubject("自己编写的发送邮件功能");//邮件主题
mmg.setText("胖子真丑");
//邮件类容,可以是纯文本text/html
mmg.saveChanges();
System.out.println("正在发送中、、、");
// smtp验证,就是你用来发邮件的邮箱用户名密码
//发送
Transport.send(mmg);
System.out.println("邮件已顺利传送");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}package util;import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SMTPAuthenticator extends Authenticator {
private String name;
private String password;
public SMTPAuthenticator(){
}
public SMTPAuthenticator(String name,String password) {
this.name=name;
this.password=password;
}
public PasswordAuthentication getPasswordAuthentication(String name,String password) {
return new PasswordAuthentication(name,password);
}
}
import javax.mail.*;
import javax.activation.*;
import java.util.*;
import javax.mail.internet.*;
public class Mail { /**
* 实现自动发邮件功能
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String send="[email protected]";
String get="[email protected]";
String pass="qiujintao520luod";
String host="smtp.qq.com";
Properties props=System.getProperties(); //获取系统Properties
props.put("mail.smtp.com",host);//设置smtp服务器
props.put("mail.smtp.auth","true");
SMTPAuthenticator smtpAuthenticator = new SMTPAuthenticator(send,pass);
Session se=Session.getDefaultInstance(props,smtpAuthenticator);
se.setDebug(true);//查看运行时的信息
MimeMessage mmg=new MimeMessage(se); //邮件会话新建一个消息对象
try {
InternetAddress from=new InternetAddress(send); //发件人
mmg.setFrom(from);
InternetAddress to=new InternetAddress(get,false); //收件人
mmg.setRecipient(Message.RecipientType.TO, to);
mmg.setSubject("自己编写的发送邮件功能");//邮件主题
mmg.setText("胖子真丑");
//邮件类容,可以是纯文本text/html
mmg.saveChanges();
System.out.println("正在发送中、、、");
// smtp验证,就是你用来发邮件的邮箱用户名密码
//发送
Transport.send(mmg);
System.out.println("邮件已顺利传送");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}package util;import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SMTPAuthenticator extends Authenticator {
private String name;
private String password;
public SMTPAuthenticator(){
}
public SMTPAuthenticator(String name,String password) {
this.name=name;
this.password=password;
}
public PasswordAuthentication getPasswordAuthentication(String name,String password) {
return new PasswordAuthentication(name,password);
}
}
解决方案 »
- 关于axis调用WebService返回null值的问题
- 求教,谢绝水贴,有能力来!
- struts2,防止多次提交button disable 的一个问题
- 如何学习spring AOP
- 跪求一个类似的管理系统,用XML作为数据存储,开发采用tomcat+jsp
- not instantiate id generator 问题请教
- 急救:struts中action路径问题,解决立即给分200
- 急:java程序中的SQL数据库连接问题
- 请问高手用过j2eesdk1.4吗,这个工具是作什么用的,方便的话,请指点一下吧,谢了先
- 请教jbuilderx和jboss集成之后ejb部署的问题
- 谁帮我看看这段代码啊?谢谢
- 参数循环 多页面页面链接参数循环
DEBUG: setDebug: JavaMail version 1.3.2
正在发送中、、、
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at util.Mail.main(Mail.java:38)
楼主参考下这篇blog,看看能否解决。
http://blog.csdn.net/tianting_wx/archive/2008/06/13/2543015.aspx
public ActionForward getPassword(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = request.getParameter("username");
String from = "[email protected]";
GpsUser gpsUser = (GpsUser) BaseDao
.getUniqueResult("FROM GpsUser user WHERE user.username='"
+ username + "'");
if (gpsUser != null) {
request.setAttribute("username", gpsUser.getUsername());
request.setAttribute("user", gpsUser);
String subject = "过程监控系统取回密码!";
String messageText = "您的密码是" + gpsUser.getPassword();
String password = "jinjinray";
Properties props = new Properties();
//声明要通过身份验证
props.setProperty("mail.smtp.auth", "true");
//根据属性新建一个邮件会话
Session s = Session.getInstance(props);
s.setDebug(true);//可以看到后台信息
//由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(s);
//设置邮件
InternetAddress to_from = new InternetAddress(from);
//设置发件人
message.setFrom(to_from);
InternetAddress to_to = new InternetAddress(username);
//设置收件人
message.setRecipient(Message.RecipientType.TO, to_to);
//设置主题
message.setSubject(subject);
//设置信件内容
message.setText(messageText);
//设置发信时间
message.setSentDate(new Date());
//发送邮件
//存储邮件信息
message.saveChanges();
Transport transport = s.getTransport("smtp");
//以smtp方式登录邮箱 注意这一步需要你的用户名和密码,务必正确,才能正常发送
transport.connect("smtp.qq.com", from, password);
//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(message, message.getAllRecipients());
transport.close();
request.setAttribute("msg", "邮件发送成功,请注意查收邮件!");
return mapping.findForward("success");
} else {
request.setAttribute("msg", "邮件发送失败,请检查您输入的用户名是否正确!");
return mapping.findForward("error");
} }
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false
220 esmtp7.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host "smtp.qq.com", port: 25EHLO ibm-70039bfc7fe
250-esmtp7.qq.com
250-PIPELINING
250-SIZE 52428800
250-AUTH LOGIN
250-AUTH=LOGIN
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
MjY2NTc5MThAcXEuY29t
454 Error: authentication failed
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at util.Mail.main(Mail.java:40)