我找了一个用Javamail写的发邮件的程序。
昨天晚上运行时还是好好的,可以查看邮件。
今天早上怎么就不行了呢?等了好久邮箱里都没有收到邮件。
这是怎么回事呢?
我是从sina发给163的是不是这些邮箱不通过这些简单的邮件啊 ?
是我的邮件太简单了,还是有一些设置的问题?请指教这是我的源码:import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;import com.test.sendToUser;
import com.test.sendToUser.*;
public class sendMail1 {

public static void main(String[] ags)throws Exception {
String host = "smtp.sina.com"; // 发件人使用发邮件的电子信箱服务器
String from = "[email protected]"; //"图档管理系统"; 发邮件的出发地(发件人的信箱)
String to = "[email protected]"; // 发邮件的目的地(收件人信箱) // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"photomanage", "********");
}
}); session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session); // Set the from address
//message.setFrom(new InternetAddress(MimeUtility.encodeText(from)));
message.setFrom(new InternetAddress(from)); // Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set the subject
message.setSubject("找回密码"); // Set the content
// message.setText("欢迎你!");
message.saveChanges(); Transport.send(message); }
}
}

解决方案 »

  1.   

    哦 对不起  倒数第三行代码 //message.setText("欢迎你 !")没有注释
      

  2.   

    这是一个从163邮箱往QQ邮箱发送邮件的实例,LZ参考一下:
    package JavaMail;import java.util.Date;
    import java.util.Properties;import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;public class SendTextMail 
    {
    String smtphost="SMTP.163.com";//smtp服务器
    String user="[email protected]";//登录smtp服务器的账号
    String password="**********";//登录服务器的密码
    String from="[email protected]";//发送人邮箱
    String to="[email protected]";//接受人邮箱
    String title="这是测试邮件不要回复!!";//邮件标题
    String content="这是测试邮件不要回复!!";//邮件内容

    public boolean send()
    {
    try {
    Properties props=new Properties();//创建一个对象属性
    props.put("mail.smtp.host",smtphost);//指定smtp服务器
    props.put("mail.smtp.auth","true");//指定是否需要

    SmtpAuth auth=new SmtpAuth();//创建一个授权验证对象
    auth.setAccount(user, password);

    Session mailsession=Session.getDefaultInstance(props,auth);//创建一个session对象
    Message message=new MimeMessage(mailsession);//创建一个session对象

    message.setFrom(new InternetAddress(from));//指定发件人
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//指定收件人邮箱

    message.setSubject(title);//文件的标题
    message.setText(content);//发送文本内容的邮件

    message.setSentDate(new Date());//指定邮件的发送日期
    message.setHeader("X-Priority","1");//指定邮件的优先级1、紧急 3、普通 5、缓慢
    message.saveChanges(); Transport transport=mailsession.getTransport("smtp");//创建一个Transport对象
    transport.connect(smtphost,user,password);//连接SMTP服务器
    transport.sendMessage(message,message.getAllRecipients());//发送邮件
    transport.close();

    return true;


    catch (AddressException e) 
    {
    e.printStackTrace();
    return false;

    catch (MessagingException e) 
    {
    e.printStackTrace();
    return false;
    }
    }

    static class SmtpAuth extends Authenticator
    {
    String user,password;
    void setAccount(String user,String password)
    {
    this.user=user;
    this.password=password;
    }
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication(user,password);
    }
    }

    public static void main(String[] args) 
    {
    new SendTextMail().send(); }}