【问题标题】:Getting error while sending mail in java在java中发送邮件时出错
【发布时间】:2013-12-03 17:53:31
【问题描述】:

我正在尝试使用 java 发送邮件。当我将它与 GMail 的 SMTP 服务器(“smtp.gmail.com”)一起使用时,我使用的代码有效。但是当我更改代码(smtp_host_name、smtp_port 属性和身份验证 UserID/Pwd)以通过我的 ISP 提供商的 SMTP 服务器发送电子邮件时,此代码失败。

这是我的代码-

String host = "smtp server host of my isp provider";

    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.debug", "true");
    props.put("mail.transport.protocol.", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.starttls.enable","true"); 

    Session mailSession = Session.getDefaultInstance(props, null);
    mailSession.setDebug(sessionDebug);

    Message msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setContent(messageText, "text/html");

    Transport transport = mailSession.getTransport("smtp");
    transport.connect(host, user, pass);

我得到了这个错误-

 javax.mail.MessagingException: Exception reading response; 
 nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

问题是我能够从我的系统工作区使用相同的代码发送邮件。但是在我将war文件上传到主机后,我得到了上述错误。我想我禁用了 SSL 但仍然收到此错误。有人可以帮忙吗。紧急...

我按照下面给出的步骤操作,但仍然遇到同样的错误。 这是我现在的代码-

 String host = "mail.myweb.com", user = emailUser, pass = emailPasswd;

    String to = emailID;
    String from = emailFrom;
    String subject = subj;
    String messageText = message;
    boolean sessionDebug = true;

    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.debug", "true");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "25");

    Session mailSession = Session.getDefaultInstance(props, null);
    mailSession.setDebug(sessionDebug);

    Message msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setContent(messageText, "text/html");

    Transport transport = mailSession.getTransport("smtp");
    transport.connect(host, user, pass);

    try {
        transport.sendMessage(msg, msg.getAllRecipients());
    }
    catch (Exception e) {
        System.out.println("Error" + e.getMessage());
    }
    transport.close();
}

这是完整的堆栈跟踪

 javax.mail.MessagingException: Exception reading response; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
javax.mail.Service.connect(Service.java:288)
javax.mail.Service.connect(Service.java:169)
common.sendMail.(sendMail.java:35)
frontend.reply.sendfeedback(reply.java:74)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)
org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
java.lang.Thread.run(Thread.java:662)

【问题讨论】:

  • 您正尝试在非 SSL 连接上使用 SSL,正如错误所说。您的最后一个属性明确地做到了这一点。
  • 我删除了最后一个属性,但仍然无法正常工作......
  • @BhaskarSharma 在下面查看我的答案。
  • @BhaskarSharma,您找到解决方案了吗?我也面临同样的问题。

标签: java ssl jakarta-mail


【解决方案1】:

这一行

props.put("mail.smtp.starttls.enable","true"); 

正在此连接上启用 SSL;改成假的,或者干脆把它拿出来。

此外,该属性名称末尾的点也是可疑的,尽管可能是无害的:

props.put("mail.transport.protocol.", "smtp");

【讨论】:

  • 嗨,我试过了,但还是不行。我不明白为什么。
【解决方案2】:

您好尝试为非 ssl smtp 连接添加以下属性

    props.put("mail.smtp.starttls.enable","false");
    props.put("mail.smtp.socketFactory.port", "25");
    props.put("mail.smtp.socketFactory.class", "");
    props.put("mail.smtp.socketFactory.fallback", "false");

例如 ssl 连接属性是

    Properties props = new Properties();
    props.put("mail.smtp.username", "email@gmail.com");
    props.put("mail.smtp.password", "password");
    props.put("mail.smtp.protocol", "smtp");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", "587");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

非 ssl 属性是

    Properties props = new Properties();
    props.put("mail.smtp.user", "email@mydomain.com");
    props.put("mail.smtp.password", "password");
    props.put("mail.smtp.protocol", "smtp");
    props.put("mail.smtp.host", "smtp.mydomain.com");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.starttls.enable","false");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", "25");
    props.put("mail.smtp.socketFactory.class", "");
    props.put("mail.smtp.socketFactory.fallback", "false");

【讨论】:

    【解决方案3】:

    您需要使用smtps 协议获取Transport 对象。使用Java Mail API 1.4.1 或更高版本。
    我不确定早期版本是否支持此功能。
    smtp.jarimap.jar 中的每一个都包含一个名为 javamail.providers 的资源文件,其中包含以下条目:

    # JavaMail SMTP provider Sun Microsystems, Inc
    protocol=smtp; type=transport; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc;
    protocol=smtps; type=transport; class=com.sun.mail.smtp.SMTPSSLTransport; vendor=Sun Microsystems, Inc;  
    
    # JavaMail IMAP provider Sun Microsystems, Inc
    protocol=imap; type=store; class=com.sun.mail.imap.IMAPStore; vendor=Sun Microsystems, Inc;
    protocol=imaps; type=store; class=com.sun.mail.imap.IMAPSSLStore; vendor=Sun Microsystems, Inc;
    

    这些资源是在您创建邮件会话对象时加载的。 DEBUG跟踪如下:

    DEBUG: JavaMail version 1.4.5
    DEBUG: URL jar:file:/E:/Ravi/work/eclipse-workspace/temp/WebContent/WEB-INF/lib/smtp.jar!/META-INF/javamail.providers
    DEBUG: successfully loaded resource: jar:file:/E:/Ravi/work/eclipse-workspace/temp/WebContent/WEB-INF/lib/smtp.jar!/META-INF/javamail.providers
    DEBUG: Tables of loaded providers
    DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsy stems, Inc]}
    DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsy stems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]}
    

    我们需要获取com.sun.mail.smtp.SMTPSSLTransport 的实例才能使用smtps 协议。

    Transport transport = mailSession.getTransport("smtps");
    

    由于资源有一个为此协议定义的提供者,Transport 对象会毫无例外地生成。

    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
    

    如果您的所有输入都正确,您将获得成功。 DEBUG 的示例执行如下所示:

    DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
    220 mx.google.com ESMTP d2sm12188996pbw.39
    DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
    
    EHLO sreekarreddy
    250-mx.google.com at your service, [117.195.223.189]
    250-SIZE 35882577
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    DEBUG SMTP: Found extension "SIZE", arg "35882577"
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    DEBUG SMTP: Attempt to authenticate
    DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
    DEBUG SMTP: AUTH LOGIN command trace suppressed
    DEBUG SMTP: AUTH LOGIN succeeded
    DEBUG SMTP: use8bit false
    MAIL FROM:<rrarepally@gmail.com>
    250 2.1.0 OK d2sm12188996pbw.39
    RCPT TO:<rrarepally@gmail.com>
    250 2.1.5 OK d2sm12188996pbw.39
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP:   rrarepally@gmail.com
    DATA
    354  Go ahead d2sm12188996pbw.39
    From: rrarepally@gmail.com
    To: rrarepally@gmail.com
    Message-ID: <19230370.0.1335124760953.JavaMail.Administrator@sreekarreddy>
    Subject: test email over smtps
    MIME-Version: 1.0
    Content-Type: text/html; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    hello world
    .
    250 2.0.0 OK 1335124739 d2sm12188996pbw.39
    QUIT
    221 2.0.0 closing connection d2sm12188996pbw.39
    

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2014-07-11
      • 2016-09-18
      相关资源
      最近更新 更多