aspose file tools*
The moose likes Java in General and the fly likes A good way to send out a bulk of emails ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "A good way to send out a bulk of emails ?" Watch "A good way to send out a bulk of emails ?" New topic
Author

A good way to send out a bulk of emails ?

Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi, I'm currently sending out 2000 emails this way, using code in #1 a loop to send out messages (plain text, no attachments) and #2 method. My question is that I'm going through Exchange Server, and if I do this, I 'm concerned that this may be regarded as spam and also be very slow or even get rejections. Is this a good way? or is there a better way of doing this? Thank you for any example code or input that you may have.
-noijet

#1. Code to send out all mail
for(int index = 1; index <= 2000; index++){
employee = emailSendeeList.get(index);
String letter = "hello test";
mailMgr.sendManyEmail(employee.getEmail(), letter);
}


#2. Actual send method (Shortened version, some code left out)
public ActionErrors sendManyEmail( String emailAddress, String letter){
...more code
MimeMessage emailMessage = this.mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(emailMessage, true, "UTF-8");
helper.setTo(emailAddress);
helper.setFrom( this.message.getFrom());
this.mailSender.send(registMessage);//Send out message
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8924
    
    9

Welcome to the JavaRanch.
I'd use CC or BCC for all the addresses and send one email (I don't know if there's a limitation to those fields). Better yet, I'd set up a distribution list in Exchange and send one email to it.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi Joe, thanks for your reply! I could put all the address's as a CC, but the server person said that the limit is 1000 emails at once, that's why I'm going through the loop, 1 by 1 sending out emails. Performance may be compromised, I was wondering if I can do this:

*** Put 500 addresses on CC's and then somehow do a delay and go to next 500, do you know a way to do this using my loop?

Thanks,
noijet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Please for the love of God do NOT use CC!
All addresses in the CC list are visible to all receivers; if one of those is a spammer he gets 500 addresses for free.

Always use BCC when sending to multiple people. ALWAYS!


Anyway, about the sending, do something like this (pseudo-code):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi Rob! thanks for reply and code. In your experience, do you think this would cause problems to the mail server? I've talked with the server group, they told me that "no more than 1000 emails at once" but in your code, I am not doing "1000 at once", I would be sending out 50 at once and then then the next 50 is considered a different job, so they should be okay with that. Am I right?

Thanks so much Rob !
noijet
BTW, who's your favorite g1 gestalt transformer?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Originally posted by Noijet Chen:
Hi Rob! thanks for reply and code. In your experience, do you think this would cause problems to the mail server? I've talked with the server group, they told me that "no more than 1000 emails at once" but in your code, I am not doing "1000 at once", I would be sending out 50 at once and then then the next 50 is considered a different job, so they should be okay with that. Am I right?

Thr 50 is just an example, but basically this way you're sending 2000 / 50 = 40 emails. You could try 100 for a total of 20 emails, just play around with it.

BTW, who's your favorite g1 gestalt transformer?

In the cartoon it's Bruticus, but the Defensor toy is definitely the best there is.
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi Rob, thanks for your reply. If I do have some personal information on the emails (emailee's name and address), then I would have to use my old way right (using loop) ? For example, if for each email I did this:
String letter = "hello " + employee.getName();
mailMgr.sendManyEmail(employee.getEmail(), letter);

Thank you,
noijet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Correct.

The few emails with many recipients only works if the emails can be exactly the same for all. As soon as you want to do something different for each email this approach can no longer be used.
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi Rob, I see that you do 'message.addBCC' does this override the previous bcc or does it attach to the other bcc addresses?

Thanks so much,
noijet
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi Rob, I'm using MimeMessageHelper helper to set my message criteria, it does not have some methods like clearBcc() or hasBCC(), I was wondering if there is an alternative way so that I can use my helper object ?

Thank you,
noijet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Originally posted by Noijet Chen:
Hi Rob, I see that you do 'message.addBCC' does this override the previous bcc or does it attach to the other bcc addresses?

Thanks so much,
noijet

It should attach it to the other BCC addresses.

Originally posted by Noijet Chen:
Hi Rob, I'm using MimeMessageHelper helper to set my message criteria, it does not have some methods like clearBcc() or hasBCC(), I was wondering if there is an alternative way so that I can use my helper object ?

Thank you,
noijet

I don't know MimeMessageHelper, but you can create another helper yourself which just stores a number of addresses. You can add to that and clear that. Just before clearing it you retrieve the addresses and assign them to the MimeMessageHelper.
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi Rob, thanks so, so much for your help! I was wondering what helper class that you know about that has methods: addBcc(), clearBcc(), hasBcc(). I tried searching through the class you used (Message.java), I couldn't find them.

Thanks so much,
noijet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

There isn't one, you'd have to create it yourself.

Basically it could be something as simple as a collection (list / set) where you use the toArray() method to get an array of addresses:

[ September 20, 2007: Message edited by: Rob Prime ]
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Thanks so much Rob ! I will try this out, and get back to you if I have any questions. Would you still be able to see my post/you get emailed with my post ?

Thank you,
noijet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

I check this board at least once every 2-3 days, so I'll notice anything.
Noijet Chen
Greenhorn

Joined: Sep 18, 2007
Posts: 9
Hi Rob, I'm going to start writing this code, but I was wondering:

1. How can I test this code out? I have one test gmail account, but how can I mimic sending out to 1000 email accounts. On outlook, I tried to do bcc: "test@abc.com, test@abc.com, ..." but test@abc.com only gets one email.

2. Also, I noticed on Outlook, when I send out a bcc, I cannot do "test@abc.com,test@abc.com", I have to use ";" like this: "test@abc.com;test@abc.com". Which character ";" or "," is safest as a delimiter?

Thank you,
noijet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

InternetAddress.parse only allows , so that would be best in this case. I wrote a helper method that can accept both, but all that does is replace all ;s with ,s and then call InternetAddress.parse.

As for the second problem, you could try creating some account with catch-alls. I know that www.cjb.net has something like that. When you register you get a "domain"; everything sent to <anything>@<yourname>.cjb.net will appear in your mailbox. As far as I know, they are treated as different email addresses until they enter the mail box (although I haven't tried this).

You could send emails to user1@..., user2@..., etc. I'd try with a small number (at most 50) at first, because you don't want to spam yourself with 1000 emails
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A good way to send out a bulk of emails ?