急。javamail发送问题!在线等待

water_zym 2009-08-31 10:32:40
Properties props=new Properties();
props.put("mail.pop3.host","pop3.sohu.com");
Session sess = Session.getDefaultInstance(props,null);
sess.setDebug(true);
MimeMessage msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,"wuweixiong_ren@sohu.com");
msg.setSubject(Tmpsubject);
msg.setText(Tmpcontent);
msg.setSentDate(new Date());
Transport.send(msg);

我的程序在运行的时候一直提示错误信息
错误信息如下:
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
...全文
98 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinzisheng 2009-09-01
  • 打赏
  • 举报
回复
props.put("mail.pop3.host","pop3.sohu.com");
这里设置错了。
KevinZhangYu 2009-09-01
  • 打赏
  • 举报
回复

package com.bfb2b.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 SendMail {

private String sender = "新浪邮箱";
private String smtpHost = "smtp.sina.com";

private String user = "新浪邮箱;
private String password = "wdmmssm";




private void sendBody(String mailTo, String subject, String information)
throws Exception, MessagingException {
Properties prop = new Properties();
prop.put("mail.smtp.host", smtpHost);// 设置smtp主机
prop.put("mail.smtp.auth", "true");// 使用smtp身份验证
prop.put("mail.smtp.user", user);
prop.put("mail.smtp.password", password);
Session session = Session.getInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(sender, password);
}
});
// 获得邮件会话对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
// 设置收件人地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
// 设置邮件主题
message.setSubject(subject);
// mail内容部分
MimeBodyPart part = new MimeBodyPart();
part.setText(information == null ? "" : information, "UTF-8");
// 设置邮件格式为html
part.setContent(information.toString(), "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(part);// 在 Multipart 中增加mail内容部分
message.setContent(multipart);// 增加 Multipart 到信息体
message.setSentDate(new Date());// 设置发送日期
Transport.send(message);// 发送邮件
}

public void send(String mailTo, String subject, String information) {
try {
this.sendBody(mailTo, subject, information);
} catch (Exception e) {
e.printStackTrace();
}
}

}




把上面代码 建成一个公共类、

需要是调用、send发放就OK了?参数值不要写错了、
KevinZhangYu 2009-09-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 water_zym 的回复:]
我找到原因了,是我的smtp没有打开,但是我又发现我机器尚没有smtp服务晕死
[/Quote]

我发给你吧、
yuwenbao 2009-08-31
  • 打赏
  • 举报
回复
pop3是接收邮件的,你要做的是发送邮件,需要设置下smtp 默认是你的本机,异常显示你的本机25端口没有打开,用telnet测试下看25端口打开没有,没有找找原因,可能是没开smtp相关的服务。如果是程序有问题,那就是你没设置好smtp
water_zym 2009-08-31
  • 打赏
  • 举报
回复
我找到原因了,是我的smtp没有打开,但是我又发现我机器尚没有smtp服务晕死

81,114

社区成员

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

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