0

我正在用java构建一个电子邮件应用程序,到目前为止我尝试使用它,我在运行此代码时没有收到任何错误,但是我编译并运行代码后的问题是net beans IDE只是向我显示,程序正在运行,但是当我检查邮箱时,没有收到这样的邮件。谁能解释我为什么会这样?

我还在最后打印了一条“成功发送”消息,但不知何故该消息没有被打印,我无法弄清楚错误是什么,任何帮助将不胜感激,谢谢。

    public class Email 
    public static void main(String[] args) {
    // TODO code application logic here


    String[] to = {"pqr@gmail.com"};
    String from = "abc@gmail.com";
    String host = "smtp.gmail.com";
            String user_name = "abc@gmail.com";
            String password = "xyz";

    try{
    Properties properties = System.getProperties();
    properties.setProperty("smtp.gmail.com", "imaps");
            properties.put("smtp.starttls.enable", "true");
            properties.put("mail.smtp.host",host);
            properties.put("mail.smtp.user",user_name);
            properties.put("mail.smtp.password", password);
            properties.put("mail.smtp.port", "465");
            properties.put("mail.smtp.auth", "true");


            //properties.put
    Session session = Session.getDefaultInstance(properties,null);
            //Store store = session.getStore("imaps");



        MimeMessage message = new MimeMessage(session);
        // basically stores one or more addresses , whom the mail has to be       sent
        //InternetAddress[] send_to = { new InternetAddress(to) };
        //InternetAddress[] send_from = { new InternetAddress(from) };
                    InternetAddress[] toAddress = new InternetAddress[to.length];

                    for(int i = 0; i < to.length ;i++)
                    {
                        toAddress[i] = new InternetAddress(to[i]);
                    }

                    for(int i = 0 ; i < toAddress.length ; i++){
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
                    }

        message.setFrom(new InternetAddress(from));
        message.setSubject("Testing email");
        message.setText("this is a test");
                    Transport transport = session.getTransport("smtp");
                    transport.connect(host, user_name, password);
        transport.send(message);
                    transport.close();


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


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

}

4

3 回答 3

1

试试这个代码

public class SendMail {

    String host, port, emailid,username, password;
    Properties props = System.getProperties();
    Session l_session = null;

    public SendMail() {
        host = "smtp.mail.yahoo.com";
        port = "587";
        emailid = "a@yahoo.com";
        username = "a";
        password = "pwd";

        emailSettings();
        createSession();
        sendMessage("a@yahoo.com", "rahul@gmail.com","Test","test Mail");
    }

    public void emailSettings() {
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "false");
        props.put("mail.smtp.port", port);
//        props.put("mail.smtp.socketFactory.port", port);
//        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.put("mail.smtp.socketFactory.fallback", "false");

    }

    public void createSession() {

        l_session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        l_session.setDebug(true); // Enable the debug mode

    }

    public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
        //System.out.println("Inside sendMessage 2 :: >> ");
        try {
            //System.out.println("Sending Message *********************************** ");
            MimeMessage message = new MimeMessage(l_session);
            emailid = emailFromUser;
            //System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid);
            //message.setFrom(new InternetAddress(emailid));
            message.setFrom(new InternetAddress(this.emailid));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
            message.setSubject(subject);
            message.setContent(msg, "text/html");

            //message.setText(msg);
            Transport.send(message);
            System.out.println("Message Sent");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }//end catch block
        return true;
    }

}
于 2012-09-20T08:44:12.223 回答
1

如果您在代理/防火墙后面运行您可能必须设置代理

System.setProperty("http.proxyHost", "myProxyServer.com");
System.setProperty("http.proxyPort", "80");
于 2012-09-20T08:51:30.913 回答
0

谢谢大家,看来我错过了

props.put("mail.smtp.socketFactory.port", "587");

部分,添加了这个并且它工作正常。

于 2012-09-20T15:11:59.533 回答