我正在尝试在Grails应用程序中发送电子邮件。 grails版本是2.1.0,Java版本是1.6.41 尝试构建项目时出现以下错误:
unable to resolve class Session
Session session = Session.getDefaultInstance(properties);
unable to resolve class MimeMessage
MimeMessage message = new MimeMessage(session);
unable to resolve class InternetAddress
message.setFrom(new InternetAddress(from));
我在互联网上尝试了一些建议。他们似乎没有工作。无法确定这是否是Grails特定的错误。
我尝试通过以下方式引用 mail-1.4.7.jar , smtp-1.4.5.jar , activation-1.1.jar 构建路径。
但是没有运气。错误仍然存在。
请指导。让我知道是否可以提供其他信息。
下面是代码段:
package com.xyz.xyx.ReportsApp
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class ScheduleXYZController {
def sendEmail() {
// Recipient's email ID needs to be mentioned.
String from = "xyz@domain.com";
// Sender's email ID needs to be mentioned
String to = "abc@domain.com";
// Assuming you are sending email from localhost
String host = "smtplb.domain.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("another Test Mail.....");
// Now set the actual message
message.setText("Another Body of the mail.... <br/><br/>"+ params.body);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}