How to configure the Quarkus Mailer extension to allow dynamic 'from' email addresses based on user?

huangapple go评论52阅读模式
英文:

How to configure the Quarkus Mailer extension to allow dynamic 'from' email addresses based on user?

问题

[这个 Quarkus 邮件指南](https://quarkus.io/guides/mailer-reference) 要求在属性文件中预配置发送邮件的地址:`quarkus.mailer.from=YOUREMAIL@gmail.com`。然而,我的电子邮件使用情况包括基于用户的唯一发件人电子邮件。使用提供的方法看起来像是:

```java
public void sendEmail(EmailSender emailSender) {

    // 发送给每个收件人
    emailMessageRepository.findByEmailSenderId(emailSender.getId())
        .forEach(emailMessage ->
                mailer.send(
                    Mail.withText(emailMessage.getEmail(),
                    emailSender.getSubject(),
                    emailSender.getMessage())
            );
    );
}

Mail.withText() 方法只提供接收方电子邮件时,我如何包括发件人的电子邮件地址(即 'from')?


<details>
<summary>英文:</summary>

[This Quarkus mailer guide](https://quarkus.io/guides/mailer-reference) requires that the sending email is preconfigured in property file: `quarkus.mailer.from=YOUREMAIL@gmail.com`. However, my use case for email includes unique originator email based on user. Using the provided method looks something like:

public void sendEmail(EmailSender emailSender) {

// Send to each recipient
emailMessageRepository.findByEmailSenderId(emailSender.getId())
    .forEach(emailMessage -&gt;
            mailer.send(
                Mail.withText(emailMessage.getEmail(),
                emailSender.getSubject(),
                emailSender.getMessage())
        );
);

}

How can I include the sender&#39;s email address (i.e. &#39;from&#39;) when the `Mail.withText()` method only provides for recipient email?

</details>


# 答案1
**得分**: 1

文档展示了如何使用多发件人地址(Multimailer,Multiple From Addresses)

```plaintext
quarkus.mailer.from=your-from-address@gmail.com 
quarkus.mailer.host=smtp.gmail.com

quarkus.mailer.aws.from=your-from-address@gmail.com 
quarkus.mailer.aws.host=${ses.smtp}
quarkus.mailer.aws.port=587

quarkus.mailer.sendgrid.from=your-from-address@gmail.com 
quarkus.mailer.sendgrid.host=${sendgrid.smtp-host}
quarkus.mailer.sendgrid.port=465

因此,您可以编写以下内容:

quarkus.mailer.from=default@gmail.com 

quarkus.mailer.aws.from=your_aws@gmail.com 

quarkus.mailer.sendgrid.from=your_sendgrid@gmail.com 

然后,您可以按照下面所示进行注入,并根据您想要发送的对象使用它们:

@Inject
@MailerName("aws") 
Mailer mailer;

@Inject
@MailerName("sendgrid") 
Mailer mailer;

awssendgrid 是在 quarkus.mailer.xxx.from 之间的名称。

详细信息请参阅:https://quarkus.io/guides/mailer-reference#multiple-mailer-configurations


Quarkus Mailer 是基于 Vert.x Mail Client 实现的,提供了异步和非阻塞的发送电子邮件的方式。

如果 您需要对邮件发送方式进行精细控制,例如需要检索消息 ID,您可以注入底层客户端并直接使用它:

@Inject MailClient client;

然后使用它:

MailMessage message = new MailMessage();
message.setFrom("user@example.com (Example User)");
message.setTo("recipient@example.org");
message.setCc("Another User <another@example.net>");
message.setText("this is the plain message text");
message.setHtml("this is html text <a href=\"http://vertx.io\">vertx.io</a>");

要使用 MailClient 发送:

mailClient.sendMail(message)
  .onSuccess(System.out::println)
  .onFailure(Throwable::printStackTrace);

详细信息请参阅:https://quarkus.io/guides/mailer-reference#using-the-underlying-vert-x-mail-client

以及:https://vertx.io/docs/vertx-mail-client/java/

英文:

The documention showcases how to use multimailer (Multiple From Addresses)

quarkus.mailer.from=your-from-address@gmail.com 
quarkus.mailer.host=smtp.gmail.com

quarkus.mailer.aws.from=your-from-address@gmail.com 
quarkus.mailer.aws.host=${ses.smtp}
quarkus.mailer.aws.port=587

quarkus.mailer.sendgrid.from=your-from-address@gmail.com 
quarkus.mailer.sendgrid.host=${sendgrid.smtp-host}
quarkus.mailer.sendgrid.port=465

So you would write:

quarkus.mailer.from=default@gmail.com 

quarkus.mailer.aws.from=your_aws@gmail.com 

quarkus.mailer.sendgrid.from=your_sendgrid@gmail.com 

Then you would inject them as shown below and use them based on whom you want to send with:

@Inject
@MailerName(&quot;aws&quot;) 
Mailer mailer;


@Inject
@MailerName(&quot;sendgrid&quot;) 
Mailer mailer;

aws and sendgrid at the names between quarkus.mailer.xxx.from

https://quarkus.io/guides/mailer-reference#multiple-mailer-configurations


> The Quarkus Mailer is implemented on top of the Vert.x Mail Client,
> providing an asynchronous and non-blocking way to send emails.

If you need fine control on how the mail is sent, for instance if you need to retrieve the message ids, you can inject the underlying client, and use it directly:

@Inject MailClient client;

Then use it:

MailMessage message = new MailMessage();
message.setFrom(&quot;user@example.com (Example User)&quot;);
message.setTo(&quot;recipient@example.org&quot;);
message.setCc(&quot;Another User &lt;another@example.net&gt;&quot;);
message.setText(&quot;this is the plain message text&quot;);
message.setHtml(&quot;this is html text &lt;a href=\&quot;http://vertx.io\&quot;&gt;vertx.io&lt;/a&gt;&quot;);

To send using MailClient:

mailClient.sendMail(message)
  .onSuccess(System.out::println)
  .onFailure(Throwable::printStackTrace);

https://quarkus.io/guides/mailer-reference#using-the-underlying-vert-x-mail-client

https://vertx.io/docs/vertx-mail-client/java/

huangapple
  • 本文由 发表于 2023年6月1日 18:15:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定