我参照网上,写了一个简单的使用javamail发送邮件的程序,使用一般的邮件服务器作为发件人,程序都很正常;但使用smtp.sina.com.cn作为邮件服务器地址的发件人时,程序却验证失败!这是什么原因?
程序:
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Session;import java.util.Properties;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.MessagingException;public class Send {
public static void main(String[] args) throws Exception { new Send().sendMail(); } private String host = "smtp.sina.com.cn";// 主机; private String user = "阿谊";// 用户名; private String password = "987654";// 密码; private String from = "ayis_test@sina.com";// 发件人----这是我用于测试的sina邮箱 private String to = "nonopo12345@gmail.com";// 收件人; private String subjetc = "一个很简单的例子,成功了...........";// 标题; private String content = "这几天会有考试吗?真的是,头痛!";// 内容; // 设置内容 public void setContent(String content) { this.content = content; } // 设置收件人 public void setTo(String to) { this.to = to; } /** * 发送email * * @throws MessagingException * @throws Exception */ public void sendMail() throws MessagingException, Exception { Properties props = new Properties(); props.put("mail.smtp.host", host);// 指定SMTP服务器 props.put("mail.smtp.auth", "true");// 指定是否需要SMTP验证 Session mailSession = Session.getDefaultInstance(props); Message message = new MimeMessage(mailSession); message.setFrom(new InternetAddress(from));// 发件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));// 收件人 message.setSubject(subjetc);// 邮件主题 message.setText(content);// 邮件内容 message.saveChanges(); Transport transport = mailSession.getTransport("smtp"); transport.connect(host, user, password); transport.sendMessage(message, message.getAllRecipients()); transport.close(); }
}