读书人

关于邮箱有关问题

发布时间: 2012-03-06 20:47:55 作者: rapoo

关于邮箱问题
我想做个邮箱用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) {



setSmtpHost(smtp);

createMimeMessage();

}

/**
*
* 设置SMTP主机
*
* @param hostName
*/

public void setSmtpHost(String hostName)

{

System.out.println("设置系统属性:mail.smtp.host = " + hostName);

if (props == null)
props = System.getProperties(); // 获得系统属性对象

props.put("mail.smtp.host", hostName);

}

/**
*
* @return boolean
*/

public boolean createMimeMessage()

{
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
} catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}

System.out.println("准备创建MIME邮件对象!");

try {

mimeMsg = new MimeMessage(session); // 创建MIME邮件对象

mp = new MimeMultipart();

return true;

}

catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);

return false;

}

}

/**
*
*设置smtp身份认证
*
* @param need
* boolean
*/

public void setNeedAuth(boolean need) {

System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);

if (props == null)
props = System.getProperties();

if (need) {

props.put("mail.smtp.auth", "true");

} else {

props.put("mail.smtp.auth", "false");

}

}

/**
*
* 设置用户密码
*/

public void setNamePass(String name, String pass) {

username = name;

password = pass;

}

/**
*
* 设置邮件主题
*
* @param mailSubject
*
* @return
*/

public boolean setSubject(String mailSubject) {

System.out.println("设置邮件主题!");

try {

mimeMsg.setSubject(mailSubject);

return true;

}

catch (Exception e) {

System.err.println("设置邮件主题发生错误!");

return false;

}

}

/**
*
* 设置邮件内容
*/

public boolean setBody(String mailBody) {

try {

BodyPart bp = new MimeBodyPart();

bp.setContent("" + mailBody, "text/html;charset=GB2312");

mp.addBodyPart(bp);

return true;

}

catch (Exception e) {

System.err.println("设置邮件正文时发生错误!" + e);

return false;

}

}

/**
*
* 设置邮件附件
*
* @param filename
* 完整的文件路径名
*
* @return
*/

public boolean addFileAffix(String filename) {

try {

BodyPart bp = new MimeBodyPart();

FileDataSource fileds = new FileDataSource(filename);

System.out.println("a");

bp.setDataHandler(new DataHandler(fileds));

System.out.println("b");

bp.setFileName(fileds.getName());

System.out.println("c");

mp.addBodyPart(bp);

System.out.println("d");

return true;

}

catch (Exception e) {

System.err.println("增加邮件附件:" + filename + "发生错误!" + e);

return false;

}

}

/**
*
* 设置发信人
*
* @param from
*
* @return
*/

public boolean setFrom(String from) {



System.out.println("设置发信人!");

try {

mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人

return true;

}

catch (Exception e)

{

return false;

}

}

/**
*
* 邮件接受人
*
* @param to
*
* @return
*/

public boolean setTo(String to) {

if (to == null)
return false;

try {

mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));

return true;

}

catch (Exception e)

{
return false;
}

}

public boolean setCopyTo(String copyto)

{

if (copyto == null)
return false;

try {

mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));

return true;

}

catch (Exception e)

{
return false;
}

}

/**
*
* 邮件发送
*
* @return
*/

public boolean sendout()

{

try {

mimeMsg.setContent(mp);

mimeMsg.saveChanges();

System.out.println("正在发送邮件....");

Session mailSession = Session.getInstance(props, null);

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

transport.connect((String) props.get("mail.smtp.host"), username,
password);

transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));

// transport.send(mimeMsg);

System.out.println("发送邮件成功!");

transport.close();

return true;

}

catch (Exception e)

{

System.err.println("邮件发送失败!" + e);

return false;

}

}

/**
*
* @param args
*/

public static void main(String[] args) {

String mailbody = ""
+ "hi:Luciferoffans 最近有什么好看的电影吗?平时也懒得去下它们了,我现在一直在期待小罗伯特唐丽的《钢铁侠2》5月7号全球上映,一起去电影院去看啊?呵呵~~~~";

MailMessage themail = new MailMessage();

themail.sendMail("smtp.163.com");

themail.setNeedAuth(true);

/* 上传一个附件 */

File file= new File("D:/Program Files/11.xlsx");
String filename=file.getPath();
System.out.println(filename);
themail.addFileAffix(filename);

/*上传多个附件*/
/*
List<String> list = new ArrayList<String>();

File f = new File("D:/Program Files");

File[] subfile = f.listFiles();

for (File item : subfile) {

boolean x = item.getName().endsWith("xlsx");

if (x)

{

list.add(item.getAbsolutePath());

}

}

System.out.println(list);

for (String itam : list)

{

System.out.println(itam);

if (themail.addFileAffix(itam) == false)
return;

}
*/
if (themail.setSubject("努力学技术,努力挣钱") == false)
return; // 邮件主题

if (themail.setBody(mailbody) == false)
return;

if (themail.setTo("laolangtou@163.com") == false)
return; // 接受邮件地址

if (themail.setFrom("laolangtou@163.com") == false)
return; // 发送邮件地址

/* if(themail.addFileAffix("c:\\boot.ini") == false) return; */

themail.setNamePass("laolangtou@163.com", "123456");

if (themail.sendout() == false)
return;

}
}

------解决方案--------------------


new InternetAddress("aa"));
这里有问题吧
[解决办法]
邮件基本常识问题

你本机没有smtp服务,你调用smtp发送怎么可能成功

你可以借用其他的smtp服务器 ,比如 163,gmail的 。

javamail 中的api只给你提供了发送邮件的动作,至于怎么发送出去的,是需要smtp服务来进行的

读书人网 >J2EE开发

热点推荐