使用javax发送电子邮件?

时间:2015-12-21 12:39:36

标签: java email javamail javax.mail

我想使用javax.mail发送电子邮件。当我运行以下代码时,它给我这个错误没有hwangheera4hagarmaher的提供者。问题在哪里,我该如何解决这个错误?

我使用的代码是:

public static boolean SendMail (String subject, String from, String Password, String Message, String to[])
{
    String Host = "hwangheera4hagarmaher@gmail.com";

    Properties Property = System.getProperties();
    Property.put("mail.hwangheera4hagarmaher.starttls.enable", true);
    Property.put ("mail.hwangheera4hagarmaher.host", Host);
    Property.put ("mail.hwangheera4hagarmaher.user", from);
    Property.put ("mail.hwangheera4hagarmaher.password", Password);
    Property.put ("mail.hwangheera4hagarmaher.port", 587);
    Property.put ("mail.hwangheera4hagarmaher.auth", "true");

    Session session = Session.getDefaultInstance(Property, null);

    MimeMessage message = new MimeMessage (session);

    try
    {
        message.setFrom(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(RecipientType.TO, toAddress[i]);
        }

        message.setSubject (subject);
        message.setText(Message);

        Transport transport = session.getTransport("hwangheera4hagarmaher");
        transport.connect (Host, from, Password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

        return true;
    }
    catch (MessagingException ex)
    {
        showMessageDialog (null, ex);
    }
return false;
}

1 个答案:

答案 0 :(得分:1)

hwangheera4hagarmaher替换为所有道具中的smtpgetTransport () 除非您已自行使用该名称对提供商进行编码。

您的主机名应该是服务器,而不是电子邮件地址。也许smtp.gmail.com

默认情况下,Google提供使用任何外部访问权限的安全预防措施,尤其是如果您使用某些电子邮件框架来访问您的帐户,首先您需要授予访问权限,登录您的帐户然后转到:

https://accounts.google.com/DisplayUnlockCaptcha

然后按照其余内容访问您的帐户。