javamail 发送邮件错误! 邮件服务器地址不对吧,如果是SMTP,要验证用户的, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的程序我没大看懂,我做过邮件的发送,第一步是连接SMTP server,需要Username、Password(可能不用,最好是有)最后是mailserver(ip或SMTP server address e.g. smtp.sohu.com);第二步是发邮件(javamail的帮助里有例子代码),邮件的形式也有很多种text、html等。以上内容,仅供参考!!!! 是不是你的session配置错了,61...那个ip地址是哪儿的,反正不是163的,我ping了一下,163是202...,你用的是什么服务器? 我用的是mailserver,,我在WEBLOGIC的配置是jndi name:MailSessionmail.host=myemail.com 给你发个javamail实例,希望能有所帮助:-)/* TestMail.java* Created on 2004-11-12*/package test;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;/** * Add one sentence class summary here. * Add class description here. * * @author lxx * @version 1.0, 2004-11-12 */public class TestMail { 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 TestMail() { // setSmtpHost(getConfig.mailHost);//如果没有指定邮件服务器,就从getConfig类中获取 createMimeMessage(); } public TestMail(String smtp){ setSmtpHost(smtp); createMimeMessage(); } /** * @param hostName String */ public void setSmtpHost(String hostName) { System.out.println("设置系统属性:mail.smtp.host = "+hostName); if(props == null)props = System.getProperties(); //获得系统属性对象 props.put("mail.smtp.host",hostName); //设置SMTP主机 } /** * @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; } } /** * @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"); } } /** * @param name String * @param pass String */ public void setNamePass(String name,String pass) { username = name; password = pass; } /** * @param mailSubject String * @return boolean */ public boolean setSubject(String mailSubject) { System.out.println("设置邮件主题!"); try{ mimeMsg.setSubject(mailSubject); return true; } catch(Exception e) { System.err.println("设置邮件主题发生错误!"); return false; } } /** * @param mailBody String */ public boolean setBody(String mailBody) { try{ BodyPart bp = new MimeBodyPart(); bp.setContent("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"+mailBody,"text/html;charset=gb2312"); mp.addBodyPart(bp); return true; } catch(Exception e){ System.err.println("设置邮件正文时发生错误!"+e); return false; } } /** * @param name String * @param pass String */ public boolean addFileAffix(String filename) { System.out.println("增加邮件附件:"+filename); try{ BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); return true; } catch(Exception e){ System.err.println("增加邮件附件:"+filename+"发生错误!"+e); return false; } } /** * @param name String * @param pass String */ public boolean setFrom(String from) { System.out.println("设置发信人!"); try{ mimeMsg.setFrom(new InternetAddress(from)); //设置发信人 return true; } catch(Exception e) { return false; } } /** * @param name String * @param pass String */ 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; } } /** * @param name String * @param pass String */ 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; } } /** * @param name String * @param pass String */ 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; } } /** * Just do it as this */ public static void main(String[] args) { String mailbody = "<meta http-equiv=Content-Type content=text/html;charset=GB2312>"+ "<div align=center><a> 你所需发送邮件的正文 </a></div>"; //本人以163信箱为例子 TestMail themail = new TestMail("smtp.163.com"); themail.setNeedAuth(true); if(themail.setSubject("标题") == false) return; if(themail.setBody(mailbody) == false) return; if(themail.setTo("待发送邮箱地址") == false) return; if(themail.setFrom("发送邮箱的地址") == false) return; // if(themail.addFileAffix("/hello.txt") == false) return; themail.setNamePass("user","password"); if(themail.sendout() == false) return; } } myeclipse无法启动Struts2项目 关于用java做wap网站,怎么做 你真的了解java.util.Calendar吗?来解释下。 MD5加密 有兴趣的进来 log4j.xml加载问题 ireport连接sqlserver2005数据库 EJB实体CMR 疑问? 由于项目需要一个轻量级的java版工作流引擎,请大家推荐一款,谢谢! 关于weblogic的LDAP不能创建group的问题 STRUTS1.1中用,使用<html:errors>,出错后的JSP页面输入的中文为乱码? JTable中如何实现拉下方滚动条时始终显示前若干列啊??? 怎样利用struts修改一条记录!!!!
第一步是连接SMTP server,需要Username、Password(可能不用,最好是有)
最后是mailserver(ip或SMTP server address e.g. smtp.sohu.com);
第二步是发邮件(javamail的帮助里有例子代码),邮件的形式也有很多种text、html等。
以上内容,仅供参考!!!!
,你用的是什么服务器?
jndi name:MailSession
mail.host=myemail.com
/* TestMail.java
* Created on 2004-11-12
*/
package test;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;
/**
* Add one sentence class summary here.
* Add class description here.
*
* @author lxx
* @version 1.0, 2004-11-12
*/
public class TestMail { 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 TestMail() {
// setSmtpHost(getConfig.mailHost);//如果没有指定邮件服务器,就从getConfig类中获取
createMimeMessage();
} public TestMail(String smtp){
setSmtpHost(smtp);
createMimeMessage();
} /**
* @param hostName String
*/
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = "+hostName);
if(props == null)props = System.getProperties(); //获得系统属性对象 props.put("mail.smtp.host",hostName); //设置SMTP主机
}
/**
* @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;
}
} /**
* @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");
}
} /**
* @param name String
* @param pass String
*/
public void setNamePass(String name,String pass) {
username = name;
password = pass;
}
/**
* @param mailSubject String
* @return boolean
*/
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try{
mimeMsg.setSubject(mailSubject);
return true;
}
catch(Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}
/**
* @param mailBody String
*/
public boolean setBody(String mailBody) {
try{
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"+mailBody,"text/html;charset=gb2312");
mp.addBodyPart(bp);
return true;
}
catch(Exception e){
System.err.println("设置邮件正文时发生错误!"+e);
return false;
}
}
/**
* @param name String
* @param pass String
*/
public boolean addFileAffix(String filename) { System.out.println("增加邮件附件:"+filename); try{
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName()); mp.addBodyPart(bp); return true;
}
catch(Exception e){
System.err.println("增加邮件附件:"+filename+"发生错误!"+e);
return false;
}
} /**
* @param name String
* @param pass String
*/
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try{
mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
return true;
}
catch(Exception e)
{ return false; }
}
/**
* @param name String
* @param pass String
*/
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; } } /**
* @param name String
* @param pass String
*/
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; }
}
/**
* @param name String
* @param pass String
*/
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;
}
}
/**
* Just do it as this
*/
public static void main(String[] args) { String mailbody = "<meta http-equiv=Content-Type content=text/html;charset=GB2312>"+
"<div align=center><a> 你所需发送邮件的正文 </a></div>";
//本人以163信箱为例子 TestMail themail = new TestMail("smtp.163.com");
themail.setNeedAuth(true); if(themail.setSubject("标题") == false) return;
if(themail.setBody(mailbody) == false) return;
if(themail.setTo("待发送邮箱地址") == false) return;
if(themail.setFrom("发送邮箱的地址") == false) return;
// if(themail.addFileAffix("/hello.txt") == false) return;
themail.setNamePass("user","password"); if(themail.sendout() == false) return;
}
}