大佬们,讨教一个问题,新浪的smtp服务器是不是有问题,126、163、搜狐、yeah邮箱javamail都能发送,就新浪不行,是新浪的配置和其他的不一样吗?邮箱能登录,smtp服务也打开了
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class javamailtest {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty(“mail.host”, “smtp.sina.com”);
prop.setProperty(“mail.transport.protocol”, “smtp”);
prop.setProperty(“mail.smtp.auth”, “true”);
prop.setProperty(“mail.smtp.port”, “25”);
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect(“smtp.sina.com”, “用户名”, “密码”);
//4、创建邮件
Message message = createSimpleMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress(“发件人”));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是本人给本人发
message.setRecipient(Message.RecipientType.TO, new InternetAddress(“收件人”));
//邮件的标题
message.setSubject(“只包含文本的简单邮件”);
//邮件的文本内容
message.setContent(“你好啊!”, “text/html;charset=UTF-8”);
//返回创建好的邮件对象
return message;
}
}
Exception in thread “main” javax.mail.AuthenticationFailedException: 535 5.7.8 authentication failed
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:894)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:815)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:729)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at com.cattsoft.collect.batch.javamailtest.main(javamailtest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class javamailtest {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty(“mail.host”, “smtp.sina.com”);
prop.setProperty(“mail.transport.protocol”, “smtp”);
prop.setProperty(“mail.smtp.auth”, “true”);
prop.setProperty(“mail.smtp.port”, “25”);
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect(“smtp.sina.com”, “用户名”, “密码”);
//4、创建邮件
Message message = createSimpleMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress(“发件人”));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是本人给本人发
message.setRecipient(Message.RecipientType.TO, new InternetAddress(“收件人”));
//邮件的标题
message.setSubject(“只包含文本的简单邮件”);
//邮件的文本内容
message.setContent(“你好啊!”, “text/html;charset=UTF-8”);
//返回创建好的邮件对象
return message;
}
}
Exception in thread “main” javax.mail.AuthenticationFailedException: 535 5.7.8 authentication failed
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:894)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:815)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:729)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at com.cattsoft.collect.batch.javamailtest.main(javamailtest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
解决方案
40
差一下用户名的要求,要完整用户名,包含 @sina.com