package com.dl.mail;import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;public class SendMail {
private String fromMail;
private String smtp;
private String userName;
private String password;
//构造方法初始化参数
public SendMail(String fromMail,String smtp,String userName,String password){
this.fromMail=fromMail;
this.smtp=smtp;
this.userName=userName;
this.password=password;
}
public  void  sendMail(String toMail,String title,String message) throws UnsupportedEncodingException{
Properties props = System.getProperties();
        // 设置SMTP邮件服务器:
        props.put("mail.smtp.host",smtp);
        //props.put("mail.smtp.localhost", smtp);
        // SMTP服务器需要验证:
        props.put("mail.smtp.auth", "true");
        // 传入用户名和口令:
        
        Session session = Session. getDefaultInstance(props,
                new PasswordAuthenticator(userName,password));
                // 创建新邮件
                Message msg = null;
             msg = new MimeMessage(session);
    try {
msg.setFrom(new InternetAddress(fromMail));
  msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(new String(toMail.getBytes("gb18030"),"gb2312")));
        msg.setSubject(new String(title.getBytes("gb18030"),"gb2312"));
         msg.setText(new String(message.getBytes("gb18030"),"gb2312"));
          msg.setSentDate(new Date());
        // 发送:
        
        Transport.send(msg);
        System.out.println("邮件发送成功-->"+new Date().toLocaleString());
} catch (AddressException e) {
// TODO Auto-generated catch block
System.out.println(e);
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
System.out.println(e);
e.printStackTrace();
}
    
        
}
     /*public static void main(String[] arge) throws Exception {
      SendMail mail = new SendMail("[email protected]","smtp.tsa.cn","[email protected]","zclswj");
 String toMail = "[email protected]";
 String title = "标题A";
 String message = "内容A";
 mail.sendMail(toMail, title, message);

    } */
}错误代码:javax.mail.MessagingException: 501 domain address required: HELO还请高手指教!!

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主截止到2008-06-18 15:04:10的汇总数据:
    发帖数:1
    结贴数:0
    结贴率: 0.00%
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    在Windows下成功发送啦,证明域名地址应该是对的哦
      

  3.   

    在 SMTP HELO 命令中,SMTP 提供程序使用 InetAddress.getLocalHost().getHostName() 的结果。如果那个调用不能返回任何数据,就不会在 HELO 命令中发送任何名称。检查你的 JDK 和名称服务器配置,确保那个调用返回正确数据。你也可以设置 mail.smtp.localhost 属性,并可以把设置为想用于 HELO 命令的名称。
      

  4.   

    在/etc/hosts文件中加入     127.0.0.1     myhostname 
      

  5.   

    myhostname 是直接添上去么?还是要改为我的主机名?
      

  6.   

    可能需要这个smtp.tsa.cn的
    IP地址
      

  7.   

        public  void  sendMail(String toMail,String title,String message) throws UnsupportedEncodingException{
            Properties props = System.getProperties();
            // 设置SMTP邮件服务器:
            props.put("mail.smtp.host",smtp);
            //props.put("mail.smtp.localhost", smtp);
            // SMTP服务器需要验证:
            props.put("mail.smtp.auth", "true");
            // 传入用户名和口令:不要注释掉
      

  8.   

    应该是这样吧props.put("mail.smtp.localhost", "localhost"); 
      

  9.   

    问题解决啦
    props.put("mail.smtp.localhost", "localhost"); 
    是对的!
    但是我昨天试就没有成功啊!
    郁闷!