从brinkster托管帐户使用java发送邮件

时间:2013-11-19 16:16:34

标签: java email smtp

我需要使用java从brinkster托管帐户发送电子邮件。我使用此代码从我的gmail帐户发送电子邮件,它工作正常。

    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("gmailuser", "password");

                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("gmailuser@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("someone@gmail.com"));

        message.setSubject("subject");
        message.setText("text");

        Transport.send(message);catch (MessagingException e) {
        throw new RuntimeException(e);
    }catch (MessagingException e) {
        throw new RuntimeException(e);
    }

现在,我需要从username@company.com.co发送电子邮件 我有这个

    Properties props = new Properties();
    props.put("mail.smtp.host", "mymail.brinkster.com");     //<--changed this
    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@company.com.co", "password");      //<--changed this

                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("username@company.com.co"));     //<--changed this
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("someone@gmail.com"));

        message.setSubject("subject");
        message.setText("text");

        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

这不起作用。我究竟做错了什么?我认为它可能与smtp.host有关,但我不知道是什么。

1 个答案:

答案 0 :(得分:0)

我可能知道你会得到什么类型的例外

每个主机都有不同的名称(mail.smtp.host),确保您的主机名正确

并且在gmail中,无论地址是什么,你都不会显示为来自地址