今天试了下JAVAMAIL的代码,郁闷到吐血,他们的邮箱都可以发送成功,我的邮箱就报
javax.mail.AuthenticationFailedException at javax.mail.Service.connect(Service.java:319) at javax.mail.Service.connect(Service.java:169) at javax.mail.Service.connect(Service.java:118) at javax.mail.Transport.send0(Transport.java:188) at javax.mail.Transport.send(Transport.java:118) at HelloJMail.sendMail(HelloJMail.java:48) at HelloJMail.main(HelloJMail.java:56)
我改了好多次密码都不行,密码绝对没有输入错误,我也试了我的另外的邮箱也是失败,非常的郁闷,值到最后我无意在网上看到,一些邮箱服务器把新注册的邮箱不支持pop3协议,我就找别人拿几年前注册的邮箱,果然可以.郁闷死了
记的以前有写过不用密码就可以发送的,
结果发现现在已经不能使用的.
其实是有些邮箱服务商为了避免别人乱发广告而加了一个需要密码验证
// 这样才能通过验证 props.setProperty("mail.smtp.auth", "true");
我同学尝试着用循环发送邮件,其实到最后只收到一封,而且发多了会报
javax.mail.MessagingException: Can't send command to SMTP host; nested exception is: java.net.SocketException: Software caused connection abort: socket write error at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408) at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1358) at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:836) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375) at javax.mail.Service.connect(Service.java:297) at javax.mail.Service.connect(Service.java:156) at javax.mail.Service.connect(Service.java:105) at javax.mail.Transport.send0(Transport.java:168) at javax.mail.Transport.send(Transport.java:98) at com.zhaipuhong.j2ee.jmail.HelloJMail.sendMail(HelloJMail.java:53) at com.zhaipuhong.j2ee.jmail.HelloJMail.main(HelloJMail.java:61) Caused by: java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101) at java.io.BufferedOutputStream.flushBuffer(Unknown Source) at java.io.BufferedOutputStream.flush(Unknown Source) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1418) ... 11 more
而且帐户可能会被封,所以小心小心.
其实发送邮件还有很多开源的架包,javamail只是一种,具体问题具体分析,这就不说了.
底下是我在网上找的一个javamail的例子,具体的地方已经不知道是哪了,先引用下
import javax.mail.*; import java.util.*; import javax.mail.internet.*; import java.io.*; import javax.activation.*; public class JavaMail { String m_user = ""; // 用于需要验证的用户名 String m_pass = ""; // 口令 String m_server = ""; // SMTP服务器名称 int m_port = 25; // 端口号,缺省为25 String m_from = ""; // 发件人 String m_to = ""; // 收件人 String m_cc = null; // 抄送 String m_bcc = null; // 密送 String m_subject = null; // 主题 String m_body = null; // 内容体 LinkedList m_attach = null; // 附件的文件名列表 JavaMail() { } // 设定smtp服务器用户名(可选) public void setUser(String user) { m_user = user; } // 设定smtp服务器口令(可选) public void setPass(String pass) { m_pass = pass; } // 设定smtp的地址和端口号(可选) public void setServer(String server, int port) { if (port > 0) { m_port = port; } m_server = server; } // 设定smtp服务器地址,端口号使用缺省值 public void setServer(String server) { m_server = server; } // 发件人 public void setFrom(String from) { m_from = from; } // 收件人 public void setTo(String to) { m_to = to; } // 抄送(可选) public void setCc(String cc) { m_cc = cc; } // 密送(可选) public void setBcc(String bcc) { m_bcc = bcc; } // 设置新的主题(可选) public void setSubject(String subject) { m_subject = subject; } // 设置信的内容,若以<html>或<HTML>开头的内容为text/html格式 public void setBody(String body) { m_body = body; } // 可以多次添加附件,文件名最好是全路径,使用相对路径容易出错 public void attach(String attachfile) { if (m_attach == null) { m_attach = new LinkedList(); } m_attach.add(attachfile); } public boolean sendMail() throws Exception { Properties props = System.getProperties(); props.setProperty("mail.transport.protocol", "smtp"); // smtp协议 props.setProperty("mail.smtp.host", m_server); // 服务器地址 props.setProperty("mail.smtp.port", "" + m_port); // 端口号 // 认证信息 if (m_user != null && m_pass != null && m_user.length() > 0 && m_pass.length() > 0) { // 这样才能通过验证 props.setProperty("mail.smtp.auth", "true"); } javax.mail.Session sess = javax.mail.Session.getDefaultInstance(props, null); MimeMessage msg = new MimeMessage(sess); // 发件人 msg.setFrom(new InternetAddress(m_from)); // 收件人、抄送、密送 msg.setRecipients(Message.RecipientType.TO, InternetAddress .parse(m_to)); if (m_cc != null && m_cc.length() > 0) { msg.setRecipients(Message.RecipientType.CC, InternetAddress .parse(m_cc)); } if (m_bcc != null && m_bcc.length() > 0) { msg.setRecipients(Message.RecipientType.BCC, InternetAddress .parse(m_bcc)); } // 主题 if (m_subject != null) { msg.setSubject(m_subject); // 发件日期 } msg.setSentDate(new Date()); // 文本内容 MimeMultipart content = new MimeMultipart(); if (content == null) { return false; } MimeBodyPart part; if (m_body != null && m_body.length() > 0) { part = new MimeBodyPart(); String type = "text/plain"; if (m_body.startsWith("<html>") || m_body.startsWith("<HTML>")) { type = "text/html"; } part.setText(m_body); // 以<html>和<HTML>开始的文本为text/html类型 part.setHeader("Content-Type", type); content.addBodyPart(part); } // 添加附件 if (m_attach != null) { String filename; Iterator it = m_attach.iterator(); while (it.hasNext()) { filename = (String) it.next(); if (filename == null || filename.length() <= 0) { continue; } part = new MimeBodyPart(); FileDataSource fds = new FileDataSource(filename); part.setDataHandler(new DataHandler(fds)); part.setFileName(MimeUtility.encodeText(fds.getName())); content.addBodyPart(part); } } // 保存修改 msg.setContent(content); msg.saveChanges(); // 如果需要验证用户(如:smtp.sina.com.cn),则使用connect并sendMessage // 注意必须设定mail.smtp.auth属性为true if (m_user != null && m_pass != null && m_user.length() > 0 && m_pass.length() > 0) { Transport trans = sess.getTransport(); trans.connect(m_server, m_user, m_pass); trans.sendMessage(msg, InternetAddress.parse(m_to)); trans.close(); } // 否则,不需要用户名和口令使用静态方法发送由件 else { Transport.send(msg); } return true; } public static void main(String argv[]) throws Exception { JavaMail smtp = new JavaMail(); smtp.setFrom(""); smtp.setTo(""); smtp.setCc(""); smtp.setServer("smtp.126.com"); smtp.setUser(""); smtp.setPass(""); smtp.setSubject("你好,测试由件"); smtp.setBody("<html>你好啊</html>"); smtp.attach("c://test.java"); boolean ret = smtp.sendMail(); if (ret) { System.out.println("发送成功"); } else { System.out.println("发送失败"); } } }