如何在没有硬编码的情况下在我的服务类中初始化电子邮件(例如:Email to = new Email("[email protected]")
。我使用以下代码初始化我的电子邮件但它返回一些错误。帮我解决这个问题。我正在使用SendGrid API。这是我的服务代码:
Email to = new Email();
to.setEmail(emailIDTO.getTO()); //emailIDTO is an object of IDTO class
// IDTO class takes the value from the JSON request body and initializes it to the email object
以及相关的IDTO代码段:
public Email getTo(){
return to;
} // method getTo return the mail id of the recipient.
public Email createTo(EmailIDTO emailIDTO){
to.setName(emailIDTO.getName();
to.setEmail(emailIDTO.getEmail());
return null;
}
错误:
setEmail (java.lang.String) in Email cannot be applied to (com.sendgrid.Email)
您的代码失败,因为您的emailIDTO.getTO()
方法正在返回Email对象,您尝试使用接受String参数的方法分配该对象。在这种情况下,错误是非常明显的。
您可以尝试以下方法之一:
选项1 - 直接从IDTO设置对象:
Email to = emailIDTO.getTO();
选项2 - 提取字符串值:
Email to = new Email();
to.setEmail(emailIDTO.getTO().getEmail());
注意 - 您的IDTO.createTo()
方法返回null,这可能会产生意外后果。也许你想回归to
?