发送邮件的问题?

时间:2014-09-12 13:28:01

标签: java spring email

我在java Spring中创建了邮件发送的应用程序,邮件发送在本地服务器上正常工作。我导出(战争文件)和主机进入tomcat服务器然后执行应用程序但邮件没有发送我不知道下面的代码是什么问题?

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

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

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("----@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("----@gmail.com"));
            message.setSubject("Test");
            message.setText("Hello"); 
            Transport.send(message); 
            System.out.println("Done"); 
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我得到了答案,它只是端口号码有问题,我改变并使用这两个端口号25(或)587。