问题描述
- java maile异常 !!!
-
求救啊!本人在本地测试、和国内的服务器测试时,发送邮件正常。公司在香洪租了个服务器,在上面布属时报错啊,求高手指教啊!代码如下:public class SendMailUtil { private static Properties props=new Properties(); static{ //获取文中的各参数 InputStream is=SendMailUtil.class.getClassLoader().getResourceAsStream("MailPramps.properties"); try { props.load(is); } catch (IOException e) { e.printStackTrace(); } } /** * * @param toAdd 收件人 * @param subject 邮件标题 * @param context 邮件内容 * @throws MessagingException 邮件信息异常 * @throws UnsupportedEncodingException */ public static String sendMail(String toAdd,String subject,String context) { String flagString = null; Authenticator authenticator = new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication(){ String user=props.getProperty("mail.user"); String password=props.getProperty("mail.password"); return new PasswordAuthentication(user,password); } }; // Session mailSession=Session.getDefaultInstance(props, authenticator); Session mailSession=Session.getInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { String user=props.getProperty("mail.user"); String password=props.getProperty("mail.password"); return new PasswordAuthentication(user,password); } }); // Session mailSession=Session.getInstance(props, authenticator); try{ mailSession.setDebug(true); MimeMessage message=new MimeMessage(mailSession) ; InternetAddress from=new InternetAddress(MimeUtility.encodeText("Colamark")+"<"+props.getProperty("mail.user")+">"); message.setFrom(from); InternetAddress to=new InternetAddress(toAdd); message.setRecipient(RecipientType.TO,to); InternetAddress cc=new InternetAddress(MimeUtility.encodeText("Colamark")+"<"+props.getProperty("mail.user")+">"); message.setRecipient(RecipientType.BCC,cc); message.setSubject(subject); message.setContent(context,"text/html;charset=UTF-8"); Transport.send(message); }catch(Exception e){ return "error"; } return flagString; } }
异常如下:
public class SendMailUtil { private static Properties props=new Properties(); static{ //获取文中的各参数 InputStream is=SendMailUtil.class.getClassLoader().getResourceAsStream("MailPramps.properties"); try { props.load(is); } catch (IOException e) { e.printStackTrace(); } } /** * * @param toAdd 收件人 * @param subject 邮件标题 * @param context 邮件内容 * @throws MessagingException 邮件信息异常 * @throws UnsupportedEncodingException */ public static String sendMail(String toAdd,String subject,String context) { String flagString = null; Authenticator authenticator = new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication(){ String user=props.getProperty("mail.user"); String password=props.getProperty("mail.password"); return new PasswordAuthentication(user,password); } }; // Session mailSession=Session.getDefaultInstance(props, authenticator); Session mailSession=Session.getInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { String user=props.getProperty("mail.user"); String password=props.getProperty("mail.password"); return new PasswordAuthentication(user,password); } }); // Session mailSession=Session.getInstance(props, authenticator); try{ mailSession.setDebug(true); MimeMessage message=new MimeMessage(mailSession) ; InternetAddress from=new InternetAddress(MimeUtility.encodeText("Colamark")+"<"+props.getProperty("mail.user")+">"); message.setFrom(from); InternetAddress to=new InternetAddress(toAdd); message.setRecipient(RecipientType.TO,to); InternetAddress cc=new InternetAddress(MimeUtility.encodeText("Colamark")+"<"+props.getProperty("mail.user")+">"); message.setRecipient(RecipientType.BCC,cc); message.setSubject(subject); message.setContent(context,"text/html;charset=UTF-8"); Transport.send(message); }catch(Exception e){ return "error"; } return flagString; } }
异常如下:
250-smtp.aliyun-inc.com
250-8BITMIME
250-AUTH=PLAIN LOGIN XALIOAUTH
250-AUTH PLAIN LOGIN XALIOAUTH
250-PIPELINING
250 DSN
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN XALIOAUTH"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XALIOAUTH"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:user@colamark.cn
250 Mail Ok
RCPT TO:jingde.liu@colamark.cn
250 Rcpt Ok
RCPT TO:user@colamark.cn
DEBUG SMTP: exception reading response, THROW:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2182)
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1690)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at cn.colamark.pc.util.SendMailUtil.sendMail(SendMailUtil.java:87)
at cn.colamark.pc.action.user.RegistAction.execute(RegistAction.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java
:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.
java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflo
wInterceptor.java:165)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterce
ptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor
.java:252)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(Ann
otationValidationInterceptor.java:68)望高手指点啊
解决方案
是不是scoket的一端关闭了