Search in sources :

Example 21 with MultiPartEmail

use of org.apache.commons.mail.MultiPartEmail in project BleLiteLib4android by afunx.

the class MailUtils method sendEmailByApacheCommonsEmail.

/**
 * 只是用来参考的DEMO
 *
 * @throws EmailException
 * @Description TODO 发送带附件的email
 */
private static void sendEmailByApacheCommonsEmail(String from, String fromPwd, String to, String cc, String bcc, String subject, String message) throws EmailException {
    if (!verifyEmailAddress(from) || !verifyEmailAddress(to)) {
        System.out.println("enter verifyEmailAddress");
        return;
    }
    // Create the email message
    MultiPartEmail email = new MultiPartEmail();
    email.setDebug(true);
    // 这里使用163邮箱服务器,实际需要修改为对应邮箱服务器
    // smtp.163.com:25   smtp.qq.com:587
    email.setHostName("smtp.qq.com");
    // 163邮箱25
    email.setSmtpPort(456);
    email.setSocketTimeout(6 * 1000);
    email.setCharset("UTF-8");
    // email.setTLS(true);
    email.setStartTLSEnabled(true);
    // email.setSSL(true);
    email.setAuthentication(from, fromPwd);
    email.addTo(to, to);
    // email.addBcc(bcc);
    // email.addCc(cc);
    email.setFrom(from, from);
    email.setSubject(subject);
    email.setMsg(message);
    // // Create the attachment
    // EmailAttachment attachment2 = new EmailAttachment();
    // attachment2.setPath(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "pdf02.png");
    // attachment2.setDisposition(EmailAttachment.ATTACHMENT);
    // attachment2.setDescription("pdf02");
    // attachment2.setName("pdf02.png");
    // 
    // EmailAttachment attachment1 = new EmailAttachment();
    // attachment1.setPath(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "pdf01.png");
    // attachment1.setDisposition(EmailAttachment.ATTACHMENT);
    // attachment1.setDescription("pdf01");
    // attachment1.setName("pdf01.png");
    // 
    // email.attach(attachment1);
    // email.attach(attachment2);
    // send the email
    String sendStr = email.send();
    System.out.println("sendStr=" + sendStr);
}
Also used : MultiPartEmail(org.apache.commons.mail.MultiPartEmail)

Example 22 with MultiPartEmail

use of org.apache.commons.mail.MultiPartEmail in project BleLiteLib4android by afunx.

the class MailUtils method sendEmail.

public static boolean sendEmail(String from, String fromPwd, String to, String subject, String message) {
    if (!verifyEmailAddress(from) || !verifyEmailAddress(to)) {
        System.out.println("enter verifyEmailAddress");
        return false;
    }
    try {
        // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        email.setDebug(true);
        // 这里使用163邮箱服务器,实际需要修改为对应邮箱服务器
        // smtp.163.com:25   smtp.qq.com:587
        email.setHostName("smtp.163.com");
        // 163邮箱25
        email.setSmtpPort(25);
        email.setSocketTimeout(6 * 1000);
        email.setCharset("UTF-8");
        email.setStartTLSEnabled(true);
        email.setAuthentication(from, fromPwd);
        email.addTo(to, to);
        email.setFrom(from, from);
        email.setSubject(subject);
        email.setMsg(message);
        String sendStr = email.send();
        System.out.println("sendStr=" + sendStr);
    } catch (EmailException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : MultiPartEmail(org.apache.commons.mail.MultiPartEmail) EmailException(org.apache.commons.mail.EmailException)

Aggregations

MultiPartEmail (org.apache.commons.mail.MultiPartEmail)22 EmailException (org.apache.commons.mail.EmailException)11 EmailAttachment (org.apache.commons.mail.EmailAttachment)9 HtmlEmail (org.apache.commons.mail.HtmlEmail)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 InternetAddress (javax.mail.internet.InternetAddress)4 Email (org.apache.commons.mail.Email)4 RenderResult (cn.bran.japid.template.RenderResult)3 IOException (java.io.IOException)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)3 Mail (ninja.postoffice.Mail)3 Test (org.junit.Test)3 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MailException (cn.bran.play.exceptions.MailException)1