Solved

Sending Mail twice using JavaMail

Posted on 2011-03-01
5
1,047 Views
Last Modified: 2012-05-11
Experts,

I have this method that sends an email to the requester once its called. instead it sends two email. jst like it is called twice. i reviewed the code make sure i called the mthod once and call sendMsg once.


my code is below, could anyone tell me what could be cause of the problem ? ><

Thanks
Shamma
Config conf = new Config("uaeu_server_config.prop");
  String host= conf.get("mail_server"); //"195.229.162.3";
  int port =Integer.parseInt(conf.get("mail_port"));
  String[] recipients = to.split(",");
  InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[recipients.length];
  for (int i = 0; i < recipients.length; i++){
	  System.out.println();
    addressTo[i] = new javax.mail.internet.InternetAddress(recipients[i]);}
  Collection toCollection = Arrays.asList(addressTo); 
  HtmlEmail htmlEmail = new HtmlEmail();
  htmlEmail.setHostName(host);
  htmlEmail.setTo(toCollection);
  htmlEmail.setFrom(from);
  htmlEmail.setSubject(subject);
  htmlEmail.setHtmlMsg(emailTemplate);

  // set the alternative message
  htmlEmail.setTextMsg("Your email client does not support HTML messages");

  // send the email
  htmlEmail.send();

Open in new window

0
Comment
Question by:Web_Admin
5 Comments
 
LVL 9

Accepted Solution

by:
ioanton earned 167 total points
ID: 35005499
>>>String[] recipients = to.split(",");

Can you post here the value of the variable "to"?
0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 167 total points
ID: 35005510
just to confirm, that it is not called twice, please put a log (System.out.println) before line 21, and let us know if that log is not printed twice

also print the recipient length on line 6, see if these are the same names
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 166 total points
ID: 35005523
Does it send two messages conssitently all the time, or only from time to time?

I'm asking because I'm using JavaMail extensively and although in absolute majority
of cases it works as expected, from time to time I experience cases of double emails.
So maybe it is some quirk in JavaMail itself. But I experience this behavior in fact very rarely. perhaps once
in several hundred emails.
If you see it consistently all the time than this is probably another issue.  
0
 

Author Comment

by:Web_Admin
ID: 35005778
ioanton : the value is one email address

gurvinder372: i already added the two line in the begning of the method and at the end. it is printed once.

for_yan: it is happening today, was working fine with me ><

----

I use apache common mail library http://commons.apache.org/email/userguide.html
when i remove this line "htmlEmail.send();", it sends the email one time ><
0
 

Author Comment

by:Web_Admin
ID: 35005817
ok guys thank you all for ur response , i found that i there is inculde file i added that call this method too :D

Thanks, points divided
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to open debugger port in Intellij idea 6 140
splitOdd10 challenge 5 74
Hibernate Not Rolling Back When something fails in a stored procedure 2 50
jar file executable 12 38
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now