当前位置: 代码迷 >> J2EE >> javax.mail.internet.AddressException {
  详细解决方案

javax.mail.internet.AddressException {

热度:1073   发布时间:2016-04-22 02:48:15.0
用javaMail 无法连接
javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.163.com, port: 25;
  nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1008)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at com.ambuf.SendMail.send(SendMail.java:82)
at com.ambuf.SendMail.main(SendMail.java:31)
代码


public static void send(final String to, final String from,
final String subject, final String body)
throws javax.mail.MessagingException,
javax.mail.internet.AddressException {
//read the hardcoded values from propertiy file
//Validation.validateNotNull( to );
//Validation.validateNotNull( from );
//Validation.validateNotNull( subject );
// Validation.validateNotNull( body );
//final String smtpServer="www.chinaxp.org";
//http://mail.sina.com.cn/ http://mail.163.com/
final String smtpServer = "smtp.mail.163.com";
final String user = "minghua131";
final String password = "XXXXXXXXX";//密码

final Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);

//add smtp Authenticator
props.put("mail.smtp.auth", "true");

final Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);

final Transport tr = session.getTransport("smtp");
tr.connect(smtpServer, user, password);

MimeMessage msg = new MimeMessage(session);
final MimeBodyPart mbp1 = new MimeBodyPart();
final MimeMultipart mp = new MimeMultipart();
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject, "GB2312");
mbp1.setText(body, "GB2312");
mp.addBodyPart(mbp1);
msg.setContent(mp);
msg.setSentDate(new java.util.Date());
msg.saveChanges(); // don't forget this

//Transport.send(msg);change to use intance not use static method
// send()
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();

------解决方案--------------------
my blog

host = "smtp.163.com"
------解决方案--------------------
Java code
import java.util.Properties;import javax.mail.Address;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class SendTest {    /*     *      * stmp信息     * */    private static String smtp="smtp.gmail.com";    private static String user="lhjtianji";    private static String password="********";    private static String from="lhjtianji@gmail.com";            //邮件发送    public static void sendemail(String[] to,String subject,String body){                Properties props=new Properties();   //获得邮件服务器,用户名,密码等信息        props.put("mail.smtp.host", "127.0.0.1");        props.put("mail.smtp.auth", "true");    //需要smtp验证        Session ssn=Session.getInstance(props,null);            System.out.println("发送成功1");        if(smtp.indexOf("smtp.gmail.com")>=0)//google smtp验证        {            props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");             props.setProperty("mail.smtp.socketFactory.fallback", "false");             props.setProperty("mail.smtp.port", "465");             props.setProperty("mail.smtp.socketFactory.port", "465");        }        MimeMessage message=new MimeMessage(ssn);        System.out.println("发送成功2");        try {            InternetAddress fromAddress=new InternetAddress(from);    //发件人的地址                        try {                message.setFrom(fromAddress);    //收件人地址                //单个接受者                if(to.length<2){                    Address toAddress=new InternetAddress(to[0]);                    message.addRecipient(Message.RecipientType.TO, toAddress);   //收件人地址的类型                    System.out.println("接受者的地址:"+toAddress);                }                if(to.length>=2){                    Address[] toAddress=new Address[to.length];                    for(int i=0;i<to.length;i++){                        InternetAddress toer=new InternetAddress(to[i]);                        toAddress[i]=toer;                        System.out.println("接受者的地址:"+toer);                    }                    message.addRecipients(Message.RecipientType.TO, toAddress);                }                    message.setSubject(subject);                    message.setContent(body, "text/html;charset=GBK");                    message.setText(body);                    System.out.println("发送成功3");                    Transport transport=ssn.getTransport("smtp");                    System.out.println("发送成功4");                    transport.connect(smtp, user, password);                    System.out.println("发送成功5");                    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));                    System.out.println("发送成功6");                    //Transport.send(message);                    System.out.println("发送成功7");                    transport.close();                    System.out.println("发送成功8");                                                } catch (MessagingException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        } catch (AddressException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }    public static void main(String[] args){    /*    String [] to={"4138960@163.com"};        String subject="hello";        String str="test";        SendTest test=new SendTest();        test.sendemail(to, subject, str);        System.out.println("ok");*/    }}
  相关解决方案