簡體   English   中英

在 java 中發送郵件時出現問題

[英]Problem while sending mail in java

我的代碼在我機器上的 windows 服務器上運行,但是當我將相同的 class 文件上傳到 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);
    }

} 

嘗試

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

這可能會捕獲所有郵件錯誤,然后您可以分析..

您是否嘗試以超級用戶身份運行您的代碼? 也許它只是一些非特權用戶問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM