发送邮件时获取java.lang.StringIndexOutOfBoundsException

时间:2018-02-14 12:46:41

标签: java javamail

尝试使用JavaMail 1.6.0版通过gmail发送电子邮件时,我收到 stringOutofBoundexception

调试细节是:

DEBUG: setDebug: JavaMail version 1.6.0
DEBUG: getProvider() returning 
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

错误详情:

  

线程中的异常" main" java.lang.StringIndexOutOfBoundsException:   字符串索引超出范围:-1。在java.lang.String.substring(未知   来源)at   com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:289)     在com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)     在   com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2160)     在   com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:726)     在javax.mail.Service.connect(Service.java:366)at   sendEmail.main(sendEmail.java:50)

这是我的代码:

import java.util.Properties;

import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
public class sendEmail  extends Authenticator  
{  
    public static void main(String[] args) throws MessagingException{ 



String  d_email = "oracle.ossa.qa@gmail.com",
        d_uname = "oracle.ossa.qa@gmail.com",
        d_password = "mypass",
        d_host = "smtp.gmail.com",
        d_port  = "465", //465,587
        m_to = "oracle.ossa.qa@gmail.com",
        m_subject = "OPEN BUGS";
  String m_text = "text";

Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
//props.put("mail.verbose",true);

props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.proxy.host","http://www-proxy-hqdc.us.oracle.com");
props.put("mail.smtp.proxy.port","80");

//Authenticator auth = new Authenticator();
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication  getPasswordAuthentication() {
        return new PasswordAuthentication(d_email, "oracle@ossa");
    }
  });
session.setDebug(true);

MimeMessage msg = new MimeMessage(session);

try {
    msg.setSubject(m_subject);
    msg.setFrom(new InternetAddress(d_email));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

Transport transport = session.getTransport("smtp");
        transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
        transport.send(msg, msg.getAllRecipients());
        transport.close();

    } catch (AddressException e) {
        e.printStackTrace();
        //return false;
    } catch (MessagingException e) {
        e.printStackTrace();
       // return false;
    }
 }
}

1 个答案:

答案 0 :(得分:0)

每{{}}},mail.smtp.proxy.host仅为主机名,不指定协议(例如http://)。