简体   繁体   English

如何从glassfish服务器发送邮件?

[英]how to send mail from glassfish server?

How to write a mail application with java mail API that run all time at glassfish server and search a specific time in the database and send mail at that time. 如何编写使用Java邮件API的邮件应用程序,该应用程序始终在glassfish服务器上运行,并在数据库中搜索特定时间并在该时间发送邮件。 i have web application written in JSF that need to send mail at specific time given in database. 我有用JSF编写的Web应用程序,需要在数据库给出的特定时间发送邮件。

For gmail, use below code 对于gmail,请使用以下代码

import org.apache.commons.mail.*;

public class GmailEmailWorking {

    public static void main(String[] args) {
        String myEmailId = "xyz@gmail.com";
        String myPassword = "password";
        String senderId = "xyz@yahoo.com";
        try {
            MultiPartEmail email = new MultiPartEmail();
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator(myEmailId, myPassword));
            email.setDebug(true);
            email.setHostName("smtp.gmail.com");
            email.setFrom(myEmailId);
            email.setSubject("Hi");
            email.setMsg("This is a test mail ... :-)\n\nPlease check attachements that I have sent.\n\nThanks,\nFahim");
            email.addTo(senderId);
            email.setTLS(true);

            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath("/Users/fahadparkar/Desktop/Fahim/tables.xlsx");
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("Excel");
            attachment.setName("tables.xlsx");
            email.attach(attachment);

            email.send();
            System.out.println("Mail sent!");
        } catch (Exception e) {
            System.out.println("Exception :: " + e);
        }
    }
}

Below are list of jar files you will need 以下是您需要的jar文件列表

To send from other server, you will need to do changes at below line 要从其他服务器发送,您需要在下面的行中进行更改

email.setSmtpPort(587);
email.setHostName("smtp.gmail.com");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM