这段代码有什么问题 [英] What's wrong with this code

查看:57
本文介绍了这段代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了编译器错误.有人可以调试吗?

I am getting the compiler error. Can anybody debug this?

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是字符串数组,但是在您的主要方法中,您正在传递字符串文字.编译器告诉您无法找到与参数列表(如(String, String, String, String))匹配的postMail方法的版本.

Your postMail function is expecting the first parameter, recipients to be an array of Strings, but in your main method you are passing a String literal. The compiler is telling you that it's unable to find a version of the postMail method that matches a parameter-list like (String, String, String, String).

尝试像这样调用它:

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

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

Another idea would be to make an overloaded version of your postMail method if this is something you intend to do often.

这篇关于这段代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆