javamail使用"普通"提供商

时间:2016-06-27 11:10:26

标签: java javamail

也许这是微不足道的......但对我而言正在浪费大量时间,因为我无法找到解决方案。

这是我的代码(我从互联网上非常常见的例子中推断出第一行):

static Properties properties = new Properties();
{
    properties.put("mail.smtp.host", getText("smtp.host"));
    properties.put("mail.smtp.socketFactory.port", getText("socketFactory.port"));
    properties.put("mail.smtp.socketFactory.class", getText("socketFactory.class"));
    properties.put("mail.smtp.auth", getText("smtp.auth"));
    properties.put("mail.smtp.port", getText("smtp.port"));

    properties.put("mail.smtp.starttls.enable", "true");
}

private String from = getText("from");
private String smtppw = getText("pw");

private void initMS() {
    if (smtppw != null) {
        mailsession = Session.getDefaultInstance(properties,  new Authenticator() {

            protected PasswordAuthentication  getPasswordAuthentication() {
                return new PasswordAuthentication(from, smtppw);
            }
        });
    }
    else {
        mailsession = Session.getDefaultInstance(properties,  new Authenticator() {
            protected PasswordAuthentication  getPasswordAuthentication() {
                return new PasswordAuthentication(from, null);
            }
        });
    }   
}



String attachpath =  ServletActionContext.getServletContext().getRealPath("/");

public String sendNote(String type, SendingParamsDTO sendingParams, String filename) throws /*AddressException,*/ MessagingException {

    String res = SUCCESS;
    //try {

        if (mailsession == null) initMS();

        Message message = new MimeMessage(mailsession);

        InternetAddress pobox = new InternetAddress(from);
        InternetAddress to[] = InternetAddress.parse(sendingParams.getMailto());

        message.setFrom(pobox);
        message.setRecipients( Message.RecipientType.TO, to);
        message.addHeader("Disposition-Notification-To", from);
        message.setSubject("[AN-V.C.O.] - "+sendingParams.getSubject() );
        message.setSentDate( sendingParams.getDate() );

        /**/
        logger.info(" ##  FROM: "+from);
        logger.info(" ##  TO:  "+to);
        logger.info(" ##  SUBJECT:     "+sendingParams.getSubject());
        logger.info(" ##  MESSAGE:   "+sendingParams.getMessage());
        if (filename != null)
            logger.info(" ##  ATTACH:    "+attachpath + filename);
        /**/


        MimeMultipart multipart = new MimeMultipart("related");
        //Multipart multipart = new MimeMultipart();

        BodyPart messageBodyPart1 = new MimeBodyPart();

        if (type.equals("text"))
            messageBodyPart1.setText(sendingParams.getMessage());
        else if (type.equals("html"))
            messageBodyPart1.setContent(sendingParams.getMessage(), "text/html");


        BodyPart messageBodyPart2 = null;
        if (filename != null){
            DataSource source = new FileDataSource(attachpath);
            messageBodyPart2 = new MimeBodyPart();
            messageBodyPart2.setDataHandler( new DataHandler(source) );
            messageBodyPart2.setFileName( filename );
        }


        multipart.addBodyPart( messageBodyPart1 );
        if (filename != null){
            multipart.addBodyPart( messageBodyPart2 );
        }


        message.setContent(multipart);


        Transport.send(message, message.getRecipients(Message.RecipientType.TO));
        //Transport transport = mailsession.getTransport();
        //transport.connect();
        //transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        //transport.close();

    //}
    //catch(Exception e) {
    //  res = ERROR;
    //  e.printStackTrace();
    //}

    return res;
}

,其中

## generic using GMAIL
smtp.host=smtp.gmail.com
socketFactory.port=465
socketFactory.class=javax.net.ssl.SSLSocketFactory
smtp.auth=true
smtp.port=465

所以一切正常! 现在我需要更改gmail whit商业提供商。我用这种方式转换了我的代码:

属性文件变为

## real using myprovider
smtp.host=smtp.myprovider.com
#socketFactory.port=465
#socketFactory.class=javax.net.ssl.SSLSocketFactory
smtp.auth=true
smtp.port=465

和java代码

static Properties properties = new Properties();
{
    properties.put("mail.smtp.host", getText("smtp.host"));
    //properties.put("mail.smtp.socketFactory.port", getText("socketFactory.port"));
    //properties.put("mail.smtp.socketFactory.class", getText("socketFactory.class"));
    properties.put("mail.smtp.auth", getText("smtp.auth"));
    properties.put("mail.smtp.port", getText("smtp.port"));

    //properties.put("mail.smtp.starttls.enable", "true");
}

我还试图保持socketfactory,使用我的个人邮件提供商,以避免指定身份验证和端口...但仍然无法正常工作!
怎么了? 感谢名单

PS-更具体。没有返回错误或异常,只有进程仍然挂起执行行

Transport.send(message, message.getRecipients(Message.RecipientType.TO));

0 个答案:

没有答案