Java 在不透露其他收件人的情况下向多个收件人发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4320100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Email multiple recipients without revealing other recipients
提问by Don
I'm using javamail to send emails to a list of recipients, but don't want them to be able to see who else received the email. I also don't want to send it using BCC since then the user doesn't even see themselves in the TO list. I thought this code would do it, but it shows all the recipients in the TO list. Other than creating a loop and sending the emails one at a time, is there another way to do this?
我正在使用 javamail 向收件人列表发送电子邮件,但不希望他们能够看到还有谁收到了电子邮件。我也不想使用密件抄送发送它,因为那时用户甚至在 TO 列表中都看不到自己。我认为这段代码可以做到,但它显示了 TO 列表中的所有收件人。除了创建一个循环并一次发送一个电子邮件之外,还有其他方法可以做到这一点吗?
(NOTE: recipients[] is a string array containing the email addresses.)
(注意:recipients[] 是一个包含电子邮件地址的字符串数组。)
javax.mail.internet.InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new javax.mail.internet.InternetAddress(recipients[i]);
}
msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
回答by Camilo Díaz Repka
No, there isn't a way to do this with email.
不,没有办法通过电子邮件做到这一点。
You have to explicitly build and send an email iterating by each of your recipients, one of them as the sole member of your addressTo
array.
您必须显式地构建并发送由每个收件人迭代的电子邮件,其中一个作为addressTo
数组的唯一成员。
回答by dhable
The SMTP protocol doesn't care who's listed in the message and the recipients specified on the RCPT TO command are only used to figure out who to transport the message to. There's nothing stopping you from building the RFC822 message with the To header as you've defined above and then writing a custom SMTP client that send your particular message out but with a different set of recipients. And just because you can send the message doesn't mean a spam filter along the way is going to notice the wonky recipient headers and block the message.
SMTP 协议不关心邮件中列出的人,并且 RCPT TO 命令中指定的收件人仅用于确定将邮件传输给谁。没有什么能阻止您使用上面定义的 To 标头构建 RFC822 消息,然后编写一个自定义 SMTP 客户端,该客户端将您的特定消息发送出去,但具有一组不同的收件人。并且仅仅因为您可以发送邮件并不意味着垃圾邮件过滤器会注意到不稳定的收件人标题并阻止邮件。
In my experience, JavaMail's SMTP client is really good at sending basic messages without any of the mail tricks you often seen used by mailing list providers and spammers. Those companies spend a lot of effort to make sure they can send messages the way they want but they also are in a constant fight to make sure they're messages are treated as legit email.
根据我的经验,JavaMail 的 SMTP 客户端非常擅长发送基本消息,而没有您经常看到的邮件列表提供商和垃圾邮件发送者使用的任何邮件技巧。这些公司花费了大量精力来确保他们能够以他们想要的方式发送消息,但他们也在不断努力以确保他们的消息被视为合法的电子邮件。
Short answer: I'd resort to BCC and if this is for marketing purposes, consider using a company that specializes in this kind of thing.
简短回答:我会求助于 BCC,如果这是出于营销目的,请考虑使用专门从事此类事情的公司。
回答by aksarben
Why are you concerned about the recipient not seeing his own address? He already knows his his own address, so why is it an issue? BCC was designed to handle exactly the problem you describe. It's been around for decades & sounds like a perfect fit.
为什么您担心收件人看不到自己的地址?他已经知道他自己的地址了,为什么会有问题呢?BCC 旨在准确处理您所描述的问题。它已经存在了几十年,听起来非常合适。
回答by Maxy-B
According to the documentation for javax.mail.Transport
:
根据文档javax.mail.Transport
:
public static void send(Message msg,
Address[] addresses)
throws MessagingException
Send the message to the specified addresses, ignoring any recipients specified
in the message itself.
So you should be able to put the actual delivery addresses (RCPT TO addresses) in the array argument to Transport.send
, while putting whatever you want the recipients to see in the message headers via Message.setRecipient
, MIMEMessage.addHeader
, etc.
所以,你应该能够把实际的送货地址(RCPT TO地址)的数组变量Transport.send
,而把任何你想要的收件人通过邮件头看Message.setRecipient
,MIMEMessage.addHeader
等
If you want different sets of users to see different things, you will have to construct and send a separate message for each set.
如果您希望不同的用户组看到不同的内容,则必须为每个组构建和发送单独的消息。
回答by Manjunath D R
Actually, we don't have to manually create InternetAddress objects for Multi Recepients. InternetAddress api provides a parse() method to do this for us. Sample code for this is as below,
实际上,我们不必为多接收者手动创建 InternetAddress 对象。InternetAddress api 提供了一个 parse() 方法来为我们做这件事。示例代码如下,
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
Here parse method creates multiple InternetAddress objects if toAddress contains multiple email addresses seperated by ,(comma).
如果 toAddress 包含多个以 ,(逗号)分隔的电子邮件地址,则 parse 方法将创建多个 InternetAddress 对象。
Check for below API for more details.
检查以下 API 以获取更多详细信息。
http://docs.oracle.com/javaee/6/api/javax/mail/internet/InternetAddress.html#parse(java.lang.String)
http://docs.oracle.com/javaee/6/api/javax/mail/internet/InternetAddress.html#parse(java.lang.String)
Happy Coding. :)
快乐编码。:)
回答by titi
as Message.RecipientType you should use Message.RecipientType.BCC to not showing the every address to every recipient
作为 Message.RecipientType,您应该使用 Message.RecipientType.BCC 来不向每个收件人显示每个地址
Google Keywords: Java Mail BCC
Google 关键字:Java 邮件密件抄送
回答by Naoufel ELYOUNOUSSI
You can do this by setting the code as below
您可以通过如下设置代码来做到这一点
message.setRecipients(Message.RecipientType.BCC, toAddrs);
instead of
代替
message.setRecipients(Message.RecipientType.TO, toAddrs);
回答by james leeroy Dator
Try this:
尝试这个:
Session session = Session.getInstance(properties); Transport transport = session.getTransport("smtp"); String recipient = "[email protected],ex2@mail."; String[] recipients = recipient.split(","); transport.connect(server, username, password); for (String to : recipients) { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; message.setRecipients(Message.RecipientType.TO, address); message.setSubject(subject); message.setText(body); message.setContent(body, "text/plain"); message.saveChanges(); transport.sendMessage(message, address); } transport.close();
Session session = Session.getInstance(properties); Transport transport = session.getTransport("smtp"); String recipient = "[email protected],ex2@mail."; String[] recipients = recipient.split(","); transport.connect(server, username, password); for (String to : recipients) { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; message.setRecipients(Message.RecipientType.TO, address); message.setSubject(subject); message.setText(body); message.setContent(body, "text/plain"); message.saveChanges(); transport.sendMessage(message, address); } transport.close();