2013年9月13日星期五

javamail successfully send mail but not receive e-mail ?

I use java to write a program to send mail , suggesting sent successfully, but sent to the e-mail I received no e-mail ? That is why
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 ();
}

}

Run prompt :
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:
250 sender ok
RCPT TO:
250 recipient 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
------ Solution ----------------------- ---------------------

/**
     * 发邮件方法
     * 
     * @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;
}

This is used to write , test yourself to do something , I feel that you set the port 25 problem , you put 25 removed, not, then take a look at my code !
------ For reference only -------------------------------------- -
someone will do
------ For reference only ------------------------------- --------
can accept mail , colleagues around me did , and it worked . may be more complex than sending mail , after all, to extract e-mail the information to the server , the server that you are likely to attack the server , to a certain extent will encounter some difficulties, as long as the landlord should be able to spend some effort to get
------ For reference only ---------------------------------------
obviously sent successfully ah ? Strange
------ For reference only ------------------------------------- -
is ie security level problem, you try to put to the minimum security level , success please give points ! ! ! ! ! ! ! ! ! ! ! !
------ For reference only -------------------------------------- -
received code sent to look
------ For reference only --------------------------- ------------
msg.setFrom (new InternetAddress ("haosiweishizhu@sohu.com"));
transport.sendMessage (msg, new Address [] {new InternetAddress ("wangzhiqing0327@sohu.com")});

these two into the same e-mail address will be able to receive , and I tried, I had also been successfully sent mail to see , changed after the enough.


------ For reference only ---------------------------------- -----
do not mean a mistake, your code and I'm not the same, nor did above changes.
------ For reference only -------------------------------------- -
halo, not copied wrong, that is the wrong word , your code should be no mistake , it may be when your mail server to spam , and you want to set your account login authentication should be no problem.
------ For reference only -------------------------------------- -
encountered the same problem slightly. Sina is probably put your e-mail as spam it ! Try another mail server

没有评论:

发表评论