我想做个邮箱用smtp和POP3,但是发送时候出现
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:768)
at javax.mail.Session.getTransport(Session.java:708)
at javax.mail.Session.getTransport(Session.java:651)
at javax.mail.Session.getTransport(Session.java:631)
at bb.Send.main(Send.java:22)
我的源代码是
- Java code
package bb;import java.util.Date;import java.util.Properties;import javax.mail.Message;import javax.mail.Provider;import javax.mail.Session;import javax.mail.Transport;import javax.mail.Message.RecipientType;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class Send { public static void main(String[] arg){ Properties properties=System.getProperties(); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.host", "127.0.0.1"); Session session=Session.getInstance(properties); try{ Transport transport=session.getTransport("smtp"); transport.connect("127.0.0.1", "bb","0"); Message txMsg=new MimeMessage(session); txMsg.setSubject("aaaaa"); txMsg.setFrom(new InternetAddress("bb")); txMsg.setRecipient(Message.RecipientType.TO,new InternetAddress("aa")); txMsg.setText("ccccccccc"); txMsg.setSentDate(new Date()); transport.sendMessage(txMsg, txMsg.getRecipients(Message.RecipientType.TO)); transport.close(); }catch(Exception e){ e.printStackTrace(); } System.out.print("---------发送完毕---------"); }}
大侠们介绍本关于邮箱的书,让小弟学习一下
------解决方案--------------------
这是由于在你应用的classpath里有两个不同版本的Javamail库导致的.
从你的应用/WEB-INF/lib目录里移除多余版本的Javamail库,重启服务即可。
------解决方案--------------------
你自己的机器没有提供smtp服务
也没提提供mail.smtp.port
是这行报的错吧
transport.connect("127.0.0.1", "bb","0");
想练手用gmail等提供smtp的邮箱试呗
------解决方案--------------------
我将源码贴出,希望对楼主有用:
package com.hisoft;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 MailMessage {
private MimeMessage mimeMsg; // MIME邮件对象
private Session session; // 邮件会话对象
private Properties props; // 系统属性
private boolean needAuth = false; // smtp是否需要认证
private String username = ""; // smtp认证用户名和密码
private String password = "";
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
/**
*
* 构造函数
*
* 如果没有指定邮件服务器,就指定一个邮件服务器.
*/
public MailMessage() {
// setSmtpHost("smtp.163.com");
// createMimeMessage();
}
/**
*
* 发送邮件前期准备
*
* @param smtp
*/
public void sendMail(String smtp) {