这段代码出了什么问题

时间:2010-04-27 18:15:47

标签: java javamail

我收到编译错误。任何人都可以调试吗?

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);
}
}

3 个答案:

答案 0 :(得分:4)

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

请尝试这样称呼它:

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

另一个想法是制作postMail方法的重载版本,如果这是你打算经常做的事情。

答案 1 :(得分:0)

确保您已将mail.jaractivation.jar包含在类路径中。

答案 2 :(得分:0)

您必须在manifest file

中指定主要课程