我能';不要使用javamail发送电子邮件

我能';不要使用javamail发送电子邮件,java,email,jakarta-mail,Java,Email,Jakarta Mail,我正在使用javamail,我尝试过使用身份验证,或者不使用身份验证,但它不起作用。 有人能帮忙吗 private boolean InitMailer(String sSMPTGateWay) { try { oProps=new Properties(); oProps.put("mail.smtp.host",sSMPTGateWay); //oProps.put("mail.smtp.socketFactory.port"

我正在使用javamail,我尝试过使用身份验证,或者不使用身份验证,但它不起作用。 有人能帮忙吗

 private boolean InitMailer(String sSMPTGateWay)
{
    try
    {
        oProps=new Properties();
        oProps.put("mail.smtp.host",sSMPTGateWay);
        //oProps.put("mail.smtp.socketFactory.port", "465");
        //oProps.put("mail.smtp.port", "465");
        oProps.put("mail.smtp.starttls.enable", "true");

      //  oProps.put("mail.smtp.socketFactory.class",
    //      "javax.net.ssl.SSLSocketFactory");
    oProps.put("mail.smtp.auth", "true");
    //oProps.put("mail.smtp.port", "465");

        Authenticator auth = new javax.mail.Authenticator() {
                            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("mmyemail@gmail.com","mypassword");
            }
        };

        oSession=Session.getInstance(oProps,auth);
        oSession.setDebug(true);
        oTransport=oSession.getTransport(oSession.getProvider("smtp"));
        System.out.println("tento di connettermi");
        oTransport.connect();
        System.out.println("connesso?");
        if(!oTransport.isConnected())
        {
            ErrorLog.warningLog("MService: MService(): Mail Server "+sMailGateWay+" non operativo. Tento un altro Gateway.");
            return false;
        }
   }
    catch(Exception e)
    {
        e.printStackTrace();
        ErrorLog.warningLog("MService: MService(): Mail Server "+sMailGateWay+" non operativo. Tento un altro Gateway.");
        return false;
    }
    return true;
}

private void sendMessage(String sFrom,String sTo,String sSubject,String sBody) throws Exception
{
        if(Constants.ENABLE_MAIL_SERVICE.equals("NO"))
        {
            ErrorLog.warningLog("Tentativo di invio, Mail disabilitata: \n From:"+sFrom+"\n To:"+ sTo +"\n Subject: "+sSubject + "\n Body:\n"+sBody);
            return;
        }
        if(Constants.ENABLE_MAIL_SERVICE.equals("TEST"))
            sTo=Constants.TEST_MAIL;

        MimeMessage msg=new MimeMessage(oSession);
        msg.setFrom(new InternetAddress(sFrom));
        msg.addRecipient(Message.RecipientType.TO,new InternetAddress(sTo));
        msg.setSubject(sSubject,"iso-8859-1");
        msg.setSentDate(new Date());
        msg.setText(sBody,"iso-8859-1");
        System.out.println("mando mex");
        oTransport.send(msg);
}
这是一个错误,虽然不是错误,但是电子邮件没有发送

220 smtp.gmail.com ESMTP q4sm9246160wjk.24 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 25

EHLO Michele
250-smtp.gmail.com at your service, [79.36.174.226]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
connesso?
NOOP
250 2.0.0 OK q4sm9246160wjk.24 - gsmtp
new MSERVICE
starcinema@gmail.com
Sending from (starcinemafe@gmail.com) to (starcinema@gmail.com) subject (Registrazione Star Cinema) html(false).
mando mex1
mando mex
NOOP
250 2.0.0 OK q4sm9246160wjk.24 - gsmtp

有时,当您使用本地smtp服务器时,ISP会阻止端口
25

如果您使用的是主机提供商提供的SMTP服务器,则需要验证用户名和密码。javax.mail.PasswordAuthentication类用于验证密码

尝试更改
properties.put(“mail.smtp.port”,“25”)

properties.put(“mail.smtp.port”,“587”)/
465还包括选项


检查您是否没有得到任何解决方案

修复所有这些问题,您将获得进一步的解决方案。特别是,请调用oTransport.sendMessage而不是oTransport.send。

您发布的错误与属性有关。换衣服

    oProps.put("mail.smtp.auth", "true");
    oProps.put("mail.smtp.starttls.enable", "true");
    oProps.put("mail.smtp.host", "smtp.gmail.com");
    oProps.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    oProps.put("mail.smtp.port", "587");