在java中发送邮件时出现问题

时间:2009-03-24 08:37:02

标签: java email

我的代码在我的机器上的Windows服务器上运行,但是当我将相同的类文件上传到Linux服务器机器时,它不起作用。

由于没有错误产生,我无法得到确切的问题..

如果你能帮助我,请参阅下面的代码。

public static String myemail = "xyz@abc.com", //username
mypassword = "*************", //password
myhost = "smtp.abc.com", // smtp address
myport = "234", // port address
mysubject = "Hello",
mytext = "this is just a test mail";


public String mail(String recp)
{

    Properties pro = new Properties();
    pro.put("mail.smtp.user", myemail);
    pro.put("mail.smtp.host", myhost);
    pro.put("mail.smtp.port", myport);
    pro.put("mail.smtp.starttls.enable","true");
    pro.put("mail.smtp.auth", "true");
    pro.put("mail.smtp.debug", "true");
    pro.put("mail.smtp.socketFactory.port",myport);


    SecurityManager security = System.getSecurityManager();

    try
    {
        Authenticator authen = new SMTPConfig();
        Session session = Session.getInstance(pro, authen);
        session.setDebug(true);

        MimeMessage msg = new MimeMessage(session);
        msg.setText(mytext);
        msg.setSubject(mysubject);
        msg.setFrom(new InternetAddress(myemail));
        msg.addRecipient(Message.RecipientType.TO, new
        InternetAddress(recp));
        Transport.send(msg);
    }

    catch (Exception ex)
    {
        ex.printStackTrace();
    }

    return null;

}

private class SMTPConfig extends javax.mail.Authenticator
{

    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(myemail, mypassword);
    }

} 

2 个答案:

答案 0 :(得分:1)

尝试

pro.put(“mail.debug”,“true”);而不是pro.put(“mail.smtp.debug”,“true”);

这可能会捕获所有邮件错误,然后您可以分析..

答案 1 :(得分:0)

您是否尝试以超级用户身份运行代码?也许它只是一些无特权的用户问题。

相关问题