java程序 收发邮件 fail to connect

vae819723280 2010-09-09 03:47:45
大家帮帮忙~这是一个小程序,收发邮件的,运行没出错~但是结果是fail to connect,要怎么设置eclipse?还是程序问题?
Jmail:[code=Java]package com.qj.mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* 用于发送jmail邮件 关联java文件 SMTPAuthenticator.java
* @author 朱志杰
*
*/
public class Jmail {

/**
* 发送jmail
* @param title email标题
* @param content Email内容
* @param hostSmtp 发件邮箱 smtp地址 如:smtp.163.com
* @param hostAddress 发送邮箱地址 如:myzhijie@163.com
* @param hostPwd 发送邮箱密码
* @param toAddress 接收邮箱地址 如:myzhijie@qq.com
*/
public void sendMail(String title, String content,String hostSmtp,String hostAddress
,String hostPwd,String toAddress) {
// String hostSmtp = "smtp.163.com"; // 邮箱smtp
// String hostAddress = "myzhijie@163.com"; // 发件箱地址
// String hostPwd = ""; // 发件箱密码
// String toAddress = "myzhijie@qq.com";// 收件箱地址
try {
String mail = content;
// properties里面包含发送邮件服务器的地址
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", hostSmtp);
mailProps.put("mail.smtp.auth", "true");
SMTPAuthenticator smtpAuthenticator = new SMTPAuthenticator(hostAddress,
hostPwd);
Session mailSession = Session.getDefaultInstance(mailProps,
smtpAuthenticator);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(hostAddress));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
toAddress, false));
message.setSubject(title);
// System.out.println("准备发送邮件!!!");
message.setText(mail);
Transport.send(message);
} catch (Exception exc) {
exc.printStackTrace();
}
}

public static void main(String[] args) {
Jmail aa = new Jmail();
aa.sendMail("朱志杰标题","朱志杰内容","smtp.163.com","myzhijie@163.com","密码","myzhijie@qq.com");
System.out.println("Well Done!");
}
}
[/code]
SMTPAuthenticator:
package com.qj.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
* 用于Jmail返回邮箱账号和密码的校验(在这里被Jmail类所用)
*
* @author 朱志杰
*
*/
public class SMTPAuthenticator extends Authenticator {
private String name = "";
private String password = "";

public SMTPAuthenticator(String name, String password) {
this.name = name;
this.password = password;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(name, password);
}

}
...全文
170 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zn85600301 2010-09-16
  • 打赏
  • 举报
回复
你的SMTP 端口 开了没 或者你电脑的SMTP服务禁用了?
chenchong32139 2010-09-15
  • 打赏
  • 举报
回复
我是来看看程序的~~
老猫的TOM 2010-09-13
  • 打赏
  • 举报
回复
	public void sendMail() throws Exception {

Properties props = new Properties();// 创建属性对象
props.put("mail.smtp.host", getHost());// 设置smtp服务器地址
props.put("mail.smtp.auth", "true");// 设置服务器smtp需要验证
Session session = Session.getInstance(props, null);// 创建新邮件并群发
// Session session = Session.getDefaultInstance(props);
// session.setDebug(true);
MimeMessage message = new MimeMessage(session);// 创建过程对象
message.setFrom(new InternetAddress(getFromAddr()));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(
getToAddr()));
message.setSubject(getTitle());// 设置主题
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");// 设置信件内容
multipart.addBodyPart(contentPart);
if (getAttachPath() != null && getAttachName() != null) {
BodyPart attachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(getAttachPath());
attachmentPart.setDataHandler(new DataHandler(source));
BASE64Encoder enc = new BASE64Encoder();
attachmentPart.setFileName("=?GBK?B?"
+ enc.encode(getAttachName().getBytes()) + "?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, getUsername(), getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
zhenge1020 2010-09-13
  • 打赏
  • 举报
回复
会不会是代理没有设置?

58,454

社区成员

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

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