grails - 将Java sendEmail方法转换为Groovy

标签 grails groovy

我有一个Java方法可以通过电子邮件发送文件附件。我正在尝试将其作为Grails应用程序的一部分移植到Groovy。我对FileDataSource声明的格式有疑问。令人反感的代码标有星号。在Groovy中做那部分的正确方法是什么?我似乎在网上找不到任何东西。

public static void sendEmail(String sendFile)
    {
    //
    // Send the log file via email.
    //
    final String username = "myname@mydomain.com"
    final String password = "mypassword"

    // Strings that contain from, to, subject, body and file path to the attachment
    String from = username;
    String subject = "Test Results"
    String body = "Body of Test Results email."
    String filename = sendFile

    // Set smtp properties
    Properties properties = new Properties()
    properties.put("mail.smtp.starttls.enable", "true")
    properties.put("mail.smtp.auth", "true")
    properties.put("mail.smtp.host", "smtp.gmail.com")
    properties.put("mail.smtp.port", "587")

    Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator()
    {
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(username,password)
        }
    });

    try
    {
        MimeMessage message = new MimeMessage(session)
        message.setFrom(new InternetAddress(from))
        // For debug uncomment the line below and enter your email address.
        //message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("peter.cook@studentuniverse.com"));
        // For debug comment the line below out.
        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("cccm@studentuniverse.com,WebsiteOperations@studentuniverse.com,peter.cook@studentuniverse.com"))
        message.setSubject(subject)
        message.setSentDate(new Date())

        // Set the email body
        MimeBodyPart messagePart = new MimeBodyPart()
        messagePart.setText(body)

        // Set the email attachment file
        MimeBodyPart attachmentPart = new MimeBodyPart()
        *FileDataSource fileDataSource = new FileDataSource(filename)
        *{
        *    @Override
        *    public String getContentType()
        *    {
        *        return "application/octet-stream"
        *    }
        * }
        attachmentPart.setDataHandler(new DataHandler(fileDataSource))
        attachmentPart.setFileName(fileDataSource.getName())

        // Add all parts of the email to Multipart object
        Multipart multipart = new MimeMultipart()
        multipart.addBodyPart(messagePart)
        multipart.addBodyPart(attachmentPart)
        message.setContent(multipart)

        // Send email
        Transport.send(message)
        System.out.println("Mail sent!")
    }
    catch (MessagingException e)
    {
        e.printStackTrace()
    }
}

最佳答案

这种糟糕的支撑风格与Groovy不需要分号冲突。尽管不需要多次使用,但不需要非匿名类定义,只需这样声明即可:

FileDataSource fileDataSource = new FileDataSource(filename) {
    String getContentType() { 'application/octet-stream' }
}

关于grails - 将Java sendEmail方法转换为Groovy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47273056/

相关文章:

java - Grails/GORM 2.3 Hibernate 寻找抽象域类持久化表

groovy - Spock 单元测试和内部封闭

java - 当 Java 类未在我的代码中实例化时,是否可以使用 Groovy 覆盖 Java 类中的方法?

database - 在 grails 中为一个应用程序使用两个数据库

grails - 在Grails应用程序中使用Apache POI生成PPT

Jenkins HttpRequest 插件 MissingMethodException

Groovy:调用 Java API 时缺少方法异常

amazon-web-services - 为什么 grails websockets 连接失败,但在使用 AWS ELB 时继续接收浏览器中已订阅的消息?

Grails 命名空间问题

grails - 无论如何,有没有更改默认的Grails服务命名约定?