This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                已关闭8年。
            
        

我收到编译器错误。有人可以调试吗?

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
  public static void main(String [] args)
  {
    SendMail sm=new SendMail();
     sm.postMail("abc@yahoo.com","hi","hello","xyz@gmail.com");
   }

public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "webmail.emailmyname.com");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
}

最佳答案

您的postMail函数期望第一个参数recipients是字符串数组,但是在您的主要方法中,您正在传递字符串文字。编译器告诉您无法找到与参数列表(如postMail)匹配的(String, String, String, String)方法的版本。

尝试像这样调用它:

sm.postMail(new String[]{"abc@yahoo.com"},"hi","hello","xyz@gmail.com");


另一个想法是,如果您打算经常这样做,则可以重载postMail方法的版本。

关于java - 此代码有什么问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2723809/

10-11 22:48
查看更多