package com.dfrz.util;
import java.io.FileInputStream;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MySendEmail {
public static void main(String[] args) {
try{
String userName="*****@sina.com";
String password="*****";
String smtp_server="smtp.sina.com";
String from_mail_address=userName;
String to_mail_address="j_hongru@sinau.com";
Authenticator auth=new PopupAuthenticator(userName,password);
Properties mailProps=new Properties();
mailProps.put("mail.smtp.host", smtp_server);
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", userName);
mailProps.put("password", password);
Session mailSession=Session.getInstance(mailProps, auth);
mailSession.setDebug(true);
MimeMessage message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from_mail_address));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to_mail_address));
message.setSubject("Mail Testw");
MimeMultipart multi=new MimeMultipart();
BodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText("电子邮件测试内容w");
//textBodyPart.setFileName("37af4739a11fc9d6b311c712.jpg");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
Transport.send(message);
}catch(Exception ex){
System.err.println("邮件发送失败的原因是:"+ex.getMessage());
System.err.println("具体的错误原因");
ex.printStackTrace(System.err);
}
}
}
class PopupAuthenticator extends Authenticator{
private String username;
private String password;
public PopupAuthenticator(String username,String pwd){
this.username=username;
this.password=pwd;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(this.username,this.password);
}
}
6 个解决方案
#1
出现了这个错
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.sina.com", port 25, isSSL false
220 smtp682-31.sinamail.sina.com.cn ESMTP
DEBUG SMTP: connected to host "smtp.sina.com", port: 25
EHLO ffffffff
250-smtp682-31.sinamail.sina.com.cn
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
al9ob25ncnVAc2luYS5jb20=
334 UGFzc3dvcmQ6
MjQ1ODk5M2ppYW5n
535 5.7.8 authentication failed
邮件发送失败的原因是:null
具体的错误原因
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.dfrz.util.MySendEmail.main(MySendEmail.java:47)
#2
Transport 对象应该通过session获取。如下
Transport transport = session.getTransport();
transport.connect(smtpServer, user, pwd);
transport.sendMessage(message, msg.getRecipients(Message.RecipientType.TO));
。。。。。
Transport transport = session.getTransport();
transport.connect(smtpServer, user, pwd);
transport.sendMessage(message, msg.getRecipients(Message.RecipientType.TO));
。。。。。
#3
#4
还是验证没通过。
#5
我貌似也出现过这个错误
好像是MyEclipse自带的j2ee包和你导的包冲突了
好像是MyEclipse自带的j2ee包和你导的包冲突了
#6
确定密码什么都正确呀,那去你的邮箱看看smtp服务器开启没有!
#1
出现了这个错
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.sina.com", port 25, isSSL false
220 smtp682-31.sinamail.sina.com.cn ESMTP
DEBUG SMTP: connected to host "smtp.sina.com", port: 25
EHLO ffffffff
250-smtp682-31.sinamail.sina.com.cn
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
al9ob25ncnVAc2luYS5jb20=
334 UGFzc3dvcmQ6
MjQ1ODk5M2ppYW5n
535 5.7.8 authentication failed
邮件发送失败的原因是:null
具体的错误原因
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.dfrz.util.MySendEmail.main(MySendEmail.java:47)
#2
Transport 对象应该通过session获取。如下
Transport transport = session.getTransport();
transport.connect(smtpServer, user, pwd);
transport.sendMessage(message, msg.getRecipients(Message.RecipientType.TO));
。。。。。
Transport transport = session.getTransport();
transport.connect(smtpServer, user, pwd);
transport.sendMessage(message, msg.getRecipients(Message.RecipientType.TO));
。。。。。
#3
#4
还是验证没通过。
#5
我貌似也出现过这个错误
好像是MyEclipse自带的j2ee包和你导的包冲突了
好像是MyEclipse自带的j2ee包和你导的包冲突了
#6
确定密码什么都正确呀,那去你的邮箱看看smtp服务器开启没有!