javaMail 问题,高手指点迷津

名剑传奇 2003-11-10 03:27:56
/*
* Created on 2003-11-7
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com;
import java.rmi.RemoteException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class MailSend {

public static void main(String[] args) {
String toAddr1 = "kxli@headsun.net";
String subject1 = "Test send mail";
String body = "我的邮件内容";
String fromAddr = "kxli@headsun.net";
MailSend t2 = new MailSend();
try {
t2.sendMail(toAddr1, subject1, body, fromAddr);
} catch (Exception ex) {
System.out.print("Error:");
}

}
public void sendMail(
String toAddr,
String subject,
String body,
String fromAddr)
throws RemoteException {
try {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "mail.headsun.net");
//Here we specify the SMTP server through
//which the mail should be delivered
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddr));
//Specify the From Address
InternetAddress[] tos = InternetAddress.parse(toAddr);
System.out.print(tos[0].toString());
//Specify the To Address
msg.setRecipients(Message.RecipientType.TO, tos);
msg.setSubject(subject);
//Specify the Subject
msg.setText(body);
//Specify the Body
Transport.send(msg);
System.out.println("Message is Sent");
} catch (MessagingException e) {
System.out.println(e);
}
}
}

//这是我的代码!系统需要的包(j2ee.jar、mail.jar、imap.jar、sunjce_provider.jar、mailapi.jar、pop3.jar、smtp.jar、activation.jar)也都方好了。
运行处了错误:javax.mail.NoSuchProviderException: smtp
跟踪单步运行主要是在Transport.send(msg);这个函数过不去,请高手出手帮助,后期我写完了BugWin(类似BugZilla功能)之后送你一个,功劳算你一份。
...全文
33 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
名剑传奇 2003-11-11
  • 打赏
  • 举报
回复
To:ChDw(米):
您好!
您的程序我调试了之后还是在以下句子出现了同样的错误,不知您是否却是测试通过?!我可急死了啊,我的BugWin系统被卡住了。
Transport trans = session.getTransport();
ChDw 2003-11-10
  • 打赏
  • 举报
回复
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.host", "111.222.333.444");

Session session = Session.getInstance(prop);

Transport trans = session.getTransport();
MimeMessage msg = new MimeMessage(session);
trans.connect();
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent("中文Message","text/plain;charset=gb2312");
mp.addBodyPart(mbp);

MimeBodyPart fp = new MimeBodyPart();
//fp.setDataHandler(new DataHandler(new FileDataSource("C:/abc.exe")));
fp.setFileName("中文abc.exe");
mp.addBodyPart(fp);

msg.setContent(mp);
msg.setSubject("中文Title");
//msg.setRecipient(Message.RecipientType.TO,new InternetAddress());
trans.sendMessage(msg,new Address[]{ new InternetAddress("abc@bac.com")});
// sendmessage,使用trans来传送,不要用静态方法
trans.close();

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧