我正在使用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
答案 0 :(得分:1)
修复所有这些common JavaMail mistakes,你会得到更多。特别是,调用oTransport.sendMessage而不是oTransport.send。
答案 1 :(得分:0)
当您使用本地smtp服务器时,有时候您的ISP会阻止端口25
如果您使用的是主机提供商提供的SMTP服务器,则需要验证用户名和密码。 javax.mail.PasswordAuthentication类用于验证密码。
尝试更改properties.put("mail.smtp.port", "25");
到properties.put("mail.smtp.port", "587");
// 465 also option
检查您是否未获得任何解决方案Java mail mistakes
答案 2 :(得分:0)
您发布的错误与属性有关。改变它。
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");