0

我在我的邮件服务中使用 Javamail。这是我的代码:

   public static void send( final String username, final String password, String recipientEmail, String ccEmail, String title, String message,
                         String from, String host, String port )
    throws AddressException, MessagingException
{
    Session session;

    Properties props = System.getProperties();
    props.setProperty( "mail.smtps.host", host );
    props.setProperty( "mail.smtp.port", port );

    session = Session.getInstance( props, null );

    // -- Create a new message --
    final MimeMessage msg = new MimeMessage( session );

    // -- Set the FROM and TO fields --
    msg.setFrom( new InternetAddress( username ) );
    msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( recipientEmail, false ) );

    if ( ccEmail.length() > 0 )
    {
        msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( ccEmail, false ) );
    }

    msg.setSubject( title, "utf-8" );
    msg.setFrom( new InternetAddress( from ) );
    msg.setText( message, "utf-8" );
    msg.setSentDate( new Date() );

    SMTPTransport t = (SMTPTransport) session.getTransport( "smtps" );

    t.connect( host, username, password );
    t.sendMessage( msg, msg.getAllRecipients() );
    t.close();
}

我称此方法为例如 - 用户名:mailtest - 来自:我的超级公司 <- 这是我的域 - 主机:mail.host.com <- 这是我的邮件提供商

并且 javamail 正在尝试以 mailtest@mysupercompany.com 身份登录我的服务但它失败了。当我从例如:test@mysupercompany.com提供时,它工作正常。

我检查了控制台中的日志,它说用户 mailtest@mysupercompany.com 不存在。就像 javamail 会尝试使用 mailtest@mysupercompany.com 登录,但它应该只使用我的用户名mailtest

为什么它与用户名相同时使用?

4

1 回答 1

0

它应该使用您作为用户名传入的任何内容。确保这是您所期望的;在连接调用之前打印出来。

添加 session.setDebug(true)。调试输出显示什么?

于 2013-05-23T00:24:06.210 回答