javaMail 问题,高手指点迷津

名剑传奇 2003-11-11 09:11:13
/*
* 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功能)之后送你一个,功劳算你一份。
...全文
121 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Unicorn008 2003-11-27
  • 打赏
  • 举报
回复
Properties props = System.getProperties();
props.put("mail.smtp.auth","true");
Session session = Session.getInstance(props,null);
Transport transport = session.getTransport("smtp");
transport.connect(mailserver,user,password);
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
Frank_Xu 2003-11-27
  • 打赏
  • 举报
回复
Session session = Session.getDefaultInstance 這句中變量要修改.
session是內部對象,不要用來作變量. 可以改為其它的字符,如mailsession等.

Transport.send(msg);這句最好修改為:
Transport trs=mailsession.getTransport("smtp");
trs.connect(SMTP主機,用戶,口令); //修改成為你的對應值
trs.sendMessage(msg,msg.getAllRecipients());
名剑传奇 2003-11-23
  • 打赏
  • 举报
回复
随便写,我给分结贴

81,094

社区成员

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

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