无法使用Java Mail从本地服务器向Web邮件服务器发送电子邮件

时间:2015-03-15 10:04:17

标签: java apache email javamail

我正在尝试从Java Mail API发送电子邮件。目前我已经安装了Apache James作为我自己的测试邮件服务器。我在詹姆斯的电子邮件帐户用户很少,他们是“红色”,“绿色”和“蓝色”。

现在,请看下面的代码。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Yohan
 */

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  

public class Mail {

     public static void main(String [] args){  
      String host="localhost";  
  final String user="red@localhost";//change accordingly  
  final String password="red";//change accordingly  

  String to="abc@gmail.com";//change accordingly  
  //String to= "blue@localhost";

   //Get the session object  
   Properties props = new Properties();  
   props.put("mail.smtp.host",host);  
   props.put("mail.smtp.auth", "true");  

   Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() {  
      protected PasswordAuthentication getPasswordAuthentication() {  
    return new PasswordAuthentication(user,password);  
      }  
    });  

   //Compose the message  
    try {  
     MimeMessage message = new MimeMessage(session);  
     message.setFrom(new InternetAddress(user));  
     message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
     message.setSubject("Test Mail From Localhost");  
     message.setText("Soetimes, it didnt work..");  

    //send the message  
     Transport.send(message);  

     System.out.println("message sent successfully...");  

     } catch (MessagingException e) {e.printStackTrace();}  
 }  


}

此代码存在问题。也就是说,如果我将邮件发送到本地邮箱,例如blue @ localhost,邮件将被发送。如果我将其发送到基于网络的“Gmail”,它就不会发送。但是在这两种情况下,我都收到消息`消息已成功发送....

这里发生了什么?

0 个答案:

没有答案