0
回答
使用javamail连接hotmail发送邮件出异常
华为云实践训练营,热门技术免费实践!>>>   

package mail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
//import org.springframework.mail.javamail.JavaMailSenderImpl;

public class TestMail {
 public static void main(String[] args){
  String mailbody = "用户邮件注册测试";
  int testresult = 0;// 定义邮件返回结果,1:成功 0:失败
  String testemail="chenling0116@163.com";
   String smtpAddress = "smtp.live.com";
   String smtpUser = "chenling0116@hotmail.com";
   String smtpPort = String.valueOf(25);
   final String username = "chenling0116@hotmail.com";
   final String password = "******";
   int link=1;
    try {
     Properties prop = new Properties(); //邮件发送者地址  
     prop.put("mail.smtp.host",smtpAddress);
     String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
     prop.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
     prop.setProperty("mail.smtp.socketFactory.fallback", "false");
     prop.setProperty("mail.smtp.socketFactory.port", smtpPort);
     prop.setProperty("mail.smtp.ssl","true");
     prop.setProperty("mail.davmail.from",smtpUser);   
     prop.setProperty("mail.smtp.auth", "false");
     prop.setProperty("mail.debug", "true");
     Session ses = Session.getInstance(prop);   
     //获得JDAVMail的邮件发送实例                  
     Transport transport = ses.getTransport("davmail_xmit"); 
     //连接到Hotmail服务器,请替换为自己的用户名和口令   
     transport.connect(smtpAddress,smtpUser,password);   
    
     
     // 准备要发送的邮件   
     MimeMessage txMsg = new MimeMessage(ses);   
     txMsg.setSubject("This is the subject");   //邮件发送者地址   
     InternetAddress addrFrom = new InternetAddress(smtpUser);   
     txMsg.setFrom(addrFrom);   //邮件接收者地址   
     InternetAddress addrTo = new InternetAddress(testemail);   
     txMsg.addRecipient(Message.RecipientType.TO, addrTo);   //邮件内容   
     txMsg.setText("Hello world !");   
     txMsg.setSentDate(new Date());   //发送邮件   
     transport.sendMessage(txMsg, txMsg.getAllRecipients());  
     }
    catch (Exception ex) {
      ex.printStackTrace();  
    } 

}

}

出现以下异常信息:如何解决??

log4j:WARN No appenders could be found for logger (org.apache.commons.httpclient.params.DefaultHttpParams).
log4j:WARN Please initialize the log4j system properly.
javax.mail.MessagingException: protocolConnect exception;
  nested exception is:
 java.net.ProtocolException: HTTP: status code 504
 at com.posisoft.jdavmail.JDAVMailService.protocolConnect(JDAVMailService.java:179)
 at com.posisoft.jdavmail.JDAVMailTransport.protocolConnect(JDAVMailTransport.java:76)
 at javax.mail.Service.connect(Service.java:288)
 at javax.mail.Service.connect(Service.java:169)
 at mail.TestMail.main(TestMail.java:40)
Caused by: java.net.ProtocolException: HTTP: status code 504
 at com.posisoft.jdavmail.JDAVMailService.executeMethod(JDAVMailService.java:223)
 at com.posisoft.jdavmail.JDAVMailService.updateFoldersInfo(JDAVMailService.java:243)
 at com.posisoft.jdavmail.JDAVMailService.protocolConnect(JDAVMailService.java:173)
 ... 4 more

 

 

 

 

 

举报
c363535948
发帖于8年前 0回/1K+阅
顶部