javamail发送失败的异常

时间:2010-01-09 11:43:33

标签: javamail

请帮我处理这段代码:

package com.ibm.lims;


import java.security.Security;
import java.util.Properties;


import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class mails {


private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";
private static final String emailMsgTxt = "robin borrowed a book";
private static final String emailSubjectTxt = "online library management system notification";
private static final String emailFromAddress = "robins.lims2009@gmail.com";
private static final String SSL_FACTORY ="javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = {"robins.lims2009@gmail.com","supersmartrobin@yahoo.co.in"};

public static void mailer() throws Exception{


Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

new mails().sendSSLMessage(sendTo,emailSubjectTxt,emailMsgTxt,emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");

}

public void sendSSLMessage(String recipients[],String subject,String message,String from) throws MessagingException {
boolean debug = true;

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol","smtps");

Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("robins.lims2009@gmail.com","xxxxxxxxx");
    }
    }
);



MimeMessage message1 =new MimeMessage(session);
message1.setFrom(new InternetAddress(from));
for(int i=0;i<recipients.length;i++){
message1.addRecipient(Message.RecipientType.TO,new InternetAddress(recipients[i]));
}



message1.setSubject(subject);


MimeBodyPart messageBodyPart =new MimeBodyPart();


messageBodyPart.setText(message);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);


message1.setContent(multipart);


Transport.send( message1 );



} 
}

我收到发送失败的异常asp>

Loading javamail.default.providers from jar:file:/C:/Program Files/IBM/WebSphere/AppServerCommunityEdition/repository/org/apache/geronimo/javamail/geronimo-javamail_1.4_mail/1.7/geronimo-javamail_1.4_mail-1.7.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=smtp, className=org.apache.geronimo.javamail.transport.smtp.SMTPTransport, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=smtps, className=org.apache.geronimo.javamail.transport.smtp.SMTPSTransport, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=nntp-post, className=org.apache.geronimo.javamail.transport.nntp.NNTPTransport, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=nntp-posts, className=org.apache.geronimo.javamail.transport.nntp.NNTPSSLTransport, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=nntp, className=org.apache.geronimo.javamail.store.nntp.NNTPStore, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=nntps, className=org.apache.geronimo.javamail.store.nntp.NNTPSSLStore, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=pop3, className=org.apache.geronimo.javamail.store.pop3.POP3Store, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=pop3s, className=org.apache.geronimo.javamail.store.pop3.POP3SSLStore, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=imap, className=org.apache.geronimo.javamail.store.imap.IMAPStore, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=imaps, className=org.apache.geronimo.javamail.store.imap.IMAPSSLStore, vendor=Apache Software Foundation, version=1.0
DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@17858a9; class=org.apache.geronimo.javamail.transport.smtp.SMTPTransport; vendor=Apache Software Foundation;version=1.0
DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@17858a9; class=org.apache.geronimo.javamail.transport.smtp.SMTPTransport; vendor=Apache Software Foundation;version=1.0
smtp DEBUG: Failing connection for missing authentication information
smtp DEBUG: Attempting plain socket connection to server smtp.gmail.com:465
220 mx.google.com ESMTP 20sm19490287pzk.13
EHLO smartrobin
250-mx.google.com at your service, [121.242.109.66]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING
smtp DEBUG: Processing extension SIZE 35651584
smtp DEBUG: Processing extension 8BITMIME
smtp DEBUG: Processing extension AUTH LOGIN PLAIN
smtp DEBUG: Processing extension ENHANCEDSTATUSCODES
smtp DEBUG: Processing extension PIPELINING
QUIT
221 2.0.0 closing connection 20sm19490287pzk.13
smtp DEBUG: Failing connection for missing authentication information
smtp DEBUG: Attempting plain socket connection to server smtp.gmail.com:465
220 mx.google.com ESMTP 23sm19555940pzk.4
EHLO smartrobin
250-mx.google.com at your service, [121.242.109.66]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING
smtp DEBUG: Processing extension SIZE 35651584
smtp DEBUG: Processing extension 8BITMIME
smtp DEBUG: Processing extension AUTH LOGIN PLAIN
smtp DEBUG: Processing extension ENHANCEDSTATUSCODES
smtp DEBUG: Processing extension PIPELINING
QUIT
221 2.0.0 closing connection 23sm19555940pzk.4
2010-01-09 17:08:49,046 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
javax.mail.MessagingException: Server doesn't support required transport level security
    at org.apache.geronimo.javamail.transport.smtp.SMTPConnection.sendHandshake(SMTPConnection.java:821)
    at org.apache.geronimo.javamail.transport.smtp.SMTPConnection.protocolConnect(SMTPConnection.java:159)
    at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:165)
    at javax.mail.Service.connect(Service.java:271)
    at javax.mail.Service.connect(Service.java:91)
    at javax.mail.Service.connect(Service.java:76)
    at javax.mail.Transport.send(Transport.java:94)
    at javax.mail.Transport.send(Transport.java:48)
    at com.ibm.lims.mails.sendSSLMessage(mails.java:86)
    at com.ibm.lims.mails.mailer(mails.java:35)
    at org.apache.jsp.borrow_jsp._jspService(borrow_jsp.java:142)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.geronimo.tomcat.valve.DefaultSubjectValve.invoke(DefaultSubjectValve.java:56)
    at org.apache.geronimo.tomcat.GeronimoStandardContext$SystemMethodValve.invoke(GeronimoStandardContext.java:406)
    at org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke(GeronimoBeforeAfterValve.java:47)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
    at org.apache.geronimo.tomcat.valve.ThreadCleanerValve.invoke(ThreadCleanerValve.java:40)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

您的身份验证凭据似乎无效。如果您尝试直接在连接线中设置用户名/密码,它是否有效?例如:

        Transport transport = session.getTransport("smtp");
        transport.connect(SMTP_HOST_NAME, "robins.lims2009@gmail.com","xxxxxxxxx");
        transport.sendMessage(message1, message1.getAllRecipients());
        transport.close();