我用java写了一个发送邮件的程序,提示发送成功,但是发送到的邮箱却收到不邮件?这是为什么
package cn.itcast;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo1 {
/**
* @param args add by zxx ,Feb 5, 2009
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setText("nengshoudaoma");
msg.setFrom(new InternetAddress("haosiweishizhu@sohu.com"));
Transport transport = session.getTransport();
transport.connect("smtp.sina.cn", 25, "haosiweishizhu", "haosiwei");
transport.sendMessage(msg,new Address[]{new InternetAddress("wangzhiqing0327@sohu.com")});
//transport.send(msg,new Address[]{new InternetAddress("itcast_test@sohu.com")});
transport.close();
}
}
运行提示:
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: trying to connect to host "smtp.sina.cn", port 25, isSSL false
220 irxd5-182.sinamail.sina.com.cn ESMTP
DEBUG SMTP: connected to host "smtp.sina.cn", port: 25
EHLO 59972864974a4a5
250-irxd5-182.sinamail.sina.com.cn
250-8BITMIME
250-SIZE 83886080
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "83886080"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
aGFvc2l3ZWlzaGl6aHU=
334 UGFzc3dvcmQ6
aGFvc2l3ZWk=
235 #2.0.0 OK Authenticated
DEBUG SMTP: use8bit false
MAIL FROM:<haosiweishizhu@sohu.com>
250 sender <haosiweishizhu@sohu.com> ok
RCPT TO:<wangzhiqing0327@sohu.com>
250 recipient <wangzhiqing0327@sohu.com> ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: wangzhiqing0327@sohu.com
DATA
354 go ahead
From: haosiweishizhu@sohu.com
Message-ID: <24212202.01317206893437.JavaMail.Administrator@59972864974a4a5>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
nengshoudaoma
.
250 ok: Message 957325150 accepted
QUIT
221 irxd5-182.sinamail.sina.com.cn
------解决方案--------------------
- Java code
/** * 发邮件方法 * * @return -2:发送失败,登陆邮件服务器用户名或密码错误;-1:发送失败,邮件格式内容不合法;0:发送成功; */ public static int sendEmail(SendEmailVo sendEmailVo) { String from = sendEmailVo.getFrom(); String toes = sendEmailVo.getTo(); String password = sendEmailVo.getPassword(); String subject = sendEmailVo.getSubject(); String content = sendEmailVo.getContent(); String host = null; String name = null; Session session = null; MimeMessage message = null; Transport transport =null; try { // 根据发件Email算出发件的邮件服务器 host = "smtp." + from.substring(from.indexOf("@") + 1, from.length()); // 根据发件Email算出登陆邮件服务器的用户名 name = from.substring(0, from.indexOf("@")); // 分割出多个收件人 String[] to = toes.split(";"); // 初始化一个存放属性的工具类 Properties props = new Properties(); // 设置发送Email的服务器 props.put("mail.smtp.host", host); // 对发送Email进行身份认证 props.put("mail.smpt.auth", "true"); // 得到与服务器的一个会话 session = Session.getInstance(props, null); // 定义一个邮件消息 message = new MimeMessage(session); // 可以设置一系列邮件属性的类 BodyPart bp = new MimeBodyPart(); Multipart mp = null; // 设置能够解析html标签的邮件 bp.setContent(content, "text/html;charset=utf-8"); // 可以存放多个BodyPart的类 mp = new MimeMultipart(); // 添加BodyPart到Multipart类 mp.addBodyPart(bp); // 设置收件人 message.setFrom(new InternetAddress(from)); // 设置邮件主题 message.setSubject(subject); // 设置邮件内容 message.setContent(mp); // 添加多个收件人 for (int i = 0; i < to.length; i++) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); } transport = session.getTransport("smtp"); } catch (MessagingException e) { //e.printStackTrace(); return -1; } //登陆发邮件服务器,发邮件 try { transport.connect(host, name, password); transport.sendMessage(message, message.getAllRecipients()); } catch (MessagingException e) { //e.printStackTrace(); return -2; } return 0; }