标签: apache-commons-email

如何在Apache Commons Email中更改charset?

我正在使用Apache Commons电子邮件向我的客户发送电子邮件,但我有一个名为'SemanadaComputação'的客户(在portugues BR中),但它是'SemanadaComputação'.我尝试修改我的代码,但它没有任何工作:

public static boolean emailWithImage(String subject, String message, String emailReceiver, String imageURL) {
    HtmlEmail email = new HtmlEmail();
    email.setCharset("UTF-8"); // I change here, but it is not working
    email.setHostName(Constantes.EMAIL_HOST_NAME);
    email.setSmtpPort(587);
    DefaultAuthenticator authenticator =
            new DefaultAuthenticator(Constantes.EMAIL_USER, Constantes.EMAIL_PASSWORD);
    email.setAuthenticator(authenticator);
    email.setTLS(true);

    try {
        email.setFrom(Constantes.EMAIL_USER, Constantes.EMAIL_NAME);
        email.setSubject(subject);
        email.addTo(emailReceiver);

        URL url = new URL(imageURL);
        String cid = email.embed(url, "image");        /* it must be 'cid' the name of the image */

        email.setHtmlMsg("<html><img src=\"cid:" + cid + "\"> <p>" + message + "</p> </html>"); /* …
Run Code Online (Sandbox Code Playgroud)

java email apache-commons-email

14
推荐指数
2
解决办法
9439
查看次数

使用Apache Commons电子邮件库以Java形式发送电子邮件

我正在使用Apache Commons电子邮件库发送电子邮件,但我无法通过GMail SMTP服务器发送它们.
任何人都可以提供与GMail SMTP服务器和其他服务器兼容的示例代码吗?

我使用以下代码不起作用:

String[] recipients = {"receiver@gmail.com"};

SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setAuthentication("sender@gmail.com", "mypasswd");
email.setDebug(true);
email.setSmtpPort(465);

for (int i = 0; i < recipients.length; i++)
{
    email.addTo(recipients[i]);
}

email.setFrom("sender@gmail.com", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
Run Code Online (Sandbox Code Playgroud)

java email gmail smtp apache-commons-email

11
推荐指数
2
解决办法
3万
查看次数

与 Java 17 上的 Jakarta 兼容的 org.apache.commons.mail.util.MimeMessageParser 的替代方案

当我将 Spring Boot 应用程序更新到 Java 17 和 Spring Boot 3 时,我从 Java EE 迁移到 Jakarta。我使用了 MimeMessageParser org.apache.commons.mail.util.MimeMessageParser,但它需要javax.mail.internet.MimeMessagea 而不是jakarta.mail.internet.MimeMessage

org.apache.commons我认为artifact可能有更新commons-email,但我已经使用当前版本(1.5)。

有人找到了如何替换 MimeMessageParser 的解决方案吗?

apache-commons-email spring-boot jakarta-migration

11
推荐指数
1
解决办法
1453
查看次数

Apache Commons Email和UTF-8

如何将使用Apache Commons Email生成的电子邮件的编码更改为UTF-8?我想根据收件人的语言发送我生成的电子邮件,我需要考虑日语和俄语.问题是:Email类没有提出我可以传递给该Email.setCharset方法的UTF-8常量.任何线索?

java email utf-8 apache-commons-email

9
推荐指数
1
解决办法
4462
查看次数

如何使用Apache Commons Email将文件附加到HTML电子邮件

我正在使用Apache Commons Email 1.1,我无法弄清楚如何将文件附加到HtmlEmail.如果我运行下面的代码,我会收到一封带附件的电子邮件,但HTML邮件也会作为附件出现.

如果我不调用email.attach(),HTML消息就会如您所愿,但我需要HTML消息和附件.我错过了什么?

  HtmlEmail email = new HtmlEmail();
  email.setHostName("localhost");
  email.addTo("test@mail.com", "Test");
  email.setFrom("testapp@mail.com", "Test App");
  email.setSubject("Test message");
  email.setHtmlMsg("<div style='font-size: 20px; color: green;'>This is html email</div>");

  EmailAttachment attachment = new EmailAttachment();
  attachment.setPath(pdfPath);
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  email.attach(attachment);

  email.send();
Run Code Online (Sandbox Code Playgroud)

java email apache-commons-email

8
推荐指数
1
解决办法
1万
查看次数

如何在一个会话中发送多个电子邮件?

我想向不同的收件人发送数千封不同的电子邮件,并希望打开与我的SMTP的连接并保留它.我希望这更快,然后重新打开ervy邮件的连接.我想使用Apache Commons Email,但如果有必要,可以回退到Java Mail API.

现在我正在这样做,每次打开关闭连接:

HtmlEmail email = new HtmlEmail();
email.setHostName(server.getHostName());
email.setSmtpPort(server.getPort());
email.setAuthenticator(new DefaultAuthenticator(server.getUsername(), server.getPassword()));
email.setTLS(true);
email.setFrom("test@example.com");
email.addTo(to);
email.setSubject(subject);
email.setHtmlMsg(htmlMsg);
email.send();
Run Code Online (Sandbox Code Playgroud)

java email apache-commons-email

8
推荐指数
1
解决办法
2万
查看次数

使用Commons-Email向Gmail发送电子邮件

Email email = new SimpleEmail();
String authuser = "......@gmail.com";
String authpwd = "*******";
// Very Important, Don't use email.setAuthentication()
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true); // true if you want to debug
email.setHostName("smtp.gmail.com");

email.getMailSession().getProperties().put("mail.smtp.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtp.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo(".............@gmail.com", "ToName");
email.send();
Run Code Online (Sandbox Code Playgroud)

它给出了以下例外

SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
Run Code Online (Sandbox Code Playgroud)

java email apache-commons-email

7
推荐指数
3
解决办法
2万
查看次数

Apache Commons电子邮件和重用SMTP连接

目前我使用Commons Email发送电子邮件,但我找不到在发送的电子邮件之间共享smtp连接的方法.我有以下代码:

    Email email = new SimpleEmail();
    email.setFrom("example@example.com");
    email.addTo("example@example.com");
    email.setSubject("Hello Example");
    email.setMsg("Hello Example");
    email.setSmtpPort(25);
    email.setHostName("localhost");
    email.send();
Run Code Online (Sandbox Code Playgroud)

这是非常易读的,但是当我执行大量消息时速度很慢,我认为这是重新连接每条消息的开销.因此,我使用以下代码对其进行了分析,并发现使用重新使用Transport会使事情快三倍.

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    Session mailSession = Session.getDefaultInstance(props, null);
    Transport transport = mailSession.getTransport("smtp");
    transport.connect("localhost", 25, null, null);

    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress("example@example.com"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("example@example.com"));
    message.setSubject("Hello Example");
    message.setContent("Hello Example", "text/html; charset=ISO-8859-1");

    transport.sendMessage(message, message.getAllRecipients());
Run Code Online (Sandbox Code Playgroud)

所以我想知道是否有办法让Commons Email重用多个电子邮件发送的SMTP连接?我更喜欢Commons Email API,但性能有点痛苦.

谢谢,赎金

java email apache-commons-email

6
推荐指数
1
解决办法
3767
查看次数

在commons email中添加附件作为流

我在我的网络应用程序中使用Apache Commons Email,它工作正常.

现在我需要通过附件发送文档,我遇到了一些问题.我需要从数据库中获取文件(作为BLOB)并将其添加为附件.看起来Commons Email不支持流附件,它只从路径中获取文件.

我需要知道这里的最佳做法是什么?

  1. 我是否还需要将文件保存在目录结构中,以便它可以与Commons Email?一起使用,或者,
  2. 有什么方法可以使用流式内容本身作为附件添加?

java jakarta-mail apache-commons-email

6
推荐指数
1
解决办法
1万
查看次数

Apache Commons Email with attach with base64

我正在尝试发送一个base64编码的文件apache.commons.mail,我只是不能接缝以获得Content-Transfer-Encoding: base64它应该去的标题.

// Create the email
MultiPartEmail email = new MultiPartEmail();
email.setSmtpPort(587);
email.setDebug(false);
email.setHostName("smtp.gmail.com");
email.setAuthentication("from@gmail.com", "password");
email.setTLS(true);

email.addTo("to@example.com");
email.setFrom("from@example.com");
email.setSubject("subject");

email.attach(new ByteArrayDataSource(
     Base64.encodeBase64(attachFull.getBytes()), "text/plain"), 
     "samplefile.txt", 
     "sample file desc", 
     EmailAttachment.ATTACHMENT
);
Run Code Online (Sandbox Code Playgroud)

而这正是收件人所获得的.

------=_Part_0_614021571.1334210788719
Content-Type: text/plain; charset=Cp1252; name=texto.txt
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=samplefile.txt
Content-Description: sample file desc
Run Code Online (Sandbox Code Playgroud)

我如何指定该文件是Base64编码?

java base64 apache-commons-email

4
推荐指数
1
解决办法
2918
查看次数

使用commons-email-1.3发送电子邮件时出错

在发送电子邮件时,我使用commons-email-1.3收到以下错误.
我已经下载并添加了外部jar到项目中.
请帮我解决这个问题!

package mypkg;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;

public class sendingmail {
     public static void main(String[] args)  throws Exception {
            Email email = new SimpleEmail();
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator("myid","mypwd")); //Here is the error
            email.setDebug(false);
            email.setHostName("smtp.gmail.com");
            email.setFrom("me@gmail.com");
            email.setSubject("Hi");
            email.setMsg("This is a test mail ... :-)");
            email.addTo("you@gmail.com");
            email.setTLS(true);
            email.send();
            System.out.println("Mail sent!");

    }
}
Run Code Online (Sandbox Code Playgroud)

给出错误的行是

email.setAuthenticator(new DefaultAuthenticator("myid","mypwd"));
Run Code Online (Sandbox Code Playgroud)

错误消息是

线程"main"中的异常java.lang.Error:未解决的编译问题:

无法解析javax.mail.Authenticator类型.它是从所需的.class文件间接引用的.来自
类型Email的方法setAuthenticator(Authenticator)是指mypkg.mailtest.main中缺少的类型Authenticator(mailtest.java:13)

java email smtp apache-commons-email

3
推荐指数
1
解决办法
6759
查看次数

如何发送带Play附件的电子邮件!框架1.2.4

试着玩!发送带附件的电子邮件的框架.如果我不在邮件中添加附件,则以下代码可以正常工作.我已经尝试过使用Play的Mailer类和Apache Commons类(如下所示),但在这两种情况下,页面都只使用微调器(Chrome)并且没有收到任何电子邮件.

EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL(base + "public/images/triangles.png"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("test");
attachment.setName("test");

emailaddress = "test@test.com";

MultiPartEmail email = new MultiPartEmail();
email.setDebug(true);
email.addTo(emailaddress);
email.setFrom("Testing <test@test.com>");
email.setSubject("Testing email");
try
{
    email.attach(attachment);
}
catch (EmailException ex)
{
    System.out.println(ex.getMessage());
}
email.setMsg("test email");
email.send();
Run Code Online (Sandbox Code Playgroud)

java playframework apache-commons-email

2
推荐指数
1
解决办法
2391
查看次数

Javamail/email commons:空消息给出了奇怪的错误

我正在尝试通过GMail发送电子邮件(实际上是Google Apps).这适用于普通消息,但当然我必须测试极端情况.

现在我正在尝试发送没有正文的消息,我收到一条奇怪的错误消息.因为我想消除代码中的错误,我想知道我做错了什么,或者我正在使用的库中是否有错误.

HtmlEmail currentMessage = new HtmlEmail();
currentMessage.setSSL(true);
currentMessage.setSslSmtpPort(465);
currentMessage.setHostName("smtp.gmail.com");
currentMessage.setAuthentication("abc@gmail.com", "secret");

/** rs is a ResultSet from database **/

if(rs.getString("mailFrom") != null && !rs.getString("mailFrom").isEmpty())
    currentMessage.setFrom(rs.getString("mailFrom"));
else
    currentMessage.setFrom("abc@gmail.com");
currentMessage.setTo(this.convertRecipientStringToArray(rs.getString("mailTo")));
currentMessage.setCc(this.convertRecipientStringToArray(rs.getString("mailCC")));
currentMessage.setBcc(this.convertRecipientStringToArray(rs.getString("mailBC")));
currentMessage.setSubject(rs.getString("mailSubject"));
if(rs.getString("mailBody") != null && !rs.getString("mailBody").isEmpty())
    currentMessage.setTextMsg(rs.getString("mailBody"));
if(rs.getString("mailHtmlBody") != null && !rs.getString("mailHtmlBody").isEmpty())
    currentMessage.setHtmlMsg(rs.getString("mailHtmlBody"));
if(rs.getString("mailReplyTo") != null && !rs.getString("mailReplyTo").isEmpty())
    currentMessage.addReplyTo(rs.getString("mailReplyTo"));
else
    currentMessage.addReplyTo("def@gmail.com");
currentMessage.send();
Run Code Online (Sandbox Code Playgroud)

此代码适用于"普通"电子邮件:具有有效的正文,主题,收件人等.

当数据库中的mailBody mailHtmlBody都为NULL或为空时,我收到以下错误:

将电子邮件发送到以下服务器失败:smtp.gmail.com:25

完整错误日志(自己的日志格式)/ stacktrace:

[CRITICAL] 2012-12-19 17:08:00 [CET] - Exception occurred in function com.mypackage.mymailobject.outgoing_sendMails: Sending the email to the …
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail apache-commons-email

2
推荐指数
1
解决办法
7036
查看次数